Spring之注入类型(DI:dependency injection)
2017-09-04 16:49
441 查看
(一)Spring的注入类型
1、setter方法注入(常用,掌握)
2、构造方法注入(了解)
3、接口方法注入(了解)
(二)setter方法注入(通过查看Spring文档)
(二)使用构造方法注入
这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来:
Java代码
public class SpringAction {
//注入对象springDao
private SpringDao springDao;
private User user;
public SpringAction(SpringDao springDao,User user){
this.springDao = springDao;
this.user = user;
System.out.println("构造方法调用springDao和user");
}
public void save(){
user.setName("卡卡");
springDao.save(user);
}
}
在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性:
Xml代码
<!--配置bean,配置后该类由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置-->
<constructor-arg ref="springDao"></constructor-arg>
<constructor-arg ref="user"></constructor-arg>
</bean>
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
<bean name="user" class="com.bless.springdemo.vo.User"></bean>
解决构造方法参数的不确定性,你可能会遇到构造方法传入的两参数都是同类型的,为了分清哪个该赋对应值,通过设置index来区分。
下面是设置index,就是参数位置:
Xml代码
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<constructor-arg index="0" ref="springDao"></constructor-arg>
<constructor-arg index="1" ref="user"></constructor-arg>
</bean>
1、setter方法注入(常用,掌握)
2、构造方法注入(了解)
3、接口方法注入(了解)
(二)setter方法注入(通过查看Spring文档)
<bean id="exampleBean" class="examples.ExampleBean"> <!-- setter injection using the nested ref element --> <property name="beanOne"> <ref bean="anotherExampleBean"/> //其中<property name="beanOne">就是表示通过setBeanOne()方法注入 </property> <!-- setter injection using the neater ref attribute --> <property name="beanTwo" ref="yetAnotherBean"/> //这是简化写法 <property name="integerProperty" value="1"/> //简单属性传值,使用value(比较少用) </bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean { private AnotherBean beanOne; private YetAnotherBean beanTwo; private int i; //setter方法 public void setBeanOne(AnotherBean beanOne) { this.beanOne = beanOne; } //setter方法 public void setBeanTwo(YetAnotherBean beanTwo) { this.beanTwo = beanTwo; } public void setIntegerProperty(int i) { this.i = i; } }
(二)使用构造方法注入
这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来:
Java代码
public class SpringAction {
//注入对象springDao
private SpringDao springDao;
private User user;
public SpringAction(SpringDao springDao,User user){
this.springDao = springDao;
this.user = user;
System.out.println("构造方法调用springDao和user");
}
public void save(){
user.setName("卡卡");
springDao.save(user);
}
}
在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性:
Xml代码
<!--配置bean,配置后该类由spring管理-->
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置-->
<constructor-arg ref="springDao"></constructor-arg>
<constructor-arg ref="user"></constructor-arg>
</bean>
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>
<bean name="user" class="com.bless.springdemo.vo.User"></bean>
解决构造方法参数的不确定性,你可能会遇到构造方法传入的两参数都是同类型的,为了分清哪个该赋对应值,通过设置index来区分。
下面是设置index,就是参数位置:
Xml代码
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">
<constructor-arg index="0" ref="springDao"></constructor-arg>
<constructor-arg index="1" ref="user"></constructor-arg>
</bean>
相关文章推荐
- Spring框架xml配置文件 复杂类型属性注入——数组 list map properties DI dependency injection 依赖注入——属性值的注入依赖于建立的对象(堆空间)
- Spring Dependency Injection (DI)
- Spring的注入类型(Injection type of Spring)
- Spring MVC快速教程:依赖注入 Spring MVC Fast Tutorial: Dependency Injection
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
- Spring(2)-Spring Dependency Injection (DI)
- 【Spring】DI的各种类型变量注入方法
- Spring的DI(Ioc) - 注入集合类型
- Spring环境搭建之:控制反转(IoC Inversion of Control)与依赖注入(DI Depenency Injection)
- Spring Dependency Injection (DI)
- Spring环境搭建之:控制反转(IoC Inversion of Control)与依赖注入(DI Depenency Injection)
- Spring笔记之七(Types of Injection) Spring的三种依赖注入实现类型
- Spring3注入类型(Injection Type)
- Spring基于XML的DI之设值注入
- 如何理解Spring的控制反转IOC和依赖注入DI思想
- Spring--IoC--基于XML的DI-自动注入-byName
- Spring控制反转(IOC)和依赖注入(DI),再记不住就去出家!
- spring的依赖注入(DI)、控制反转(IOC)和面向切面(AOP)
- j2ee的spring应用中注入boolean类型的数据会报异常
- Spring2.5学习2.3_如何注入基本类型