spring setter方式注入:

注入对象属性

前提:

在bean对应实体中有对应的setter方法。

基础代码:

在bean中有另一个bean属性的setter方法。

package cn.itcast.dao.impl;

import cn.itcast.dao.PersonDao;

public class PersonDaoBean implements PersonDao {

    public void add(){
System.out.println("执行PersonDaoBean中的add()方法");
}
} package cn.itcast.service.impl; import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService; public class PersonServiceBean implements PersonService { private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} public PersonDao getPersonDao() {
return personDao;
} public void save(){
personDao.add();
} }

ref方式注入:

<bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
name:注入的属性
ref:值所对应的bean的id

内部类方式::

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="personDao">
<bean class="cn.itcast.dao.impl.PersonDaoBean"/>
</property>
</bean>

两种方式的比较:

  前者的bean可以被其他bean使用,后者的内部bean不能被其他bean使用

基本数据类型的注入

<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
<property name="name" value="itcast"/>
<property name="id" value=""/>
</bean>

集合属性的注入

准备工作:

    public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private Set<String> sets=new HashSet<String>();
private List<String> lists=new ArrayList<String>();
private Properties properties=new Properties();
private Map<String,String> maps=new HashMap<String,String>();
。。。
}
 <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一</value>
<value>第二</value>
<value>第三</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key1" value="value1"/>
<entry key="key2" value="value2"/>
<entry key="key3" value="value3"/>
</map>
</property>
</bean>

构造器注入

public PersonServiceBean(PersonDao personDao,String name){
this.personDao=personDao;
this.name=name;
} <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean"></bean>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<constructor-arg index="" type="cn.itcast.dao.PersonDao" ref="personDao"/>
<constructor-arg index="" value="....."/>
</bean>

注意:如果写了type,type对应的不是bean对象对应的实际type而是你的构造器中的参数的实际type。

field注入:(注解注入)

准备工作:

spring使用注解要先加入声明:

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd

启动注解配置:
<context:annotation-config/>

例子:

<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/>

这个配置隐式注入了多个对注释进行解析处理的处理器。

注入注解:

@Autowired(required=""):按类型装配
required属性类型为boolean,默认值为false,如果写为true的话,装配失败会报异常,如果为false,装配失败会装配null进去

@Resource(name=""):
name属性给了值的话直接查找对应的bean进行注入,如果name没有给值,先将name当成字段名在beans中查找,找不到再按类型查找对应的类型的bean进行注入

两个注解都可以写在属性上,也可以写在属性的setter方法上

@Autowired private PersonDao personDao;

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

按名称装配还可以这样写:
@AutoWried(required=true) @Qualifier(value="xxx")
前面学习注解的时候说过:注解中如果变量名为value的话,使用的时候可以省略掉value=

自动装配
了解即可,不建议使用,因为结果具有不可预知性。
例子:
<bean id="..." class="..." autowire="byType"/>
autowire属性取值如下:
byType对应上面的@AutoWried
byName对应上面的@Resource
constructor与byType类似,应用于构造参数,如果容器中没有找到匹配的bean,会抛出异常
autodetect:通过bean类的自省(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。

注意一点:
如果装配找到多个匹配的bean,也会抛出异常。

spring属性注入DI的更多相关文章

  1. Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用

    Spring 属性注入(一)JavaBean 内省机制在 BeanWrapper 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/101174 ...

  2. Spring 属性注入(二)BeanWrapper 结构

    Spring 属性注入(二)BeanWrapper 结构 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) BeanWrap ...

  3. Spring 属性注入(三)AbstractNestablePropertyAccessor

    Spring 属性注入(三)AbstractNestablePropertyAccessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117 ...

  4. Spring 属性注入(四)属性键值对 - PropertyValue

    Spring 属性注入(四)属性键值对 - PropertyValue Spring 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) P ...

  5. Spring属性注入、构造方法注入、工厂注入以及注入参数(转)

    Spring 是一个开源框架. Spring 为简化企业级应用开发而生(对比EJB2.0来说). 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.Spring ...

  6. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  7. spring 属性注入

    Spring的核心技术室依赖注入,下面是依赖注入之属性注入的实现过程,牛刀小试,请看效果. 1.首先添加Spring.Web引用.本例中是使用分层思想来演示的,下面是项目的结构和UserModel类的 ...

  8. spring属性注入

    1,set方法注入 (1)对于值类型的属性: 在对象中一定要有set方法 package com.songyan.demo1; import com.songyan.injection.Car; /* ...

  9. Spring 依赖注入(DI) 的三种方式 和 对集合类型的注入

    // 分别省略了getter setter public class Student { private String name; private int age; private Teacher t ...

随机推荐

  1. 翻翻git之---一个丰富的通知工具类 NotifyUtil

    转载请注明出处王亟亟的大牛之路 P1(废话板块.今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了. .简直心酸. ... 近期手头上有一些职位能够操作,然后 ...

  2. 身份证号码 javascript 验证

    function checkIsIdno(idcard) { var Errors=new Array( "SUCCESS", "身份证号码位数不对!", &q ...

  3. nfs部署和优化 -2

    客户端: cat /etc/passwd 显示用户 weifeng 500   服务端: vim /etc/exports /mnt 192.168.1.105(rw,sync,all_squash, ...

  4. ASP.NET MVC深入浅出系列(持续更新) ORM系列之Entity FrameWork详解(持续更新) 第十六节:语法总结(3)(C#6.0和C#7.0新语法) 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字 各种通讯连接方式 设计模式篇 第十二节: 总结Quartz.Net几种部署模式(IIS、Exe、服务部署【借

    ASP.NET MVC深入浅出系列(持续更新)   一. ASP.NET体系 从事.Net开发以来,最先接触的Web开发框架是Asp.Net WebForm,该框架高度封装,为了隐藏Http的无状态模 ...

  5. HTTP状态码中301与302的区别

    301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有链接编辑功能的客户端应当自动把请求的地址修改 ...

  6. 近期公共祖先(LCA)——离线Tarjan算法+并查集优化

    一. 离线Tarjan算法 LCA问题(lowest common ancestors):在一个有根树T中.两个节点和 e&sig=3136f1d5fcf75709d9ac882bd8cfe0 ...

  7. 【转】安卓apk反编译、修改、重新打包、签名全过程

    首先明确,反编译别人apk是一件不厚道的事情.代码是程序员辛苦工作的成果,想通过这种手段不劳而获,是不对的.这也说明,代码混淆是非常重要的.本文抱着学习的态度,研究在一些特殊的情况下如果有需要,该怎么 ...

  8. XP,32/64位Win7,32/64位Win10系统【电脑城版】

    本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...

  9. 【Java并发编程实战】—–“J.U.C”:ReentrantLock之二lock方法分析

    前一篇博客简介了ReentrantLock的定义和与synchronized的差别,以下尾随LZ的笔记来扒扒ReentrantLock的lock方法.我们知道ReentrantLock有公平锁.非公平 ...

  10. Java知识点梳理——常用方法总结

    1.查找字符串最后一次出现的位置 String str = "my name is zzw"; int lastIndex = str.lastIndexOf("zzw& ...