Spring @Autowired工作没有上下文:annotation-config

时间:2014-01-31 14:35:03

标签: spring annotations autowired

我已经测试了自动连线的行为,以防上下文:应用程序上下文xml文件中缺少annotation-config元素。令我惊讶的是它的工作原理是一样的。

所以这是我的问题: 为什么在ApplicationContext中注册了AutowiredAnnotationBeanPostProcessor,即使应用程序上下文配置文件中缺少context:annotation-config元素,或者该配置有什么其他机制可用?

我正在使用Spring版本3.0.6.RELEASE 这是项目pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-    v4_0_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.samples.spring</groupId>
    <artifactId>spring-utility</artifactId>
    <version>1.0.0.CI-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Spring Utility</name>
    <url>http://www.springframework.org</url>
    <description>
        <![CDATA[
  This project is a minimal jar utility with Spring configuration.
]]>
    </description>
    <properties>
        <maven.test.failure.ignore>true</maven.test.failure.ignore>
        <spring.framework.version>3.0.6.RELEASE</spring.framework.version>
    </properties>   
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>

        </plugins>
    </build> 
</project>

这是应用程序上下文配置文件,其中包含上下文:annotation-config元素:

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

    <description>Example configuration to get you started.</description>

    <!-- context:annotation-config/ -->

    <context:component-scan base-package="com.foo.ch04" />

</beans>

MessageProvider,将由依赖bean MessageRenderer用作协作者

package com.foo.ch04.helloworld;

import org.springframework.stereotype.Service;

@Service("messageProvider")
public class HelloWorldMessageProvider implements MessageProvider {

    public String getMessage() {
        return "Hello, World!";
    }

}

MessageRenderer,其依赖性messageProvider被自动注入:

package com.foo.ch04.helloworld;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("messageRenderer")
public class StandardOutMessageRenderer implements MessageRenderer {

    private MessageProvider messageProvider;

    public void render() {
        if (messageProvider == null) {
            throw new RuntimeException(
                    "You must set the property messageProvider before rendering     message.");
        }
        System.out.println(messageProvider.getMessage());
    }

    @Autowired
    public void setMessageProvider(MessageProvider provider) {
        messageProvider = provider;
    }

    public MessageProvider getMessageProvider() {
        return messageProvider;
    }

}    

加载应用程序上下文并测试messageRenderer的测试应用程序:

package com.foo.ch04.helloworld;

import org.springframework.context.support.GenericXmlApplicationContext;

public class DeclareSpringComponents {

    public static void main(String[] args) {
        GenericXmlApplicationContext context = new GenericXmlApplicationContext();
        context.load("classpath:META-INF/spring/app-context-annotation.xml");
        context.refresh();

        MessageRenderer renderer = context.getBean("messageRenderer",
                MessageRenderer.class);
        renderer.render();
    }

}

即使应用程序上下文配置文件中缺少该消息,也会显示消息“Hello,World!”在运行应用程序时写入stdout。

3 个答案:

答案 0 :(得分:4)

他们的功能重叠。看看这个类似的问题:

annotation-config vs component-scan

答案 1 :(得分:4)

使用<context:component-scan />意味着基于注释的配置,因此指定<context:annotation-config/>是多余的。

答案 2 :(得分:0)

'context:annotation-config' This annotation-config tag is used to process the auto wired beans declared in the application context XML file. If the auto wired bean could be discovered using the scope of 'context:component-scan' tag then no need to use 'context:annotation-config' tag