在这篇文章中,我将介绍<context:annotation-config>和<context:component-scan>标签的区别,将来再使用它们的时候能够清楚的知道你在干什么
首先,<context:annotation-config>是用来激活已经在application context中注册的bean。注意,不管你使用那种注册的方式都是可以的,比如使用<context:component-scan>或者在application-context.xml中定义。
第二个区别是是由第一个差异导致的,它确实在context中注册beans,它也在beans内扫描注解并且激活他们,所以<context:component-scan>做了<context:annotation-config>要做的事情,<context:component-scan>还要扫描packages并且在application context中注册扫描的beans.
使用<context:annotation-config>和<context:component-scan>的例子:
我将以例子的方式更加直观的方式介绍这两个标签,为了保持例子的简洁性我只创建了3个beans,然后我将使用多种方式在配置文件中配置他们,然后我们可以在控制台上根据他们的输出结果看到他们的不同。
下边三个beans,BeanA引用了BeanBag和BeanC。
package com.howtodoinjava.beans; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @SuppressWarnings("unused") @Component public class BeanA { private BeanB beanB; private BeanC beanC; public BeanA(){ System.out.println("Creating bean BeanA"); } @Autowired public void setBeanB(BeanB beanB) { System.out.println("Setting bean reference for BeanB"); this.beanB = beanB; } @Autowired public void setBeanC(BeanC beanC) { System.out.println("Setting bean reference for BeanC"); this.beanC = beanC; } } //Bean B package com.howtodoinjava.beans; import org.springframework.stereotype.Component; @Component public class BeanB { public BeanB(){ System.out.println("Creating bean BeanB"); } } //Bean C package com.howtodoinjava.beans; import org.springframework.stereotype.Component; @Component public class BeanC { public BeanC(){ System.out.println("Creating bean BeanC"); } }
package com.howtodoinjava.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml"); } }
A:
<bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Creating bean BeanC
B:
<bean id="beanA" class="com.howtodoinjava.beans.BeanA"> <property name="beanB" ref="beanB"></property> <property name="beanC" ref="beanC"></property> </bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Creating bean BeanC Setting bean reference for BeanB Setting bean reference for BeanC
C:
仅仅使用<context:annotation-config>
<context:annotation-config /> //No Output
D:
使用<context:annotation-config>和bean的声明
<context:annotation-config /> <bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC
E:仅仅使用<context:component-scan>
<context:component-scan base-package="com.howtodoinjava.beans" /> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC
F:同时使用<context:component-scan>和<context:annotation-config>
<context:annotation-config /> <context:component-scan base-package="com.howtodoinjava.beans" /> <bean id="beanA" class="com.howtodoinjava.beans.BeanA"></bean> <bean id="beanB" class="com.howtodoinjava.beans.BeanB"></bean> <bean id="beanC" class="com.howtodoinjava.beans.BeanC"></bean> Output: Creating bean BeanA Creating bean BeanB Setting bean reference for BeanB Creating bean BeanC Setting bean reference for BeanC
现在我希望在你使用这些的时候已经给了你一个清晰的轮廓。
原文地址:http://howtodoinjava.com/2014/07/19/spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/