Difference between and

前端 未结 15 2437
再見小時候
再見小時候 2020-11-22 04:44

I\'m learning Spring 3 and I don\'t seem to grasp the functionality behind and .

相关标签:
15条回答
  • 2020-11-22 05:21

    The <context:annotation-config> tag tells Spring to scan the codebase for automatically resolving dependency requirements of the classes containing @Autowired annotation.

    Spring 2.5 also adds support for JSR-250 annotations such as @Resource, @PostConstruct, and @PreDestroy.Use of these annotations also requires that certain BeanPostProcessors be registered within the Spring container. As always, these can be registered as individual bean definitions, but they can also be implicitly registered by including <context:annotation-config> tag in spring configuration.

    Taken from Spring documentation of Annotation Based Configuration


    Spring provides the capability of automatically detecting 'stereotyped' classes and registering corresponding BeanDefinitions with the ApplicationContext.

    According to javadoc of org.springframework.stereotype:

    Stereotypes are Annotations denoting the roles of types or methods in the overall architecture (at a conceptual, rather than implementation, level). Example: @Controller @Service @Repository etc. These are intended for use by tools and aspects (making an ideal target for pointcuts).

    To autodetect such 'stereotype' classes, <context:component-scan> tag is required.

    The <context:component-scan> tag also tells Spring to scan the code for injectable beans under the package (and all its subpackages) specified.

    0 讨论(0)
  • 2020-11-22 05:27

    Spring allows you to do two things:

    1. Autowiring of beans
    2. Autodiscovery of beans

    1. Autowiring
    Usually in applicationContext.xml you define beans and other beans are wired using constructor or setter methods. You can wire beans using XML or annotations. In case you use annotations, you need to activate annotations and you have to add <context:annotation-config /> in applicationContext.xml. This will simplify the structure of the tag from applicationContext.xml, because you will not have to manually wire beans (constructor or setter). You can use @Autowire annotation and the beans will be wired by type.

    A step forward for escaping the manual XML configuration is

    2. Autodiscovery
    Autodiscovery is simplifying the XML one step further, in the sense that you don't even need too add the <bean> tag in applicationContext.xml. You just mark the specific beans with one of the following annotation and Spring will automatically wire the marked beans and their dependencies into the Spring container. The annotations are as follow: @Controller, @Service, @Component, @Repository. By using <context:component-scan> and pointing the base package, Spring will auto-discover and wire the components into Spring container.


    As a conclusion:

    • <context:annotation-config /> is used in order to be able to use @Autowired annotation
    • <context:component-scan /> is used to determine the search of specific beans and attempt of autowiring.
    0 讨论(0)
  • 2020-11-22 05:29

    As a complementary, you can use @ComponentScan to use <context:component-scan> in annotation way.

    It's also described at spring.io

    Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's element.

    One thing to note, if you're using Spring Boot, the @Configuration and @ComponentScan can be implied by using @SpringBootApplication annotation.

    0 讨论(0)
  • 2020-11-22 05:31

    <context:annotation-config> activates many different annotations in beans, whether they are defined in XML or through component scanning.

    <context:component-scan> is for defining beans without using XML

    For further information, read:

    • 3.9. Annotation-based container configuration
    • 3.10. Classpath scanning and managed components
    0 讨论(0)
  • 2020-11-22 05:32

    The difference between the two is really simple!.

    <context:annotation-config /> 
    

    Enables you to use annotations that are restricted to wiring up properties and constructors only of beans!.

    Where as

    <context:component-scan base-package="org.package"/> 
    

    Enables everything that <context:annotation-config /> can do, with addition of using stereotypes eg.. @Component, @Service , @Repository. So you can wire entire beans and not just restricted to constructors or properties!.

    0 讨论(0)
  • 2020-11-22 05:32

    <context:annotation-config>: Scanning and activating annotations for already registered beans in spring config xml.

    <context:component-scan>: Bean registration + <context:annotation-config>


    @Autowired and @Required are targets property level so bean should register in spring IOC before use these annotations. To enable these annotations either have to register respective beans or include <context:annotation-config />. i.e. <context:annotation-config /> works with registered beans only.

    @Required enables RequiredAnnotationBeanPostProcessor processing tool
    @Autowired enables AutowiredAnnotationBeanPostProcessor processing tool

    Note: Annotation itself nothing to do, we need a Processing Tool, which is a class underneath, responsible for the core process.


    @Repository, @Service and @Controller are @Component, and they targets class level.

    <context:component-scan> it scans the package and find and register the beans, and it includes the work done by <context:annotation-config />.

    Migrating XML to Annotations

    0 讨论(0)
提交回复
热议问题