1、依赖注入
spring中的依赖注入(Dependency Injection)
1、IOC的作用:降低程序间的耦合(依赖关系)
2、依赖关系的管理:以后都交给spring来维护。
3、在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
4、依赖关系的维护:就称之为依赖注入。
5、依赖注入:
(1)能注入的数据:有三类
基本类型和String
其他的bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
(2)注入的方式有三种
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供(明天的内容)
2、构造函数注入
<?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="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="test"></constructor-arg>
<constructor-arg name="age" value="85"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
</beans>
public class AccountServiceImpl implements IAccountService{
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name,Integer age,Date birthday) {
this.age=age;
this.name=name;
this.birthday=birthday;
}
public void saveAccount() {
System.out.println("servlce中的saveAccount方法执行了"
+ "~~~\n"+birthday+","+name+","+age);
}
}
2、set方法注入
<?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="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
<property name="name" value="TEST"></property>
<property name="age" value="81"></property>
<property name="birthday" ref="now"></property>
</bean>
</beans>
public class AccountServiceImpl2 implements IAccountService{
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void saveAccount() {
System.out.println("servlce中的saveAccount方法执行了"
+ "~~~\n"+birthday+","+name+","+age);
}
}
3、复杂/集合类型的注入
<?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="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
<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="1" value="a"></entry>
<entry key="2"><value>bbb</value></entry>
<entry key="3" value="c"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="testc">ccc</prop>
<prop key="testd">ddd</prop>
</props>
</property>
</bean>
</beans>
public class AccountServiceImpl3 implements IAccountService{
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void saveAccount() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(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;
}
}