Difference between and

前端 未结 15 2427
再見小時候
再見小時候 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:35
    <context:annotation-config>
    

    Only resolves the @Autowired and @Qualifer annotations, that's all, it about the Dependency Injection, There are other annotations that do the same job, I think how @Inject, but all about to resolve DI through annotations.

    Be aware, even when you have declared the <context:annotation-config> element, you must declare your class how a Bean anyway, remember we have three available options

    • XML: <bean>
    • @Annotations: @Component, @Service, @Repository, @Controller
    • JavaConfig: @Configuration, @Bean

    Now with

    <context:component-scan>
    

    It does two things:

    • It scans all the classes annotated with @Component, @Service, @Repository, @Controller and @Configuration and create a Bean
    • It does the same job how <context:annotation-config> does.

    Therefore if you declare <context:component-scan>, is not necessary anymore declare <context:annotation-config> too.

    Thats all

    A common scenario was for example declare only a bean through XML and resolve the DI through annotations, for example

    <bean id="serviceBeanA" class="com.something.CarServiceImpl" />
    <bean id="serviceBeanB" class="com.something.PersonServiceImpl" />
    <bean id="repositoryBeanA" class="com.something.CarRepository" />
    <bean id="repositoryBeanB" class="com.something.PersonRepository" />
    

    We have only declared the beans, nothing about <constructor-arg> and <property>, the DI is configured in their own classes through @Autowired. It means the Services use @Autowired for their Repositories components and the Repositories use @Autowired for the JdbcTemplate, DataSource etc..components

    0 讨论(0)
  • 2020-11-22 05:35
    <context:component-scan /> implicitly enables <context:annotation-config/>
    

    try with <context:component-scan base-package="..." annotation-config="false"/> , in your configuration @Service, @Repository, @Component works fine, but @Autowired,@Resource and @Inject doesn't work.

    This means AutowiredAnnotationBeanPostProcessor will not be enabled and Spring container will not process the Autowiring annotations.

    0 讨论(0)
  • 2020-11-22 05:43
    <context:annotation-config/> <!-- is used to activate the annotation for beans -->
    <context:component-scan base-package="x.y.MyClass" /> <!-- is for the Spring IOC container to look for the beans in the base package. -->
    

    The other important point to note is that context:component-scan implicitly calls the context:annotation-config to activate the annotations on beans. Well if you don't want context:component-scan to implicitly activate annotations for you, you can go on setting the annotation-config element of the context:component-scan to false.

    To summarize:

    <context:annotation-config/> <!-- activates the annotations --> 
    <context:component-scan base-package="x.y.MyClass" /> <!-- activates the annotations + register the beans by looking inside the base-package -->
    
    0 讨论(0)
  • 2020-11-22 05:43

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

    This is used to tell the container that there are bean classes in my package scan those bean classes. In order to scan bean classes by container on top of the bean we have to write one of the stereo type annotation like following.

    @Component, @Service, @Repository, @Controller

    <context:annotation-config />:

    If we don't want to write bean tag explicitly in XML then how the container knows if there is a auto wiring in the bean. This is possible by using @Autowired annotation. we have to inform to the container that there is auto wiring in my bean by context:annotation-config.

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

    you can find more information in spring context schema file. following is in spring-context-4.3.xsd

    <conxtext:annotation-config />
    
    Activates various annotations to be detected in bean classes: Spring's @Required and
    @Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available),
    JAX-WS's @WebServiceRef (if available), EJB 3's @EJB (if available), and JPA's
    @PersistenceContext and @PersistenceUnit (if available). Alternatively, you may
    choose to activate the individual BeanPostProcessors for those annotations.
    
    Note: This tag does not activate processing of Spring's @Transactional or EJB 3's
    @TransactionAttribute annotation. Consider the use of the <tx:annotation-driven>
    tag for that purpose.
    
    <context:component-scan>
    
    Scans the classpath for annotated components that will be auto-registered as
    Spring beans. By default, the Spring-provided @Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice, and @Configuration stereotypes    will be detected.
    
    Note: This tag implies the effects of the 'annotation-config' tag, activating @Required,
    @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit
    annotations in the component classes, which is usually desired for autodetected components
    (without external configuration). Turn off the 'annotation-config' attribute to deactivate
    this default behavior, for example in order to use custom BeanPostProcessor definitions
    for handling those annotations.
    
    Note: You may use placeholders in package paths, but only resolved against system
    properties (analogous to resource paths). A component scan results in new bean definitions
    being registered; Spring's PropertySourcesPlaceholderConfigurer will apply to those bean
    definitions just like to regular bean definitions, but it won't apply to the component
    scan settings themselves.
    
    0 讨论(0)
  • 2020-11-22 05:44

    <context:annotation-config>:

    This tells Spring that I am going to use Annotated beans as spring bean and those would be wired through @Autowired annotation, instead of declaring in spring config xml file.

    <context:component-scan base-package="com.test..."> :

    This tells Spring container, where to start searching those annotated beans. Here spring will search all sub packages of the base package.

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