0

I've tested the behavior of auto wiring in case the context:annotation-config element is missing in the application context xml file. To my surprise it worked just the same.

So here is my question: How come an AutowiredAnnotationBeanPostProcessor is registered in the ApplicationContext even though the context:annotation-config element is missing from the application context configuration file, or what else mechanism makes this configuration work?

I'm using Spring version 3.0.6.RELEASE This is the 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[
  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>

This is the application context configuration file, with the 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 to get you started.</description>

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

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

</beans>

A MessageProvider, that will be used as collaborator by a 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, whose 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 the 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 the application context and testing the 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 the is missing in the application context configuration file, the message "Hello, World!" is written to stdout when the application is run.

rexford
  • 5,192
  • 5
  • 27
  • 40
  • Their functionality is overlapping. Look at this similar question here: [annotation-config vs component-scan](https://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan) – jonasnas Jan 31 '14 at 14:43

2 Answers2

5

The use of <context:component-scan /> implies annotation based configuration and as such specifying <context:annotation-config/> is redundant.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • I read spring help doc,-->plays role of within scanned packages and is useful if we want Autowire external bean which is not defined using component scan. Can you pl suggest on this Sir? – Nagappa L M Mar 05 '21 at 21:14
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

J Jothish
  • 11
  • 2