2014-01-31 45 views
0

在應用上下文xml文件中缺少context:annotation-config元素的情況下,我測試了自動佈線的行爲。令我驚訝的是,它的工作原理是一樣的。Spring @Autowired working without context:annotation-config

因此,這裏是我的問題: 怎麼來的AutowiredAnnotationBeanPostProcessor註冊在ApplicationContext中即使背景:註釋-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> 

這是應用程序上下文配置文件,與上下文:註釋-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。

回答

4

採用<context:component-scan />意味着基於註解的配置,因此指定<context:annotation-config/>是多餘的。

0

'context:annotation-config'此批註配置標籤用於處理應用程序上下文XML文件中聲明的自動連線bean。如果可以使用'context:component-scan'標籤的範圍發現自動連線bean,那麼不需要使用'context:annotation-config'標籤