8、引入依赖注入
8.1 创建工程
8.1.1 CustomerDao
package com
.zhc1024
.dao
;
public interface CustomerDao {
void save();
}
8.1.2 CustomerDaoImpl
package com
.zhc1024
.dao
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
public class CustomerDaoImpl implements CustomerDao {
public CustomerDaoImpl
(){
System
.out
.println("我是构造方法");
}
public void save() {
System
.out
.println("保存成功!");
}
}
8.1.3 CustomerService
package com
.zhc1024
.service
;
public interface CustomerService {
void save();
}
8.1.4 CustomerServiceImpl
package com
.zhc1024
.service
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
import com
.zhc1024
.dao
.impl
.CustomerDaoImpl
;
import com
.zhc1024
.service
.CustomerService
;
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao
= new CustomerDaoImpl();
public void save() {
customerDao
.save();
}
}
8.1.5 CustomerController
package com
.zhc1024
.controller
;
import com
.zhc1024
.service
.CustomerService
;
import org
.springframework
.context
.ApplicationContext
;
import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
public class CustomerController {
public static void main(String
[] args
) {
ApplicationContext ac
= new ClassPathXmlApplicationContext("bean.xml");
CustomerService service
= (CustomerService
) ac
.getBean("service");
service
.save();
}
}
8.1.6 运行结果
上面是正常的程序,下面我们根据Spring的思想:IOC帮我们实例对象。我们在Service实现类那里做一下修改,看看运行结果
8.1.7 修改CustomerServiceImpl
package com
.zhc1024
.service
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
import com
.zhc1024
.service
.CustomerService
;
public class CustomerServiceImpl implements CustomerService {
private CustomerDao customerDao
;
public void save() {
customerDao
.save();
}
}
运行结果
这是因为我们还有把对象放到IOC容器里面,也就是说还没有和Spring扯上关系,所以它自然不会帮我们创建和管理对象了,接下来我们就学习一下Spring的注入依赖
9、依赖注入(构造方法注入)
9.1 改造CustomerDaoImpl
package com
.zhc1024
.dao
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
import java
.util
.Date
;
public class CustomerDaoImpl implements CustomerDao {
private int id
;
private String name
;
private int age
;
private Date birthday
;
public CustomerDaoImpl(int id
, String name
,int age
, Date birthday
) {
this.id
= id
;
this.name
= name
;
this.age
=age
;
this.birthday
= birthday
;
}
public void save() {
System
.out
.println("id="+id
+",name="+name
+",age="+age
+",birthday="+birthday
);
}
}
9.2 改造bean.xml
<?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="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
<constructor-arg index="0" value="1"/>
<constructor-arg name="name" value="张三"/>
<constructor-arg name="age" value="18"/>
<constructor-arg name="birthday" ref="date"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
9.3 改造controller和运行结果
10、依赖注入(set方法注入)
10.1 改造CustomerDaoImpl
package com
.zhc1024
.dao
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
import java
.util
.Date
;
public class CustomerDaoImpl implements CustomerDao {
private int id
;
private String name
;
private int age
;
private Date birthday
;
public void setId(int id
) {
this.id
= id
;
}
public void setName(String name
) {
this.name
= name
;
}
public void setAge(int age
) {
this.age
= age
;
}
public void setBirthday(Date birthday
) {
this.birthday
= birthday
;
}
public CustomerDaoImpl(int id
, String name
, int age
, Date birthday
) {
this.id
= id
;
this.name
= name
;
this.age
=age
;
this.birthday
= birthday
;
}
public void save() {
System
.out
.println("id="+id
+",name="+name
+",age="+age
+",birthday="+birthday
);
}
}
10.2 修改bean.xml
<?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="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
<property name="id" value="1"/>
<property name="age" value="18"/>
<property name="birthday" ref="date"/>
<property name="name" value="李四"/>
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
10.3 运行结果
10.4 简化set注入方式
简化set注入方式可以使用p命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl"
p:id="1" p:age="18" p:birthday-ref="date" p:name="王五"
/>
<bean id="date" class="java.util.Date"/>
</beans>
10.5 运行结果
10.6 简化构造方法注入方式
修改bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl"
c:id="1" c:age="18" c:birthday-ref="date" c:name="赵六">
</bean>
<bean id="date" class="java.util.Date"/>
</beans>
修改CustomerDaoImpl
package com
.zhc1024
.dao
.impl
;
import com
.zhc1024
.dao
.CustomerDao
;
import java
.util
.Date
;
public class CustomerDaoImpl implements CustomerDao {
private int id
;
private String name
;
private int age
;
private Date birthday
;
public CustomerDaoImpl(int id
, String name
, int age
, Date birthday
) {
this.id
= id
;
this.name
= name
;
this.age
= age
;
this.birthday
= birthday
;
}
public void save() {
System
.out
.println("id="+id
+",name="+name
+",age="+age
+",birthday="+birthday
);
}
}
10.7运行结果
11、两种依赖注入的区别
set注入:spring在实例化一个对象的时候先把该对象实例化,然后再找依赖于这个对象的其他对象实例化;
构造函数注入:在实例化一个对象的时候先把其他依赖于该对象的对象实例化,再把该对象实例化
12、集合属性依赖注入
12.1 修改CustomerDaoImpl
private String
[] array
;
private List
<String> list
;
private Set
<String> set
;
private Map
<String,String> map
;
private Properties prop
;
public void setArray(String
[] array
) {
this.array
= array
;
}
public void setList(List
<String> list
) {
this.list
= list
;
}
public void setSet(Set
<String> set
) {
this.set
= set
;
}
public void setMap(Map
<String, String> map
) {
this.map
= map
;
}
public void setProp(Properties prop
) {
this.prop
= prop
;
}
public void saveCustomer() {
System
.out
.println(array
!=null
? Arrays
.asList(array
):"");
System
.out
.println(list
);
System
.out
.println(set
);
System
.out
.println(map
);
System
.out
.println(prop
);
}
12.2 修改bean.xml
<?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="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
<property name="array">
<array>
<value>array1
</value>
<value>array2
</value>
</array>
</property>
<property name="list">
<list>
<value>list1
</value>
<value>list2
</value>
</list>
</property>
<property name="set">
<set>
<value>set1
</value>
<value>set2
</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="1"/>
<entry key="2" value="2"/>
</map>
</property>
<property name="prop">
<props>
<prop key="p1">p1
</prop>
<prop key="p2">p2
</prop>
</props>
</property>
</bean>
</beans>
12.3 运行结果