本例主要说明spring的集中注入方式:set注入(又名属性注入)、构造注入、静态工厂注入、非静态工厂注入、方法注入。
1、set注入
要求被注入的bean对象对应的属性要有setXxxx的方法如部门编码属性deptCode得有setDeptCode的方法
package com.hsk.spring.bean;
public class DeptBean {
private String deptCode;
private String deptName;
private String deptPcode;
private String cmpyCode;
public String getDeptCode() {
return deptCode;
}
public void setDeptCode(String deptCode) {
this.deptCode = deptCode;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptPcode() {
return deptPcode;
}
public void setDeptPcode(String deptPcode) {
this.deptPcode = deptPcode;
}
public String getCmpyCode() {
return cmpyCode;
}
public void setCmpyCode(String cmpyCode) {
this.cmpyCode = cmpyCode;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "set注入DeptBean:"+deptCode+"/"+deptPcode+"/"+cmpyCode+"/"+deptName;
}
}
xml中set注入的配置:
<!-- set注入 -->
<bean id="deptBean" class="com.hsk.spring.bean.DeptBean">
<property name="deptCode" value="24"></property>
<property name="deptName" value="北京有限公司"></property>
<property name="cmpyCode" value="2"></property>
<property name="deptPcode" value="21"></property>
</bean>
测试结果:
2、构造注入:
package com.hsk.spring.bean;
public class UserBean {
private String userCode;
private String userName;
private String deptCode;
private DeptBean deptBean;
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getDeptCode() {
return deptCode;
}
public void setDeptCode(String deptCode) {
this.deptCode = deptCode;
}
public DeptBean getDeptBean() {
return deptBean;
}
/**
* set注入
* @param deptBean
*/
public void setDeptBean(DeptBean deptBean) {
System.out.println("spring set注入deptBean");
this.deptBean = deptBean;
}
/**
* 构造方法注入
*/
public UserBean(DeptBean deptBean){
System.out.println("spring 构造方法注入deptBean"+deptBean);
}
public UserBean(String deptCode,String userCode,String userName){
System.out.println("spring 构造方法注入UserBean:"+deptCode+"/"+userCode+"/"+userName);
}
}
xml中构造注入配置:
<!-- 构造方法注入 -->
<bean id="userBean" class="com.hsk.spring.bean.UserBean">
<constructor-arg index="0" value="这是第一个构造方法传参"></constructor-arg>
<constructor-arg index="1" value="这是第二个构造方法传参"></constructor-arg>
<constructor-arg index="2" value="这是第三个构造方法传参"></constructor-arg>
<property name="deptBean" ref="deptBean"></property>
</bean>
运行结果:
3、静态工厂方法注入
package com.hsk.spring.bean;
public class UserBeanStaticFactory {
/**
* 静态工厂方法注入
* @return
*/
public static UserBean createUserBean(){
System.out.println("这是静态工厂方法注入");
return new UserBean("33", "44", "李四");
}
}
xml中配置:
<bean id="staticFactory" class="com.hsk.spring.bean.UserBeanStaticFactory"
factory-method="createUserBean">
</bean>
运行结果:
4、非静态方法注入
package com.hsk.spring.bean;
public class UserBeanFactory {
/**
* 非静态方法注入
*/
public UserBean createUserBean()
{
System.out.println("非静态方法注入");
return new UserBean("24","25","张三");
}
}
xml中配置:
<bean id="userBean" class="com.hsk.spring.bean.UserBean"
factory-bean="beanFactory" factory-method="createUserBean"></bean>
<bean id="beanFactory" class="com.hsk.spring.bean.UserBeanFactory"></bean>
运行结果:
5、方法注入(一般用于单例的bean依赖于多实例的bean对象的时候在单例的bean中注入)
package com.hsk.spring.bean;
public abstract class CardBean {
private String cardId;
private String cardAddr;
//这是一个多实例的依赖bean的注入方式
protected abstract UserBean createUserBean();
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getCardAddr() {
return cardAddr;
}
public void setCardAddr(String cardAddr) {
this.cardAddr = cardAddr;
}
}
xml中配置:
<!-- lookup-method方法注入 -->
<bean id="multiExampleDeptBean" class="com.hsk.spring.bean.DeptBean"></bean>
<bean id="cardBean" class="com.hsk.spring.bean.CardBean" scope="singleton">
<lookup-method name="createUserBean" bean="multiExampleDeptBean"/>
</bean>
运行结果: