您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > JavaWebSpring依赖注入深入学习
JavaWebSpring依赖注入深入学习一、依赖注入(DI)依赖注入听起来很高深的样子,其实白话就是:给属性赋值。一共有两种方法,第一是以构造器参数的形式,另外一种就是以setting方法的形式。1构造器注入1使用构造器注入使用xml的注入方式A.通过参数的顺序constructor-argindex=0value张三/value/constructor-argconstructor-argindex=1value56/value/constructor-argB.通过参数的类型constructor-argtype=java.lang.Integervalue56/value/constructor-argconstructor-argtype=java.lang.Stringvalue张三/value/constructor-arg具体实例假如现在要对一个Person类注入参数,Student是一个另外一个类。12345678910111213141516publicclassPerson{privateStringpid;privateStringname;privateStudentstudent;publicPerson(Stringpid,Studentstudent){this.pid=pid;this.student=student;}publicPerson(Stringpid,Stringname){this.pid=pid;this.name=name;}}配置applicationContext.xml,假如不进行参数配置,则报错,找不到相应的构造器。配置了相应的参数,则应在类中声明相应的构造函数。1234567?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:schemaLocation=://=personclass=com.itheima10.spring.di.xml.constructor.Person!--8910111213141516171819202122不配参数,将会采取默认的构造器constructor-argperson类中某一个构造器的某一个参数index为参数的角标type参数的类型value如果为基础属性,则用这个赋值ref引用类型赋值--constructor-argindex=0type=java.lang.Stringvalue=aaa/constructor-argconstructor-argindex=1ref=student/constructor-arg/beanbeanid=person1class=com.itheima10.spring.di.xml.constructor.Personpropertyname=pidvalue=1/property/beanbeanid=studentclass=com.itheima10.spring.di.xml.constructor.Student/bean/beans编写测试类DIXMLConstructorTest,进行断点调试,将会发现根据配置的参数,进入的构造函数是Person(Stringpid,Studentstudent)12345678publicclassDIXMLConstructorTest{@Testpublicvoidtest1(){ApplicationContextcontext=newClassPathXmlApplicationContext(applicationContext.xml);Personperson=(Person)context.getBean(person);}}2使用属性setter方法进行注入使用xml的注入方式:A.简单Bean的注入简单Bean包括两种类型:包装类型和String12345beanid=personServiceclass=com.itcast.bean.impl.PersonServiceImpl!--基本类型,string类型--propertyname=agevalue=20/propertypropertyname=namevalue=张无忌/property/beanB.引用其他Bean?1234beanid=personclass=com.itcast.bean.Person/beanid=personServiceclass=com.itcast.bean.impl.PersonServiceImplpropertyname=personref=person//bean1.1装配list集合1propertyname=lists234567listvaluelist1/valuevaluelist2/valuerefbean=person//list/property1.2装配set集合1234567propertyname=setssetvaluelist1/valuevaluelist2/valuerefbean=person//set/property1.3装配map12345678910propertyname=mapsmapentrykey=01valuemap01/value/entryentrykey=02valuemap02/value/entry/map/propertymap中的entry的数值和list以及set的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的。1.4装配Properties123456propertyname=propspropspropkey=01prop1/proppropkey=02prop2/prop/props/property具体实例1.创建两个对象Person和Student123456packagexgp.spring.demo;importjava.util.List;importjava.util.Map;importjava.util.Properties;importjava.util.Set;publicclassPerson{78910111213141516171819202122privateStringpid;privateStringname;privateStudentstudent;privateListlists;privateSetsets;privateMapmap;privatePropertiesproperties;privateObject[]objects;publicPerson(){System.out.println(newperson);}//省略getter和setter方法}1234567891011packagexgp.spring.demo;publicclassStudent{publicStudent(){System.out.println(newstudent);}publicvoidsay(){System.out.println(student);}}配置applicationContext.xml文件123456789101112131415?xmlversion=1.0encoding=UTF-8?beansxmlns=:xsi=:schemaLocation=://!--把person和student放入到spring容器中property用来描述Person类的属性value如果是一般属性,则用value赋值ref如果该属性是引用类型,用ref赋值--beanid=personclass=com.itheima10.spring.di.xml.setter.Personinit-method=initlazy-init=truepropertyname=pidvalue=1/property1617181920212223242526272829303132333435363738394041424344454647484950515253545556575859propertyname=namevalue=王二麻子/propertypropertyname=studentref=student/propertypropertyname=listslistvaluelist1/valuevaluelist2/valuerefbean=student//list/propertypropertyname=setssetvalueset1/valuevalueset2/valuerefbean=student//set/propertypropertyname=mapmapentrykey=entry1valuemap1/value/entryentrykey=entry2refbean=student//entry/map/propertypropertyname=propertiesprops!--不需要引用类型--propkey=prop1prop1/proppropkey=prop2prop2/prop/props/propertypropertyname=objectslistvalueaa/valuevaluebb/value/list/property/beanbeanid=studentclass=com.itheima10.spring.di.xml.setter.Student/bean/beans编写测试类DIXMLSetterTest12345678910111213141516171819202122232425262728293031323334353637packagexgp.spring.test;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.{/
本文标题:JavaWebSpring依赖注入深入学习
链接地址:https://www.777doc.com/doc-2880477 .html