Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
来源:互联网 发布:手机淘宝登入卖家 编辑:程序博客网 时间:2024/05/16 14:04
在这篇文章中,我将介绍<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")@Componentpublic 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; @Componentpublic class BeanB { public BeanB(){ System.out.println("Creating bean BeanB"); }} //Bean C package com.howtodoinjava.beans; import org.springframework.stereotype.Component; @Componentpublic class BeanC { public BeanC(){ System.out.println("Creating bean BeanC"); }}
BeanDemo这个类被用来加载和初始化这个application context
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"); }}
现在让我们开始用不同的方式写配置文件bean.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 BeanACreating bean BeanBCreating bean BeanC
在这个例子中,所有的3个beans都被创建了,但是BeanA中的依赖没有被注入,因为我们没有使用任何property/ref 属性
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 BeanACreating bean BeanBCreating bean BeanCSetting bean reference for BeanBSetting bean reference for BeanC
现在毫无疑问,所有的bean都被注入了
C:
仅仅使用<context:annotation-config>
<context:annotation-config /> //No Output
像我刚才已经说到的,<context:annotation-config>仅仅激活那些已经发现和注册了的beans,这里我们没有发现任何bean所以什么也不会发生
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 BeanACreating bean BeanBSetting bean reference for BeanBCreating bean BeanCSetting bean reference for BeanC
在上边的配置中,阀门通过bean标签来发现beans,现在我们使用<context:annotation-config>仅仅激活@Atuowired注解,BeanA中bean的注入发生。
E:仅仅使用<context:component-scan>
<context:component-scan base-package="com.howtodoinjava.beans" /> Output: Creating bean BeanACreating bean BeanBSetting bean reference for BeanBCreating bean BeanCSetting bean reference for BeanC
上边的代码实现了我在前边提到的两种事情,第一:发现bean(在包中查找@Component标签),第二激活额外的标签(例如@Atuowired)
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 BeanACreating bean BeanBSetting bean reference for BeanBCreating bean BeanCSetting bean reference for BeanC
奇怪的是,使用上边的配置我们发现和激活注解都是两次,但是输出的结果只有一次,这是因为spring很聪明的值注册了一次bean。
现在我希望在你使用这些的时候已经给了你一个清晰的轮廓。
原文地址:http://howtodoinjava.com/2014/07/19/spring-mvc-difference-between-contextannotation-config-vs-contextcomponent-scan/
- Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
- Spring <context:annotation-config>和<context:component-scan>区别
- Spring <context:annotation-config> 和 <context:component-scan>区别
- Spring <context:annotation-config/> 和 <context:component-scan>
- <context:annotation-config/>和context:component-scan的区别
- <context:annotation-config> 和 <context:component-scan>的区别
- <context:annotation-config> 和 <context:component-scan>的区别
- context:component-scan和context:annotation-config的区别
- <context:annotation-config> 和 <context:component-scan>的区别
- <context:annotation-config> 和 <context:component-scan>的区别
- <context:annotation-config>和<context:component-scan>
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config>和 <context:component-scan>诠释及区别
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别
- Mesos&PaaS系列文章之五——负载均衡与服务发现
- 【软考】---软件开发方法
- Activity启动模式
- android 屏幕适配
- 重磅教程!帮你全面彻底搞定Material design的学习笔记
- Spring MVC: <context:annotation-config> 和 <context:component-scan>区别
- openSession和getCurrentSession之间的区别
- 读《C专家编程》笔记-第三章 分析C语言的声明
- #ifndef/#define/#endif使用和交叉引用问题详解
- Win7怎样添加环境变量
- XCode快捷键
- android自定义的toast样式
- HDOJ 题目4000 Fruit Ninja(树状数组)
- 斐波那契数列的算法实现 —— python