当getBean没有时,@ Resource会返回null

时间:2014-04-14 05:19:49

标签: java spring dependency-injection annotations

我有一段使用Spring依赖注入和ApplicationContext

的代码
public abstract class BigDataParent {

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

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

    ...
}

孩子:

public class BigBadChild extends BigDataParent  {

    private Something query;

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

        ....
    }
}

我想更改注入以使用注释而不是上下文,所以我刚刚将子类更改为

public class BigBadChild extends BigDataParent  {

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

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

        ....
    }
}

我还更改了我的xml配置文件并添加了

<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 />

但是现在我得到的查询是空的。我打赌我错过了一些基本的东西,但它是什么?谢谢。

编辑:

某个界面有多个实现,我在xml文件file.xml中选择实现。当我使用context.getBean()执行此操作时,它可以正常实施,但在使用@Reousrcejavax.annotation.Resource)时,我会变为空。

使用context.getBean()和@Resource时,流程有什么区别?有没有什么不同于初始化值的方式和时间可能会导致这种情况?

编辑#2:我的依赖列表:

<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>

1 个答案:

答案 0 :(得分:1)

指定

<context:annotation-config />

Spring注册了许多BeanPostProcessor bean来执行bean注入。其中一个是CommonAnnotationBeanPostProcessor,它处理@Resource个带注释的成员。

仅当程序运行时BeanPostProcessor位于类路径上时才会注册此javax.annotation.Resource

您必须运行应用程序而不将其提供给类路径。


请注意,您必须在编译时提供它,否则对它的任何引用都将失败。这与在运行时提供它不同。