http://blog.csdn.net/yerenyuan_pku/article/details/52858878

前面我们讲过spring的依赖注入有两种方式:

  1. 使用构造器注入。
  2. 使用属性setter方法注入。

但其实还有一种注入方式,我们没讲,那就是使用Field注入(用于注解方式)。在详细讲解这种方式之前,我们还须知道以下的知识点。 
注入依赖对象可以采用手工装配自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果。 
手工装配依赖对象,在这种方式中又有两种编程方式:

  1. 在xml配置文件中,通过在bean节点下配置,如:

    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
    <constructor-arg index="0" type="java.lang.String" value="xxx" /> // 构造器注入
    <property name="name" value="zhao" /> // 属性setter方法注入
    </bean>
  2. 在java代码中使用@Autowired或@Resource注解方式进行装配。但我们需要在xml配置文件中配置以下信息:

    <?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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:annotation-config/> </beans>
    <context:annotation-config/>
    • 1
    • 1

    这个配置隐式注册了多个对注解进行解析处理的处理器:

    • AutowiredAnnotationBeanPostProcessor
    • CommonAnnotationBeanPostProcessor
    • PersistenceAnnotationBeanPostProcessor
    • RequiredAnnotationBeanPostProcessor

接下来我们就来讲解用@Resource注解如何完成属性注入。首先新建一个普通的Java Project,名称为spring_anno,并迅速搭建好spring的开发环境。 
接着在src目录下新建一个cn.itcast.dao包,并在该包下创建PersonDao接口,其代码为:

public interface PersonDao {

    void add();

}

紧接着在src目录下新建一个cn.itcast.dao.impl包,并在该包下创建PersonDao接口的实现类——PersonDaoBean.java,其代码为:

public class PersonDaoBean implements PersonDao {
@Override
public void add() {
System.out.println("执行PersonDaoBean中的add()方法");
}
}

接下来在src目录下新建一个cn.itcast.service包,并在该包下创建PersonService接口,其代码为:

public interface PersonService {

    void save();

}

紧接着仍在src目录下新建一个cn.itcast.service.impl包,并在该包下创建PersonService接口的实现类——PersonServiceBean.java,其代码为:

public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name; public PersonServiceBean() {} public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
System.out.println(name);
personDao.add();
}
}

在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。在本文中我们将关注点着重放在@Resource注解上。 
@Resource注解和@Autowired一样,同样也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上时,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上时,即默认取属性名作为bean名称寻找依赖对象。如:

@Resource(name="personDaoBean")
private PersonDao personDao; // 用于字段上 @Resource
public void setPersonDao(PersonDao personDao) { // 用于属性的setter方法上
this.personDao = personDao;
}

注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。 
我们首先将@Resource注解用在字段上,修改PersonServiceBean类的代码如下:

public class PersonServiceBean implements PersonService {
@Resource private PersonDao personDao;
private String name; public PersonServiceBean() {} public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
// System.out.println(name);
personDao.add();
}
}

注意:当我们使用注解方式注入属性时,一定要往项目中导入spring-aop-4.2.5.RELEASE.jar包,不然就会报异常。 
接下来,我们就要将Spring的配置文件——beans的内容置为:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:annotation-config/> <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
</beans>

最后,在src目录下新建一个junit.test包,并在该包下新建一个单元测试类——SpringTest.java,其代码为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
ctx.close();
} }

测试instanceSpring()方法,可发现Eclipse控制台打印: 

若我们将Spring的配置文件中的

<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

改为:

<bean id="personDaoxxxx" class="cn.itcast.dao.impl.PersonDaoBean"></bean>

再次测试instanceSpring()方法,仍可发现Eclipse控制台打印: 

并没有异常发生。 
我们也可通过@Resource的name属性指定名称,告诉@Resource按名称注入,如将PersonServiceBean类中的字段

@Resource private PersonDao personDao;

修改为:

@Resource(name="personDaoxxxx") private PersonDao personDao;

紧接着测试instanceSpring()方法,仍可发现Eclipse控制台打印: 

当然了,我们也可以将@Resource注解用在属性的setter方法上,如将PersonServiceBean类的代码修改为:

public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name; @Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public PersonServiceBean() {} public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
// System.out.println(name);
personDao.add();
}
}

测试instanceSpring()方法,仍可发现Eclipse控制台打印: 

如要查看源码,可点击用@Resource注解完成属性装配进行下载。

(转)用@Resource注解完成属性装配的更多相关文章

  1. @Resource注解完成自动装配

    @Resource注解是通过名字来自动装配的.在spring中自动装配的模式如果是通过名字来自动装配那么必须保证bean的名字和pojo 的属性名一直. 下面是详细代码:说明了@Resource注解是 ...

  2. (转)编码剖析@Resource注解的实现原理

    http://blog.csdn.net/yerenyuan_pku/article/details/52860046 上文我们已经学会使用@Resource注解注入属性.学是学会了,但也仅限于会使用 ...

  3. (转)@Autowire注解与自动装配

    http://blog.csdn.net/yerenyuan_pku/article/details/52860713 前面我们已经学会使用@Resource注解注入属性,并且我们还编码剖析了@Res ...

  4. @Autowired注解与@resource注解的区别(十分详细)

    背景: 今天下班路上看到一个大货车,于是想到了装配,然后脑海里跳出了一个注解@Autowired(自动装配),于是又想到最近工作项目用的都是@Resource注解来进行装配.于是本着学什么东西都要一钻 ...

  5. 用@resource注解方式完成属性装配

    注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果. 1 需要修改xml文件的以下信息.    加入下列红色部分的4行 & ...

  6. @Resource注解省略name属性后的行为

    @Resource有一个name属性,该属性值为所要注入的Bean实例的id,类似于<property.../>元素的ref属性,不过在spring中允许省略name属性值,省略后在以下情 ...

  7. Spring5:@Autowired注解、@Resource注解和@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  8. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

  9. Spring中的Autowired注解和Resource注解的区别

    1.所属jar包不同,Autowired是Spring中的Resource是JSR-250规范定义的注解

随机推荐

  1. 【系列】 2-SAT

    bzoj 1997 Planar 题目大意: 给一个存在曼哈顿回路的无向图,求该图是否为平面图 思路: 先把曼哈顿回路提出来,则剩下的边的两个端点若有$ABAB$的形式则这两条边必定一个在环外一个在环 ...

  2. 【USACO】 Balanced Photo

    [题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; int i,N,ans,l1,l2; ] ...

  3. 【前端】CentOS 7 系列教程之二: 安装 git 最新版

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_2.html 这一篇我们来安装git高版本. 卸载yum安装的旧版本 yum remove git 安装 ...

  4. 【转】cocos2dx 内存管理机制

    原文地址: http://www.zaojiahua.com/memory-management.html cocos2dx采用的是在堆上分配内存空间,想想看你在写程序的时候对于cocos2dx中的类 ...

  5. CMake命令之list

    用途:提供一些列表操作 list(LENGTH <list><output variable>)  list(GET <list> <elementindex ...

  6. 网络爬虫之requests模块的使用+Github自动登入认证

    本篇博客将带领大家梳理爬虫中的requests模块,并结合Github的自动登入验证具体讲解requests模块的参数. 一.引入:   我们先来看如下的例子,初步体验下requests模块的使用: ...

  7. OC静态代码检查实战

    此文已由作者杨晓授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在Mac OS系统上,采用Xcodebuild Analyze命令和OClint工具,对iOS项目进行静态代码 ...

  8. [原]Windows下openssl的下载安装和使用

    安装openssl有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.下面对两种方式均进行详细描述. 一.下载和安装openss 方法一:直接使用openssl安装包 W ...

  9. PJzhang:工作之余一起来看剧

    猫宁!!! 最近几个月陆续看了4部电视剧,不错,分别来自4个国家或地区,英国.日本.中国香港.俄罗斯,是的,没有美剧,平时真的很少看美剧. 1-IT狂人,英剧,一共4季,每季6集,每集25分钟左右. ...

  10. Raphael.js改变元素层叠顺序

    Raphael.js 元素(Element)改变层叠顺序,Raphael.js是一个矢量绘图库兼容svg和vml.初学时感觉css的z-index能搞定,结果是不支持,不过矢量绘图符合dom标准.可以 ...