Spring、使用注解方式装配对象(@Resource、@Autowired)
使用手工注解方式有两种方式@Resource、@Autowired
首先,引入注解所使用的Jar包 :common-annotations.jar
然后在beans.xml中加入命名空间空间
xmlns:context="http://www.springframework.org/schema/context"
然后打开使用注解的开关(在beans节点下的第一个节点处加入<context:annotation-config /> );
</pre></p><p><pre name="code" class="html"><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="personIDao" class="cn.dao.impl.PersonDaoImpl" />
<bean id="personIService" class="cn.server.impl.PersonServiceImpl">
</bean>
</beans>
@Resource
在程序中使用@Resource ,@Resource可以加在字段上,如:
@Resource private PersonIDao personIDao;
@Resource 也可以加在 字段的Set方法上,如:
</pre><pre name="code" class="java">private PersonIDao personIDao; @Resource
public void setPersonIDao(PersonIDao personIDao) {
this.personIDao = personIDao;
}
public PersonIDao getPersonIDao() {
return personIDao;
}
@Resource 首先会根据字段的名称到beans.xml中去寻找相匹配的配置,如果找不到再根据字段的类型到beans.xml中找类型相匹配的配置;
还可以为@Resources指定特定的名字如下:
@Resource(name="personIDao")
private PersonIDao personIDao;
还可以为@Resources指定类型,如下:
@Resource(type=PersonIDao.class)
private PersonIDao personIDao;
以上几种 方式都可以完成对字段或者字段的set方法进行注入;
@Autowired
默认情况下@Autowired是按类型进行注入:
<pre name="code" class="java"> @Autowired
private PersonIDao personIDao;
但 @Autowired同样也可以按名称注入:
<pre name="code" class="java"> @Autowired @Qualifier("personIDaoxx")
private PersonIDao personIDao;
下面说明一下为什么推荐使用 @Resorce进行注解:
1、@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上。
2、@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:
- @Autowired() @Qualifier("baseDao")
- private BaseDao baseDao;
3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定,
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
- @Resource(name="baseDao")
- private BaseDao baseDao;
我喜欢用 @Resource注解在字段上,且这个注解是属于J2EE的,减少了与spring的耦合。最重要的这样代码看起就比较优雅。
Spring、使用注解方式装配对象(@Resource、@Autowired)的更多相关文章
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- IoC容器-Bean管理注解方式(注入属性@Autowired和Qualifier)
基于注解方式实现属性注入 (1)@Autowired:根据属性类型进行自动装配 第一步 把 service 和 dao 对象创建,在service 和 dao 类添加创建对象注解 第二步 在servi ...
- Spring基于注解自动装配
前面我们介绍Spring IoC装载的时候,使用XML配置这种方法来装配Bean,这种方法可以很直观的看到每个Bean的依赖,但缺点也很明显:写起来非常繁琐,每增加一个组件,就必须把新的Bean配置到 ...
- Spring的注解方式
在Java代码中可以使用@Resource或者@Autowired注解方式来经行注入.虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区别的. a.@Resource默 ...
- spring通过注解方式依赖注入原理 (私有成员属性如何注入)
一.spring如何创建依赖的对象 用过spring的都知道我们在dao.service层加上@repository.@Service就能将这两个对象交给spring管理,在下次使用的时候使用@res ...
- spring 纯注解方式 与AOP
spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...
- Spring使用注解方式注入多例的方式
目前Spring+Netty的开发方式这么火热,想把Netty注册成Spring组件就一定得用多例的方式,我不由得想吐槽明明这么常见的需求网上相关博客都少的很,这里给出Spring使用注解注入多例的方 ...
随机推荐
- Learn LIBSVM---a practical Guide to SVM classification
想学习一下SVM,所以找到了LIBSVM--A Library for Support Vector Machines,首先阅读了一下网站提供的A practical guide to SVM cla ...
- Easyui tabs学习
前端时间花了一些时间学习easy ui,这个东西非常好用,界面也很美观,你都不需要在界面上花太多的工夫,例子程序也比较完善,基本上看下例子就能很好的使用easyui了,很方便. 特地分享一些使用时候遇 ...
- Ruby小例子
1.ruby定义函数与执行函数案例 def fact(n) ) end end print fact() 结果: 24 2.一个小例子 words = [)] print "guess?\n ...
- 【hihocoder1255 Mysterious Antiques in Sackler Museum】构造 枚举
2015北京区域赛现场赛第2题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
- UVA 10131 Is Bigger Smarter?(DP)
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to t ...
- 每个人应该知道的NVelocity用法
NVelocity是一个基于.NET的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由.NET代码定义的对象.从而使得界面设 ...
- ng-validate
客户端表单验证是AngularJS里面最酷的功能之一. AngularJS表单验证可以让你从一开始就写出一个具有交互性和可相应的现代HTML5表单. 在AngularJS中,有许多表单验证指令.在这里 ...
- Linux挂载硬盘出错:$LogFile indicates unclean shutdown (0, 0)
前一次还挂载好好的,今天在挂载NTFS的分区就不行了,出现如下错误信息和提示: $LogFile indicates unclean shutdown (0, 0) Mount is denied b ...
- JavaScript中的this引用
在JavaScript的学习当中,this关键字的出现频率可不低,所以想想有必要对this关键字做一个总结.在总结过程中,参考的资料来源于书本及网上. 一.定义 1.this是函数内部的一个特殊对象( ...
- 深搜基础题目 杭电 HDU 1241
HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...