二.DI
1.spring的依赖注入方式
有三种方式:
1.使用构造函数注入
2.使用set方法注入
3.使用注解注入
2.注入的数据类型
有三类:
1.基本类型和String类型
2.其他bean类型(必须是在spring的配置文件中出现过的bean)
3.复杂类型(集合类型)
eg
构造方法 + 基本类型注入
涉及的标签:constructor-arg
标签的属性:
type:指定参数的类型
index:指定参数的索引位置,从0开始
name:指定参数的名字(一般用它)
value:指定基本数据类型或String类型的数据
ref:指定其他bean数据类型
applicationContext.xml中配置
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<constructor-arg name="driver" value="com.mysql.jdbc.Driver"></constructor-arg>
<constructor-arg name="port" value="3306"></constructor-arg>
<constructor-arg name="today" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
public class CustomerServiceImpl implements CustomerService {
private String driver;
private Integer port;
private Date today;
public CustomerServiceImpl(String driver, Integer port, Date today) {
this.driver = driver;
this.port = port;
this.today = today;
}
@Override
public void saveCustomer() {
System.out.println(driver + " " + port + " " + today);
}
eg
set方法 + 基本类型注入
涉及的标签:property
标签的属性:
name:要设置的成员变量名称(前提有set 方法)
value:指定基本数据类型或String类型的数据
ref:指定其他bean数据类型
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="port" value="3306"></property>
<property name="today" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
public class CustomerServiceImpl implements CustomerService {
private String driver;
private Integer port;
private Date today;
public void setDriver(String driver) {
this.driver = driver;
}
public void setPort(Integer port) {
this.port = port;
}
public void setToday(Date today) {
this.today = today;
}
@Override
public void saveCustomer() {
System.out.println(driver + " " + port + " " + today);
}
}
eg
复杂类型注入
<bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
<property name="myStrs">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<property name="myList">
<list>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</list>
</property>
<property name="myset">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="testD" value="DDD"></entry>
<entry key="testE">
<value>EEE</value>
</entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="testF">FFF</prop>
<prop key="testG">GGG</prop>
</props>
</property>
</bean>
public class CustomerServiceImpl implements CustomerService {
private String[] myStrs;
private List<String> myList;
private Set<String> myset;
private Map<String, String> myMap;
private Properties myProps;
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 saveCustomer() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(myset);
System.out.println(myMap);
System.out.println(myProps);
}
}