Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

时人不识凌云木,直待凌云始道高。这篇文章主要讲述Spring:IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!相关的知识,希望能为你提供帮助。
一、构造器注入

  • 构造器注入其实在上一篇文章中讲得比较详细了,在这里就不再赘述,只举个最简单的例子来吧。
①实体类:
public class Person { private String name; private int age; private String like; private String high; public Person(String name, int age, String like, String high){ this.name = name; this.age = age; this.like = like; this.high = high; } //set、get、tostring方法因为篇幅原因省略,请手动加上! }

②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 name="person1,person2 person3; person4" class="entity.Person"> < constructor-arg index="0" value="https://www.songbingjia.com/android/丁大大1"/> < constructor-arg name="age" value="https://www.songbingjia.com/android/23"/> < constructor-arg type="java.lang.String" value="https://www.songbingjia.com/android/钓鱼1"/> < constructor-arg type="java.lang.String" value="https://www.songbingjia.com/android/173"/> < /bean> < /beans>

③测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Person person = Context.getBean("person4", Person.class); System.out.println(person); } }

④执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

⑤总结:
  • 无参构造器不用多说,无法注入值
  • 可以通过有参构造器来注入值
二、Set 注入(重点!!!!!)
  • 前提: 实体类:
public class Pojo { private String name; private Person person; private int[] intArr; private List< String> list; private Map< String,Object> map; private Set< String> set; private String like; private Properties info; } //set、get、tostring方法因为篇幅原因省略,请手动加上!

2.01、常量注入
①beans.xml:
< bean id="Pojo" name="testPojo" class="entity.Pojo"> < property name="name" value="https://www.songbingjia.com/android/丁大大"/> < /bean>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getName()); } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.02、Bean注入
①beans.xml:
  • 这里要注意property
  • value 是用来赋值常量的
  • ref 可以用来赋值bean
< !--先将实体bean的值通过构造器注入进去--> < bean name="testPerson" class="entity.Person"> < constructor-arg index="0" value="https://www.songbingjia.com/android/丁大大2"/> < constructor-arg name="age" value="https://www.songbingjia.com/android/23"/> < constructor-arg type="java.lang.String" value="https://www.songbingjia.com/android/钓鱼2"/> < constructor-arg type="java.lang.String" value="https://www.songbingjia.com/android/173"/> < /bean> < !--再将实体bean注入到Pojo中去--> < bean id="Pojo" name="testPojo" class="entity.Pojo"> < property name="name" value="https://www.songbingjia.com/android/丁大大"/> < property name="person" ref="testPerson"/> < /bean>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getName()); System.out.println(testPojo.getPerson()); } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.03、数组注入
①beans.xml:
  • 使用 array 来加值
< bean id="Pojo" name="testPojo" class="entity.Pojo"> < property name="intArr"> < array> < value> 7< /value> < value> 5< /value> < value> 9< /value> < /array> < /property> < /bean>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getName()); System.out.println(testPojo.getPerson()); int[] intArr = testPojo.getIntArr(); for (int i : intArr) { System.out.print(i+""); } } }

③执行结果:

Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.04、List注入
①beans.xml:
< bean id="Pojo" name="testPojo" class="entity.Pojo"> < property name="list"> < list> < value> 钓鱼< /value> < value> 捕鱼< /value> < value> 吃鱼< /value> < /list> < /property> < /bean>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); List< String> list = testPojo.getList(); for (String s : list) { System.out.print(s+""); } } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.05、Map注入
①beans.xml:
< property name="map"> < map> < entry key="name" value="https://www.songbingjia.com/android/丁大大"/> < entry key="age" value="https://www.songbingjia.com/android/23"/> < entry key="like" value="https://www.songbingjia.com/android/钓鱼"/> < /map> < /property>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); Map< String,Object> map = testPojo.getMap(); for (Object value : map.values()){ System.out.print(value+""); } } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.06、set注入
①beans.xml:
< property name="set"> < set> < value> 英雄联盟< /value> < value> 吃鸡< /value> < value> 王者荣耀< /value> < value> 原神< /value> < /set> < /property>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); Set< String> set = testPojo.getSet(); for (String s : set) { System.out.println(s); } } }

③执行结果:

Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.07、Null注入
①beans.xml:
< property name="like"> < null> < /null> < /property>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getLike()); } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.08、Properties注入
①beans.xml:
< property name="info"> < props> < prop key="userName"> 773530472< /prop> < prop key="passWord"> 123456< /prop> < prop key="验证码"> Tg3O< /prop> < /props> < /property>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getInfo().toString()); } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.09、p命名注入
①beans.xml:注意这里要在头文件引入外部约束
  • xmlns:p="www.springframework.org/schema/p"
  • P(属性: properties)命名空间 , 属性依然要设置set方法
  • 不能有有参构造函数
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

Spring(IOC之DI(依赖注入)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" 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="Person" class="entity.Person" p:name="丁大大" p:age="23"/> < /beans>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Person testPojo = Context.getBean("Person", Person.class); System.out.println(testPojo); } }

③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片

2.10、c命名注入
①beans.xml:需要在头文件中加入约束文件
  • xmlns:c="www.springframework.org/schema/c"
  • C(构造: Constructor)命名空间 , 属性依然要设置set方法
  • 构造器中有几个参数就得写几个c的参数,不然会报错!

    Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

    文章图片

Spring(IOC之DI(依赖注入)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" 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="Person" class="entity.Person" c:name="丁大大" c:age="23" c:like="哈哈哈" c:high="123"> < /bean> < /beans>

②测试类:
public class Test { public static void main(String[] args) { ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml"); Person testPojo = Context.getBean("Person", Person.class); System.out.println(testPojo); } }

【Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)】③执行结果:
Spring(IOC之DI(依赖注入)Set注入和构造器注入的区别和实现!)

文章图片


    推荐阅读