【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标签进行设值注入. 二.构造注入 通过类的构造方法的方式注入 ...
随机推荐
- MySQL key/value存储方案(转)
需求 250M entities, entities表共有2.5亿条记录,当然是分库的. 典型解决方案:RDBMS 问题:由于业务需要不定期更改表结构,但是在2.5亿记录的表上增删字段.修改索引需要锁 ...
- MFC的类层次结构图
- BIP_开发案例02_BI Publisher中复杂案例实现代码(案例)
2014-12-27 Created By BaoXinjian
- POJ 2318 TOYS(计算几何)
题目大意:有一个矩形盒子,盒子里会有一些木块线段,并且这些线段是按照顺序给出的,有n条线段,把盒子分层了n+1个区域,然后有m个玩具,这m个玩具的坐标是已知的,问最后每个区域有多少个玩具 解题思路:因 ...
- cocopods的安装和使用
- 牢骚 - 你代码写得丑,又不肯用好一点的IDE,这让我很为难啊。
又有人问我代码错误,发过来就是一篇巨丑无比的代码,先不说左大括号转行还和代码写在同一行的谭浩强风格,你这狗啃的一样的缩进是闹哪样!粘进VS2015里面,自动格式化,瞬间赏心悦目,编译错误出了5行,我直 ...
- stdlib
system(pause): int cmp(const void *a ,const void *b) { return *(int *)a - *(int *)b ; //从小到 ...
- R %operator% 含义
%foo% is the syntax for a binary operator. In base R: %in%: '"%in%" <- function(x, tabl ...
- ICE中间件说明文档
ICE中间件说明文档 1 ICE中间件简介 2 平台核心功能 2.1 接口描述语言(Slice) 2.2 ICE运行时 2.2.1 ...
- 无侵入方面编程-用HttpModule+SoapExtension监视页面执行参数(二)
上一篇文章 "无侵入方面编程-用HttpModule+SoapExtension监视页面执行参数(一)"中,我们实现了监视每个页面的执行情况和调用WebService的简单信息. ...