spring的bean对象的依赖注入
通过构造函数注入
在spring中,可以使用的子标签来通过对应的构造函数(constructor-arg标签中有几个属性,在对应的类对象中必须有对应个数参数的构造函数,否则无法成功创建对象)来注入依赖对象,并且可以没有set方法。
标签的属性:
type:指定要注入的参数在构造函数中的数据类型,如果有多个相同的数据类型,不能使用该属性。
index:指定要注入的参数在构造函数中的索引位置。
name:指定要注入的参数在构造函数中的名称
value:指定注入的数据内容,他只能指定基本数据类型和String类型
ref:指定其他bean类型数据,其值是其他bean的id;其他bean指的是存在于spring容器中的bean;并且ref引用的标签必须在当前引用的bean标签的下面。
<bean id="accountService" class="com.yuxiang.service.impl.AccountServiceImpl">
<constructor-arg name="accountDao" ref="accountDao"></constructor-arg>
<constructor-arg name="name" value="accountDao"></constructor-arg>
<constructor-arg name="age" value="accountDao"></constructor-arg>
<constructor-arg name="brithday" ref="date"></constructor-arg>
</bean>
<bean id="accountDao" class="com.yuxiang.dao.impl.AccountDaoImpl">
<bean id="date" class="java.util.Date"></bean>
/**
* 构造函数注入bean对象的例子
*/
public class AccountServiceImpl implements IAccountService{
private IAccountDao accountDao;
//必须有个该构造函数,否则无法注入
public AccountServiceImpl(IAccountDao accountDao) {
super();
this.accountDao = accountDao;
this.age = age;
this.name = name;
this.brithday = brithday;
}
@Override
public void saveAccount() {
System.out.println("创建对象成功"+name+", "+age+", "+brithday);
accountDao.saveAccount();
}
}
使用set方法注入(常用)
在spring中,可以使用的子标签来通过对应属性的set方法(使用property标签添加的每个属性都要有set方法)来注入依赖对象。
标签属性:
name:指定的是类中的成员变量的名称,匹配的是类中的各个成员的set方法,如果没有对应的set方法,注入失败。
value:指定注入的数据内容,他只能指定基本数据类型和String类型数据,
ref:指定其他bean类型数据,其值是其他bean的id;其他bean指的是存在于spring容器中的bean;并且ref引用的标签必须在当前引用的bean标签的下面。
使用set方法注入必须保证有property的name属性指定的名称对应的set方法和无参构造函数,否则都无法成功注入。
<bean id="accountService" class="com.yuxiang.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="name" value="张三"></property>
<property name="age" value="25"></property>
<property name="brithday" ref="date"></property>
</bean>
<bean id="accountDao" class="com.yuxiang.dao.impl.AccountDaoImpl"></bean>
<bean id="date" class="java.util.Date"></bean>
/**
* set方法注入数据的例子
*/
public class AccountServiceImpl implements IAccountService{
private int age;
private String name;
private Date brithday;
private IAccountDao accountDao;
/**
* 当类中没有有参构造函数时,
* 可以不明写此无参构造方法;
* 但是,当类中出现了有参构造方法,
* 必须明写无参构造函数,
* 否则注入创建对象失败
*/
public AccountServiceImpl() {
super();
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void saveAccount() {
System.out.println("创建对象成功"+name+", "+age+", "+brithday);
accountDao.saveAccount();
}
}
使用p名称空间注入
他的本质仍然是需要类中提供set方法和无参构造函数,同时还需要在配置文件中要导入p名称空间。
<!-- p名称空间 -->
xmlns:p="http://www.springframework.org/schema/p"
<bean id="accountService3" class="com.yuxiang.service.impl.AccountServiceImpl3"
p:name="张三"
p:age="25"
p:brithday-ref="date"
p:accountDao-ref="accountDao">
</bean>
<bean id="accountDao" class="com.yuxiang.dao.impl.AccountDaoImpl"></bean>
<bean id="date" class="java.util.Date"></bean>
/**
* p空间注入数据的例子
*/
public class AccountServiceImpl implements IAccountService{
private int age;
private String name;
private Date brithday;
private IAccountDao accountDao;
/**
* 当类中没有有参构造函数时,
* 可以不明写此无参构造方法;
* 但是,当类中出现了有参构造方法,
* 必须明写无参构造函数,
* 否则注入创建对象失败
*/
public AccountServiceImpl() {
super();
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void saveAccount() {
System.out.println("创建对象成功"+name+", "+age+", "+brithday);
accountDao.saveAccount();
}
}
spring中复杂类型数据的注入
在spring中当需要往类中注入集合类型数据时,通常情况下使用set方法注入。
<bean id="accountService" class="com.yuxiang.service.impl.AccountServiceImpl">
<!-- 给数组注入数据 -->
<property name="myStrs">
<array>
<value>111</value>
<value>222</value>
<value>333</value>
</array>
</property>
<!-- 给set注入数据 -->
<property name="mySet">
<set>
<value>111</value>
<value>222</value>
<value>333</value>
</set>
</property>
<!-- 给List注入数据 -->
<property name="myList">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
<!-- 给map注入数据 -->
<property name="myMap">
<map>
<entry key="A" value="a"></entry>
<entry key="B">
<value>b</value>
</entry>
</map>
</property>
<!-- 给Properties注入数据 -->
<property name="myProps">
<props>
<prop key="a">A</prop>
<prop key="b">B</prop>
</props>
</property>
</bean>
/**
* 复杂类型数据注入
*/
public class AccountServiceImpl implements IAccountService{
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
/**
* 当类中没有有参构造函数时,
* 可以不明写此无参构造方法;
* 但是,当类中出现了有参构造方法,
* 必须明写无参构造函数,
* 否则注入创建对象失败
*/
public AccountServiceImpl() {
super();
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public void saveAccount() {
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
System.out.println(Arrays.toString(myStrs));
}
}