2014-01-31 2 views
0

응용 프로그램 컨텍스트 xml 파일에 context : annotation-config 요소가없는 경우 자동 배선 동작을 테스트했습니다. 놀랍게도 그것은 똑같이 작동했습니다.Spring @ 컨텍스트없는 자동 작업 : annotation-config

그래서 내 질문은 다음과 같습니다. 컨텍스트 : annotation-config 요소가 응용 프로그램 컨텍스트 구성 파일에서 누락되었거나이 구성이 작동하는 다른 메커니즘이 있어도 어떻게 AutowiredAnnotationBeanPostProcessor가 ApplicationContext에 등록됩니까?

<?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 요소는 주석 :

나는 봄 버전 3.0.6.RELEASE 을 사용하고이 프로젝트 치어 파일입니다

<?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, 종속 콩 MessageRenderer 의해 협력자로서 사용될 것이다

package com.foo.ch04.helloworld; 

import org.springframework.stereotype.Service; 

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

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

} 
,691,363,210

그 종속성 messageProvider 자동 주입 얻는 MessageRenderer :

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'이 annotation-config 태그는 응용 프로그램 컨텍스트 XML 파일에 선언 된 자동 유선 콩을 처리하는 데 사용됩니다. 'context : component-scan'태그의 범위를 사용하여 자동 유선 빈을 검색 할 수 있다면 'context : annotation-config'태그를 사용할 필요가 없습니다.