简体   繁体   中英

@Resource returns null when getBean does not

I have a piece of code that uses Spring dependency injection with ApplicationContext

public abstract class BigDataParent {

    private static final Logger log = LoggerFactory.getLogger(BigDataOperation.class);

    protected static ApplicationContext context =
            new ClassPathXmlApplicationContext("file.xml");

    ...
}

And the child:

public class BigBadChild extends BigDataParent  {

    private Something query;

    @Override
    public void doSomething() {
        query = (Something) context.getBean("beanName");

        ....
    }
}

I want to change the injection to use annotations instead of context, so I have just changed the child class to

public class BigBadChild extends BigDataParent  {

    @Resource(name = "beanName")
    private Something query;

    @Override
    public void doSomething() {
        // Removed query = (Something) context.getBean("beanName");

        ....
    }
}

I have also changed my xml configuration file and have added

<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config />

But now I'm getting that query is null. I bet that I am missing something basic, but what is it? Thank you.

EDIT:

Something is an interface that has several implementations, I'm choosing the implementation in the xml file file.xml . When I'm doing so using context.getBean() , it works and I have the right implementation, but when using @Reousrce ( javax.annotation.Resource ) I'm getting null.

What is the difference in flow when using context.getBean() and @Resource? Is there something different to how and when the values are initialized that might cause this?

EDIT #2: My list of dependencies:

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.6</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.2</version>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>4.3.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
        <version>4.3.4.Final</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.175</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.6</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>2.4.9</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

</dependencies>

By specifying

<context:annotation-config />

Spring registers a number of BeanPostProcessor beans to perform bean injection. One of these is CommonAnnotationBeanPostProcessor which handles @Resource annotated members.

This BeanPostProcessor is only registered if javax.annotation.Resource is on the classpath when the program runs.

You must be running the application without providing it to the classpath.


Note that you must be providing it at compilation time, otherwise any references to it would fail. This is not the same as providing it at run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM