spring配置文件注入的多种方式
1.默认构造函数
package com.zho.service.impl;
import com.zho.service.IAccountService;
import java.util.*;
public class IAccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public IAccountServiceImpl() {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accoutnService" class="com.zho.service.impl.IAccountServiceImpl"/ >
</beans>
2.有参构造函数注入(注入的参数必须齐全,否则注入失败)
package com.zho.service.impl;
import com.zho.service.IAccountService;
import java.util.*;
public class IAccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public IAccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
}
#配置文件写法
<bean id="accountService" class="com.zho.service.impl.IAccountServiceImpl">
<constructor-arg name="age" value="12"/>
<constructor-arg name="name" value="梨花"/>
<constructor-arg name="birthday" ref="now"/>
</bean>
<bean