• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring MVC Difference between context:annotation-config vs context:component-scan

April 20, 2015 by javainterviewpoint 1 Comment

We have already learnt the about basics of Spring MVC in my previous articles. we have used <context:annotation-config> and <context:component-scan> tags but we have not discussed much about it,In this tutorial we will see the difference between <context:annotation-config> and <context:component-scan> tags and use of them. so that we can use them effectively.

Difference between <context:annotation-config> vs <context:component-scan>

<context:annotation-config>

  • The <context:annotation-config> tag activates the annotation of the beans which is already registered in the application context. It doesn’t bother how it is registered if it is by <context:component-scan> or defined in the xml itself.
  • It mainly activates the 4 types of BeanPostProcessors
    • CommonAnnotationBeanPostProcessor : @PostConstruct, @PreDestroy, @Resource
    • AutowiredAnnotationBeanPostProcessor : @Autowired, @Value, @Inject, @Qualifier, etc
    • RequiredAnnotationBeanPostProcessor : @Required annotation
    • PersistenceAnnotationBeanPostProcessor :@PersistenceUnit and @PersistenceContext annotations

<context:component-scan>

  • The main function of <context:component-scan> tag is to register the beans to the context and also scans the annotations in the beans and activate them. In short what we can say is that <context:component-scan> does what <context:annotation-config> does as well as registers the beans to the context
    • <context:component-scan>=<context:annotation-config>+Bean Registration

Use of <context:annotation-config> and <context:component-scan>

Here we will create 3 different beans (Bean1, Bean2, Bean3) and will configure all the possible configurations to have a deeper idea about <context:component-scan> and <context:annotation-config>.

Bean1.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Bean1 
{
    private Bean2 bean2;
    private Bean3 bean3;
    
    public Bean1()
    {
        System.out.println("Creating bean bean1");
    }
    @Autowired
    public void setBean2(Bean2 bean2) {
        this.bean2 = bean2;
        System.out.println("Setting bean2 reference");
    }
    @Autowired
    public void setBean3(Bean3 bean3) {
        this.bean3 = bean3;
        System.out.println("Setting bean3 reference");
    }
}

Bean2.java

import org.springframework.stereotype.Component;

@Component
public class Bean2 
{
    public Bean2()
    {
        System.out.println("Creating bean bean2");
    }
}

Bean3.java

import org.springframework.stereotype.Component;

@Component
public class Bean3 
{
    public Bean3()
    {
        System.out.println("Creating bean bean3");
    }
}

Bean1 will have the reference of Bean2 and Bean3, and they are auto-wired with @Autowired annotation.

AutowireLogic.java

AutowireLogic is the base class where we read our config files and call-in the beans.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutowireLogic 
{
    public static void main(String[] args) 
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:SpringConfig.xml");
    }
}

Now lets write our configuration file(SpringConfig.xml) in some different variations and see their differences.

1. Only Bean Tag

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <bean id="bean1" class="com.javainterviewpoint.Bean1"></bean>
 <bean id="bean2" class="com.javainterviewpoint.Bean2"></bean>
 <bean id="bean3" class="com.javainterviewpoint.Bean3"></bean>
</beans>

Output will be

Creating bean bean1
Creating bean bean2
Creating bean bean3

Here we have created the bean and we have not set the values for the property in the Bean1

2. Bean Tag with property references

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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	
http://www.springframework.org/schema/mvc	
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

 <bean id="bean1" class="com.javainterviewpoint.Bean1">
 <property name="bean2" ref="bean2"></property>
 <property name="bean3" ref="bean3"></property>
 </bean>
 <bean id="bean2" class="com.javainterviewpoint.Bean2"></bean>
 <bean id="bean3" class="com.javainterviewpoint.Bean3"></bean>
</beans>

Output

Creating bean bean1
Creating bean bean2
Creating bean bean3
Setting bean2 reference
Setting bean3 reference

We have created the bean and we have injected reference to the property as well.

3. Only <context:annotation-config>

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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	
http://www.springframework.org/schema/mvc	
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

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

Output

You will get no output as we already know that <context:annotation-config> will activates the annotation of the bean which is already registered in the application context.

4. <context:annotation-config> and Bean definition

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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	
http://www.springframework.org/schema/mvc	
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

 <context:annotation-config></context:annotation-config>
 
 <bean id="bean1" class="com.javainterviewpoint.Bean1"></bean>
 <bean id="bean2" class="com.javainterviewpoint.Bean2"></bean>
 <bean id="bean3" class="com.javainterviewpoint.Bean3"></bean>
</beans>

Output

Creating bean bean1
Creating bean bean3
Setting bean3 reference
Creating bean bean2
Setting bean2 reference

Here as we have already registered the bean to the application context, <context:annotation-config> will activate the annotations over it.

5. Only <context:component-scan>

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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	
http://www.springframework.org/schema/mvc	
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

 <context:component-scan base-package="com.javainterviewpoint" />
</beans>

Output

Creating bean bean1
Creating bean bean3
Setting bean3 reference
Creating bean bean2
Setting bean2 reference

As we already know that <context:component-scan> registers the bean to the application context and as well as activates the annotation over them.

6. Putting it all together <context:component-scan>, <context:annotation-config> and Bean definition

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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	
http://www.springframework.org/schema/mvc	
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

 <context:component-scan base-package="com.javainterviewpoint"></context:component-scan>
 <context:annotation-config></context:annotation-config>
 <bean id="bean1" class="com.javainterviewpoint.Bean1" lazy-init="true"></bean>
 <bean id="bean2" class="com.javainterviewpoint.Bean2"></bean>
 <bean id="bean3" class="com.javainterviewpoint.Bean3"></bean>
</beans>

Output

Creating bean bean1
Creating bean bean3
Setting bean3 reference
Creating bean bean2
Setting bean2 reference

Even thought we have the configuration to discover the bean twice and activate the annotation, spring will fetch you the output only once.

Other interesting articles which you may like …

  • Spring 3 MVC Hello World Example
  • Spring MVC Form Handling Example
  • Spring MVC Form Validation Tutorial (With Annotations)
  • Spring MVC Form Validation -Annotations and ResourceBundle
  • Spring MVC Exception Handling @ExceptionHandler
  • Spring MVC Exception Handling @ControllerAdvice and @ExceptionHandler
  • Spring MVC Custom Exception Handling
  • Spring MVC Textbox Example
  • Spring MVC Password Example
  • Spring MVC Dropdown Box Example
  • Spring MVC Checkbox And Checkboxes Example
  • Spring MVC Radiobutton And Radiobuttons Example
  • Spring MVC TextArea Example
  • Spring MVC HiddenValue Example
  • Spring MVC BeanNameUrlHandlerMapping Example
  • Spring MVC ControllerClassNameHandlerMapping Example
  • Spring MVC SimpleUrlHandlerMapping Example
  • Spring MVC Flow Diagram

Filed Under: J2EE, Java, Spring, Spring MVC, Spring Tutorial Tagged With: context:annotation-config, context:component-scan, Difference between, Spring MVC

Comments

  1. kumar says

    July 8, 2016 at 9:37 pm

    Excellent explanation. Thank you!

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·