3

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

 8 months ago
source link: https://www.geeksforgeeks.org/spring-mvc-contextannotation-config-vs-contextcomponent-scan/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Courses
Practice

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 scanassigns 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:

@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.

<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.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 05 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK