浅谈Spring03—依赖注入

mac2024-01-26  30

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 { //我不new了,按照Spring的思想是容器给我提供对象,我只是声明而已 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"> <!-- constructor-arg标签的属性及其作用: index:成员变量在构造方法参数列表中的索引 name:成员变量的属性名称 type:成员变量的类型 value:给java简单类型的成员变量赋值(八种简单类型和String类型) ref:引用 其他bean类型的值 --> <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl"> <constructor-arg index="0" value="1"/><!--可以使用下表表示--> <constructor-arg name="name" value="张三"/><!--也可以使用name-value的形式--> <constructor-arg name="age" value="18"/> <constructor-arg name="birthday" ref="date"/><!--ref表示引用--> </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"> <!-- set注入: property:指定通过set方法赋值 value:给java简单类型的成员变量赋值(八种简单类型和String类型) ref:引用 其他bean类型的值 --> <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"> <!-- 导入p名称空间 xmlns:p="http://www.springframework.org/schema/p" p:属性名="属性值" --> <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"> <!-- 第一步:导入c名称空间 xmlns:c="http://www.springframework.org/schema/c" 第二步: c:属性名称 ==>给java简单类型成员变量赋值 c:属性名称-ref ==>给其他bean类型成员变量赋值 --> <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; //===========================================set方法 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> <!--list集合赋值--> <property name="list"> <list> <value>list1</value> <value>list2</value> </list> </property> <!--set集合赋值--> <property name="set"> <set> <value>set1</value> <value>set2</value> </set> </property> <!--map集合赋值--> <property name="map"> <map> <entry key="1" value="1"/> <entry key="2" value="2"/> </map> </property> <!--prop--> <property name="prop"> <props> <prop key="p1">p1</prop> <prop key="p2">p2</prop> </props> </property> </bean> </beans>

12.3 运行结果

最新回复(0)