Ram72119
Ram72119

Reputation: 107

why @autowired is not working when I access a bean

When I access a bean from spring bean configuration file using BeanFactory like this:

public class Person {
    private String id,address;
    @Autowired
    private Customer customer;
     //setters & getters
    }

and bean configuration file

<bean name="person" class="com.ram.spring.model.Person"></bean>
<bean class="com.ram.spring.model.Customer">
    <property name="email" value="[email protected]"></property>
    <property name="name" value="Ram"></property>
</bean>

here is the executor class

public class PersonExecutor {

    public static void main(String[] args) {
        BeanFactory context = new XmlBeanFactory(new ClassPathResource("Spring.xml"));
        Person person = (Person)context.getBean("person");
        System.out.println(person.getCustomer());
    }

}

when I execute this, I got null.is BeanFactory not supported for annotations?? any ideas??

Upvotes: 0

Views: 999

Answers (4)

Chop
Chop

Reputation: 4517

Why doesn't it work?

When using Spring with an XML context, using annotations is not activated by default. This means @Autowired, @Transactional, @PostConstruct and any other annotation you will use will simply not be exploited.

How do I fix it?

To make Spring aware of annotations, you need to add the following line:

<context:annotation-config />

Thus, Spring will search for annotations in the beans it creates and process them accordingly.

This requires activating the context namespace. At the top of your context, make sure you have all context related links and arguments1:

<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.0.xsd">

    <context:annotation-config />

    <!-- Your context -->
</beans>

You do not need <context:component-scan /> in your case. This would be useful if you used full-annotation context (e.g. classes annotated with @Component). See the difference between <context:annotation-config /> and <context:component-scan /> in this answer.

Alternate solution

As Naman Gala suggested, you could also drop @Autowired completely and inject all dependencies in XML. See the related answer for more details.


1 This includes the xmlns:context attribute (xmlns = XML NameSpace) and two URLs in xsi:schemaLocation.

Upvotes: 0

Naman Gala
Naman Gala

Reputation: 4692

Approach 1: Include below code in your xml

<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.0.xsd">

    <context:annotation-config />

    <!-- Remaining bean declaration -->
</beans>

Approach 2: Remove @Autowired and inject customer in your xml file only.

<bean name="person" class="com.ram.spring.model.Person">
    <property name="customer" ref="customer"></property>
</bean>
<bean name="customer" class="com.ram.spring.model.Customer">
    <property name="email" value="[email protected]"></property>
    <property name="name" value="Ram"></property>
</bean>

Upvotes: 3

Javy
Javy

Reputation: 954

As @jens suggested

you should active annotation scan

<context:component-scan base-package="package_path">
</context:component-scan>
<context:annotation-config />

hope that helped

Upvotes: 0

Jens
Jens

Reputation: 69439

You have to use AnnotationConfigApplicationContext or you have to add to yor Spring.xml to activate the annotation scan.

Upvotes: 1

Related Questions