Spring @Autowired working without context:annotation-config -


i've tested behavior of auto wiring in case context:annotation-config element missing in application context xml file. surprise worked same.

so here question: how come autowiredannotationbeanpostprocessor registered in applicationcontext though context:annotation-config element missing application context configuration file, or else mechanism makes configuration work?

i'm using spring version 3.0.6.release project pom file:

<?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[   project minimal jar utility 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> 

this application context configuration file, context:annotation-config element commented out:

<?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 started.</description>      <!-- context:annotation-config/ -->      <context:component-scan base-package="com.foo.ch04" />  </beans> 

a messageprovider, used collaborator dependent 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!";     }  } 

the messagerenderer, dependency messageprovider gets auto-injected:

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 property messageprovider before rendering     message.");         }         system.out.println(messageprovider.getmessage());     }      @autowired     public void setmessageprovider(messageprovider provider) {         messageprovider = provider;     }      public messageprovider getmessageprovider() {         return messageprovider;     }  }     

the test application loading application context , testing 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();     }  } 

even though missing in application context configuration file, message "hello, world!" written stdout when application run.

their functionality overlapping. @ similar question here:

annotation-config vs component-scan


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -