後端-框架-Spring-IoC-contructor-arg-構造注入
阿新 • • 發佈:2018-11-11
後端-框架-Spring-IoC-contructor-arg
public class UserServiceImpl implements UserService {
// 宣告介面型別的引用,和具體實現類解耦合
private UserDao dao;
private String s1;
private String s2;
private int i1;
public UserServiceImpl() {
}
public UserServiceImpl(UserDao dao) {
this.dao = dao;
}
public UserServiceImpl (UserDao dao, String s1) {
this.dao = dao;
this.s1 = s1;
}
public UserServiceImpl(UserDao dao, int i1) {
this.dao = dao;
this.i1 = i1;
}
public UserServiceImpl(String s2, UserDao dao) {
this.s2 = s2;
this.dao = dao;
}
}
<bean id="UserService" class="cn.service.UserServiceImpl" >
<constructor-arg>
<ref bean="UserDao"/>
</constructor-arg>
<!-- index為構造方法中第幾個引數,Re:0 -->
<!-- type為構造方法的引數型別 -->
<constructor-arg index="1" type="int">
<value>123</value>
</constructor-arg>
</bean>