【Spring 2】spring的属性注入形式
一、注入简介
spring是一个java bean的容器,它摒弃了过去通过new关键字调用类再调用类的实例的形式,通过依赖注入维护者一系列的java bean的示例。通过spring所提供的依赖注入的方法,我们可以管理这些java bean之间的引用关系。
spring的注入形式,主要是有4种:setter方法、构造器方法、静态 / 实例工厂方法、注解,这里主要介绍属性注入的:setter方法和构造器方法。
二、实例解析
2.1,setter方法属性注入
首先,在applicationContext里面进行配置
<pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;"><?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userDao4Mysql" class="com.angel.spring.dao.UserDao4MySqlImpl"></bean>
<span> </span><bean id="userDao4Oracle" class="com.angel.spring.dao.UserDao4OracleImpl"></bean>
<span> </span><bean id="userManager" class="com.angel.spring.manager.UserManagerImpl">
<span> </span><property name="userDao" ref="userDao4Oracle" />
<span> </span></bean> </beans></span>
最后,测试类
<span style="font-family:KaiTi_GB2312;font-size:18px;"></span><span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.spring.manager;
import com.angel.spring.dao.UserDao;
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public void addUser(String username, String password) {
userDao.addUser(username, password);
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
} </span>
2.2,构造器方法属性注入
首先,在application中的配置
<pre name="code" class="html"><span style="font-family:KaiTi_GB2312;font-size:18px;"><?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
<span style="color:#ff0000;">default-autowire="byType"</span>> <bean id="bean2" class="com.angel.spring.Bean2"></bean> <bean id="beantest3" class="com.angel.spring.Bean3">
<constructor-arg index="0" value="1002" />
<constructor-arg index="1" value="Angel" />
<constructor-arg index="2" value="23" />
</bean>
</beans></span>
其次,在实体类中提供构造方法
<pre name="code" class="java"><span style="font-family:KaiTi_GB2312;font-size:18px;">public Bean3(int id,String name,String sex){
this.id=id;
this.name=name;
this.sex=sex;
}</span>
注意:spring 提供了Resource、Autowired这两个注解用于注入,另外在xml配置文件中,beans标签下有一个参数default-autowire用来设置默认的注入类型。这里我先说明一下default-autowire的用途。首先default-autowire参数有几个可选值:
default:实际上是会优先执行constructor然后是byType
byType:这个是会根据类型去注入,当找到了会直接注入,没有找到不会报错,但是找到多个会报No unique bean of type的错误
byName:这个会根据name注入
constructor:这个是通过构造注入
no:不启用自动注入
三、总结
以上就介绍了属性注入的两种形式:setter和构造器注入。spring同样还支持注解注入,但是今天例子没有做完,等做完了之后,再次进行总结!以前就知道个依赖注入,可是到底是怎么注入的,都没有认真了解过,学习还是需要踏踏实实的!
【Spring 2】spring的属性注入形式的更多相关文章
- spring练习,使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录。
相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,属性注入通过构造方法方式实现,模拟用户的正常登录.要求如下: 通过 ...
- Spring学习日记02_IOC_属性注入_其他类型属性
ICO操作Bean管理(xml注入其它类型属性) 字面量 null值 <property name="address"> <null></null&g ...
- Spring中集合类型属性注入
我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...
- Spring boot将配置属性注入到bean类中
一.@ConfigurationProperties注解的使用 看配置文件,我的是yaml格式的配置: // file application.yml my: servers: - dev.bar.c ...
- Spring boot将配置属性注入到bean 专题
https://blog.csdn.net/wangmx1993328/article/details/81002901 Error starting ApplicationContext. To d ...
- Spring中@value以及属性注入的学习
1.简单的Java配置 配置文件(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://1 ...
- Spring学习日记03_IOC_属性注入_集合类型属性
Ioc操作Bean管理(xml注入集合属性) 注入数组类型属性 注入List集合类型属性 注入Map集合类型属性 Stu类 public class Stu { //1. 数组类型属性 private ...
- 【坑】Spring中抽象父类属性注入,子类调用父类方法使用父类注入属性
运行环境 idea 2017.1.1 spring 3.2.9.RELEASE 需求背景 需要实现一个功能,该功能有2个场景A.B,大同小异 抽象一个抽象基类Base,实现了基本相同的方法BaseMe ...
- spring的bean的属性注入
一.设置注入 设置注入要求: 要求属性在实体类中必须有getter 和setter方法,然后在spring的工厂中就可以使用property标签进行设值注入. 二.构造注入 通过类的构造方法的方式注入 ...
随机推荐
- Linux进程内存分析pmap命令(转)
名称: pmap - report memory map of a process(查看进程的内存映像信息)用法 pmap [ -x | -d ] [ -q ] pids... ...
- Akka(二) - Future
1. future的所有方法都是非阻塞立即返回的 (1)future都要有TimeOut和ExecutionContextExecutor这2个隐士参数 (2)打印future object Hell ...
- 关于c语言中qsort函数的一点心得
今天写c时无意间用到了排序,便想着使用c语言标准库中提供的排序函数,即qsort函数(c++stl中提供了sort函数用于排序),首先是介绍qsort函数的一些基本用法(以下内容转自: http:// ...
- MyEclipse-File Serarch时报错:Problems encountered during text search
- smartgit document merge
'Normal' Merge In case of a normal merge, a merge commit with at least two parent commits (i.e., the ...
- HDU 2188 悼念512汶川大地震遇难同胞――选拔志愿者(巴什博奕)
选拔志愿者 题意: 对于四川同胞遭受的灾难,全国人民纷纷伸出援助之手,几乎每个省市都派出了大量的救援人员,这其中包括抢险救灾的武警部队,治疗和防疫的医护人员,以及进行心理疏导的心理学专家.根据要求,我 ...
- Json--Android中数据文件解析(Json解析--从服务器端获取数据并且解析,显示在客户端上面)
前面学习过了使用SAX解析XML数据(点击进入:SAX解析XML数据),今天学习Json解析: 首先说一下Json数据的最基本的特点,Json数据是一系列的键值对的集合,和XML数据来比,Json数据 ...
- 表空间、Schema和用户
源地址:http://www.cnblogs.com/kevinanni/p/3688921.html
- crm 4 隐藏自定义 toolbar
//隐藏指定title按钮 function hideISVButton(buttonTitle) { var comps = document.getElementsByTagName('li'); ...
- Entity Framework6 访问MySQL
先用PM命令安装EF6,MySQL提供的EF实现新增.删除.修改是采用存储过程实现的 Install-Package EntityFramework 配置修改如下 <?xml version=& ...