手动配置Spring的bean是一件极为头疼的事情,工作量大不说,还特别容易写错,Annotation标签很好的解决了这个问题,会帮我们剩下很多时间,接下来就是配置的方法:
第一步:添加common-annotations.jar包;
第二步:引入对应的命名空间及对应的Schema文件地址:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
第三步:在配置文件中添加<context:annotation-config /> 让Spring启用对annotation的支持。它其实是注册了多个对annotation进行解析处理的处理器:
- AutowiredAnnotionBeanPostProcessor
- CommonAnnotationBeanPostProcessor
- PersistenceAnnotionBeanPostProcessor
- RequiredAnnotationBeanPostProcessor.
第四步:在配置文件中添加需要进行扫描的包路径,例如:<context:component-scan base-package="com.mossad" />就是扫描com.mossad包及其子包下所有的bean,最后配置文件就像这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 扫描标记了注解的类 -->
<context:annotation-config />
<context:component-scan base-package="com.mossad" />
</beans>
大功告成!接下来就可以使用各种标签进行各种配置了!
记一下@Resource标签和@Autowired标签的区别和用法:
1、@Resource和@Autowired都可以用来标记bean的field属性、成员方法、构造函数、形参的,标记field属性的目的是为field注入相应的bean。
2、@Resource标签默认是byName自动注入,也就是说它会自动注入与field名称相同的bean
@Resource
private StockService stockService;
也可以通过设置name属性来指定注入bean的名称,如下所示,此时注入的bean就是abcService而不是stockService了
@Resource("abcService")
private StockService stockService;
它还有一个属性是type,用来指定bean的类型,如果不设置,那么Spring会通过反射获取field的类型,然后查找相同类型的bean进行注入,如果多余一个则会抛出异常。
3、@Autowired标签默认是byType自动注入,但是它也可以通过另外一个@Qualifier标签指定bean的name
@Autowired
@Qualifier("stockService")
private StockService stockService;
4、@Resource位于javax.annotation包下,是来自Java EE规范的一个Annotation,Spring直接借鉴了该Annotation来为目标Bean指定协作者Bean;@Autowired是Spring自己的标签,来提供自动装配。
5、设置了这两个标签之后,就都不需要setter方法了。
分享到:
相关推荐
Spring的Annotation配置相关讲义
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
spring annotation 注解 maven 配套的源码
spring 使用annotation替代xml配置实例
spring的annotation-driven配置事务管理器详解 (多数据源配置
spring的annotation的autowired配置方法。
配置整合DWR3.0和Spring2.5使用annotation注解
struts2.1 spring2.5 ibatis2.3 dwr3 annotation配置 集成 此中例子不完整. 如要下载:http://download.csdn.net/source/2138885
spring3、 hibernate4 配置声明式事务管理(annotation方式)
Spring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于注释(Annotation)的配置.pdfSpring基于...
struts2+spring2.5+hibernate3.2 annotation配置完整eclipse项目,带数据库脚本
spring ,hibernate ,flex 应用注释配置的一个例子,里面有数据库脚本和说明文件
Spring事务配置的例子。有多种方法,有传统的XML,还有Annotation等等
spring3,hibernate4 配置声明式事务管理(annotation方式)
spring2.5 ibatis2.3 struts2.1 dwr3 annotation集成配置
NULL 博文链接:https://baobeituping.iteye.com/blog/1201798
SpringMVC-annotation配置配置配置配置配置配置配置配置配置
maven & spring & myBatis 的整合使用
springmvc + hibernate annotation 配置