@Autowired не работает с XML Configuration Spring - PullRequest
0 голосов
/ 11 июля 2019

У меня есть простой beans.xml файл, как показано ниже.

Имеет две фасоли:

Сотрудник и Адрес

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="address" class="com.atul.test.Address"></bean> 

    <bean id="employee" class="com.atul.test.Employee"> 
    </bean>

</beans>

У меня ниже классы Java и конфигурация.

public class Employee {

    @Autowired
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void checkAddress(){

        System.out.println("Your address is = "+this.address);
        this.address.vomit();


    }   
}
public class Address {

    public void vomit(){
        System.out.println("Vomit !!!!");
    }

}
public class App 
{
    public static void main( String[] args )
    {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Employee employee = (Employee)ctx.getBean("employee");

        System.out.println("Employee = "+employee);
        Address address = (Address)ctx.getBean("address");
        System.out.println("address = "+address);

        System.out.println("employee.address = "+employee.getAddress());

    }
}

Вопрос:

  1. Даже если у меня @Autowired на Сотрудник класс, Адрес не вводится

  2. Я получаю employee.address как NULL

  3. ctx.getBean("employee") и ctx.getBean("address") оба возвращают правильные бобы (non null)

  4. Насколько я понимаю, @Autowired должен работать, пока оба бина доступны в Spring context, что в данном случае верно

Ответы [ 2 ]

1 голос
/ 11 июля 2019

Вам нужно <context:annotation-config/> в вашем Spring XML, чтобы включить аннотацию Spring.

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context = "http://www.springframework.org/schema/context"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="address" class="com.atul.test.Address"></bean>

    <bean id="employee" class="com.atul.test.Employee"></bean>

</beans>
0 голосов
/ 11 июля 2019

Вы должны использовать @Component в вашем классе Адрес

@Component
public class Address {

    public void vomit(){
        System.out.println("Vomit !!!!");
    }

}

Вы должны использовать @Component на всех классах, которые вы хотите использовать с автонастройкой

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...