问题?Spring的DI讲解、DI有几种注入方式、
一、spring的DI:依赖注入给属性赋值
DI定义:一个对象类的属性可以使用springDI(依赖注入)来进行赋值,但是并不是所有的类属性都适合springDI来赋值.,
一共有两种方式进行属性赋值:set方法和带参构造方法
案例1:setting方法为对象类属性赋值
Person类
一、spring的DI:依赖注入给属性赋值
DI定义:一个对象类的属性可以使用springDI(依赖注入)来进行赋值,但是并不是所有的类属性都适合springDI来赋值.,
一共有两种方式进行属性赋值:set方法和带参构造方法
案例1:setting方法为对象类属性赋值
Person类
package cn.itcast.sh.spring.di; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Person { private Long pid; private String pname; private Student student; private List lists; private Map map; private Properties properties; private Set sets; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public List getLists() { return lists; } public void setLists(List lists) { this.lists = lists; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Set getSets() { return sets; } public void setSets(Set sets) { this.sets = sets; } }
Student类、
package cn.itcast.sh.spring.di; public class Student { }
配置文件:
<?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-2.5.xsd"> <!-- 先把person和student放入到spring容器中 --> <!-- SpringDI:依赖注入,本质就是向对象类的属性赋值 --> <!-- 用setting方式来为属性赋值 --> <bean id="person" class="cn.itcast.sh.spring.di.Person"> <!-- property是用来描述一个类的属性 基本类型的封装类、String等需要值的类型用value赋值 引用类型用ref赋值(一般是对象的注入) --> <property name="pid" value="2"></property> <property name="pname" value="lijun"></property> <property name="lists"> <list> <value>list1</value> <value>list2</value> <ref bean="student"/> </list> </property> <property name="map"> <map> <entry key="m1"> <value>map1</value> </entry> <entry key="map2"> <ref bean="student"/> </entry> </map> </property> <!-- properties:只有String类型 --> <property name="properties"> <props> <prop key="p1">p1的值</prop> <prop key="p2">p2的值</prop> </props> </property> <property name="sets"> <set> <value>set第一个值</value> <value>set第二值</value> <ref bean="student"/> </set> </property> <property name="student"> <ref bean="student"/> </property> </bean> <bean id="student" class="cn.itcast.sh.spring.di.Student"></bean> </beans>
测试类:
package cn.itcast.sh.spring.ioc.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.sh.spring.di.Person; import cn.itcast.sh.spring.di.Student; public class IDTest { @Test public void A(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person)applicationContext.getBean("person"); System.out.println(person.getPid()); System.err.println(person.getStudent()); System.out.println("--------------"); System.out.println(person.getLists()); System.out.println("--------------"); System.out.println(person.getMap()); System.out.println("--------------"); System.out.println(person.getProperties()); System.out.println("--------------"); System.out.println(person.getSets()); } }
结果图片处:
案例2:带参构造器为对象类属性赋值
Person类
package cn.itcast.sh.spring.di.constructor; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Person { private String pname; private Student student; private void person() {}//如果写的有带参构造方法,一定要写默认的构造方法 public Person(String pname,Student student){ this.pname = pname; this.student = student; } public String getPname() { return pname; } public Student getStudent() { return student; } }
配置文件:
<?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-2.5.xsd"> <!-- 先把person和student放入到spring容器中 --> <!-- SpringDI:依赖注入,本质就是向对象类的属性赋值 --> <!-- 用构造函数的方式为属性赋值( 如果没有constructor-arg:默认情况下是调用的无参构造函数, 如果有constructor-arg:则调用带参数的构造函数) 注:一个bean只能确认一个构造函数,因此在对象类中不能出现两个构造函数 --> <bean id="personConstructor" class="cn.itcast.sh.spring.di.constructor.Person"> <!-- index:表示构造函数参数的位置 type:表示参数的类型 value:表示给基本类型赋值 ref:表示给引用类型赋值 --> <constructor-arg index="0" type="java.lang.String" value="aaa"></constructor-arg> <constructor-arg index="1" type="cn.itcast.sh.spring.di.constructor.Student" ref="studentConstructor"></constructor-arg> </bean> <bean id="studentConstructor" class="cn.itcast.sh.spring.di.constructor.Student"></bean> </beans>
测试类:
package cn.itcast.sh.spring.ioc.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.sh.spring.di.constructor.Person; public class DIConstructorTest { @Test public void A(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Person person = (Person)applicationContext.getBean("personConstructor"); System.out.println(person.getPname()); person.getStudent().B(); } }
结果图片处:
案例写到这里;我们就可能要提一个问题了,那我们用这个方式来干甚呢?
用spring这种赋值方式,我们可以实现属性的赋值,还可以实现对象的注入,在我们进行对象的实现的时候,就不需要我们来做了,全部交给spring容器来做。
二、springIOC和DI的意义
意义:不完全面接口编程----->变成完全的接口编程(也就是将对象的产生给了sprin*生,我们不需要关心是谁来实现的接口)
案例:spring+MVC模式
action层
package cn.itcast.sh.spring.mvc; public class PersonAction { private PersonService personService; public PersonService getPersonService() { return personService; } public void setPersonService(PersonService personService) { this.personService = personService; } public void savePerson1(){ this.personService.savePerson(); } }
service接口层及实现
package cn.itcast.sh.spring.mvc; public interface PersonService { public void savePerson(); }
package cn.itcast.sh.spring.mvc; public class PersonServiceImp implements PersonService { private PersonDao personDao; public PersonDao getPersonDao() { return personDao; } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public void savePerson() { this.personDao.savePerson(); } }
Dao接口层及实现
package cn.itcast.sh.spring.mvc; public interface PersonDao { public void savePerson(); }
package cn.itcast.sh.spring.mvc; public class PersonDaoImp implements PersonDao { public void savePerson() { System.out.println("sava Person Dao!!"); } }
spring配置文件
<?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-2.5.xsd"> <!-- Spring模拟MVC --> <bean id="personDao" class="cn.itcast.sh.spring.mvc.PersonDaoImp"></bean> <!-- 这里采用的是setting方法进行注入的 --> <bean id="personService" class="cn.itcast.sh.spring.mvc.PersonServiceImp"> <property name="personDao"> <ref bean="personDao"/> </property> </bean> <bean id="personAction" class="cn.itcast.sh.spring.mvc.PersonAction"> <property name="personService"> <ref bean="personService"/> </property> </bean> </beans>
测试类:
package cn.itcast.sh.spring.ioc.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.sh.spring.mvc.PersonAction; import cn.itcast.sh.spring.util.SpringUtil; public class MVCTest { @Test public void A(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonAction personAction = (PersonAction)applicationContext.getBean("personAction"); personAction.savePerson1(); } }
结果图片处: