Difference between annotation-config and component-scan

annotation-config and component-scan are important basic concepts of Spring Core.
This tutorial helps you understand how to use them and difference between them.

I. Overview

<context:annotation-config> activates all annotations in beans which are already registered in the application context. Those beans could be defined with XML or by package scanning.

<context:component-scan> not only does everything that annotation-config does, but also registers the java classes as a bean which are annotated with @Component, @Service ,@Repository…

II. Usage

Now begin with hierarchy that we want to create in our application:
-|Bean Name
-|Bean Address
-|Bean Customer
——|injected Bean Name
——|injected Bean Address

1. Full Beans in XML

The java class for Bean are:

To register those beans, the XML content should be:

Output when loading the Context:

2. Beans in XML with Java Annotation – <context:annotation-config>

If we use annotation to lessen quantity of referenced beans in XML, new Customer class will be:

Now we can remove referenced bean lines, and the content of XML file becomes:

Output when loading the Context:

– What happens with the setter injection methods?
– Why weren’t they called after being annotated with @Autowired?

The annotation itself doesn’t do anything if not being enabled. So annotation-config will handle this operation, it enables the annotated actions which are found on the beans defined in the same application context where we write <context:annotation-config>.

So the XML content should be:

Output when loading the Context:

3. Beans with Java Annotation – <context:component-scan>

In case that we wanna remove all beans definition in XML file, Spring provides appropriate annotations to do those things.
Our classes for beans should be:

Of course, we remove all beans in XML file, and the content just has only:

Now, run the application with this context.
Nothing shows for bean constructor or annotated methods in output.

Look back to the overview of <context:annotation-config>:
– “…activates all annotations in beans which are already registered in the application context.”
– “Those beans could be defined with XML or by package scanning.”

There is no bean which was registered in the context before. In previous step, annotations have effects because the bean was defined using XML.

So, we have a powerful package scanning: <context:component-scan>.
Because it “does everything that annotation-config does”, so we can replace annotation-config.

New content of XML file:

Now we can check the Output when loading the Context:

III. Source code

springannoconfig-composcan

Technology:
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

Build and Run:
– Config maven build: clean install
– Run project with mode Spring Boot App

By JavaSampleApproach | September 28, 2016.

Last updated on June 4, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*