Explain the difference between context:annotation-config vs context:component-scan
Explain the difference between context:annotation-config vs context:component-scan
The difference between <context:annotation-config> and <context:component-scan> in Spring MVC revolves primarily around their roles in bean registration and annotation processing within the Spring container.
<context:annotation-config>The <context:annotation-config> tag is used specifically to activate annotations on beans that have already been registered in the Spring application context. This tag does not itself register any beans; instead, it is used to enhance beans that are already managed by the Spring container. It processes annotations like @Autowired, @Resource, @PostConstruct, @PreDestroy, and @Required on existing beans, regardless of how these beans were registered (either through XML configuration or through component scanning).
<context:component-scan>On the other hand, <context:component-scan> is a more comprehensive directive that not only activates annotations but also automatically detects and registers bean definitions. When you use <context:component-scan>, Spring scans the specified packages and automatically registers beans for any classes annotated with stereotype annotations such as @Component, @Service, @Repository, @Controller, etc. This tag effectively subsumes the functionality of <context:annotation-config> because it not only detects and registers beans but also activates annotations on them.
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.