Open In App

Spring MVC context:annotation-config VS context:component-scan

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn about Spring MVC <context:annotation-config> vs <context:component-scan>. Activating all annotations existing in Java beans that have already been registered either by definition in an application context file or by component scanning is the primary function of annotation-config. The requirement for registration is crucial. The component scan assigns the Spring Bean registration to Java classes annotated with @component, @service, @repository, and other attributes. It can do all the functions that annotation config can.

<context:annotation-config>

Annotations in beans previously registered in the application context can be activated using context:annotation-config, regardless of whether they were specified using XML or via package scanning. This implies that for beans that have already been produced and saved in the spring container, it will resolve the @Autowired and @Qualifier annotations.

Annotation Activation by <context:annotation-config>

Annotations for dependency injection are primarily activated via the <context:annotation-config> annotation. Some of those that <context:annotation-config> can resolve are @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource.In a unit test, we may make reference to our beans and dependencies:

Java




@Test
public void givenContextAnnotationConfig_whenDependenciesAnnotated_thenNoXMLNeeded() {
    ApplicationContext context
      = new ClassPathXmlApplicationContext("classpath:annotationconfigvscomponentscan-beans.xml");
  
    UserService userService = context.getBean(UserService.class);
    AccountService accountService = context.getBean(AccountService.class);
  
    Assert.assertNotNull(userService);
    Assert.assertNotNull(accountService);
    Assert.assertNotNull(userService.getAccountService());
}


<context:component-scan>

The context:component-scan tag does the same functions as context:annotation-config, plus it scans the packages and marks the stereotype-annotated classes as spring beans. Declaring each bean in the XML files would be unnecessary if the spring configuration file had the context:component-scan element.

Annotation Activation by <context:component-scan>

Essentially, package scanning is used by <context:component-scan> to identify the annotations. It instructs Spring about which packages to search in order to find the components or beans that have been annotated. <context:component-scan> may identify a number of them, including @Component, @Repository, @Service, @Controller, @RestController, and @Configuration.

XML




<context:component-scan base-package="org.geeksforgeeks">
  
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
    <context:include-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
  
</context:component-scan>


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

<context:annotation-config>

<context:component-scan>

<context:annotation-config> can resolve are @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource

<context:component-scan> may identify a number of them, including @Component, @Repository, @Service, @Controller, @RestController, and @Configuration.

In context, the beans are notregistered by the component-config.

In context, the beans are registered by the component-scan.

Determining beans and their dependencies is done using context:annotation-config.

Context:component-scan is used for general-purpose component auto-detection.

Annotations in beans that are already registered in the application context are activated using <context:annotation-config>.

Similar to <context:annotation-config>, <context:component-scan> may likewise search packages for beans inside the application context and register them.

Conclusion

In this article, we learnt the Java Spring MVC <context:annotation-config> vs <context:component-scan>. This tool assigns the Spring Bean registration to Java classes annotated with @component, @service, @repository, and other attributes. It can do all the functions that annotation config can.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads