经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring使用Setter完成依赖注入方式
来源:jb51  时间:2021/9/28 9:54:58  对本文有异议

对依赖注入的理解

依赖:实体间的所有依赖由容器创建

注入:容器负责完成实体间依赖互相注入的任务

使用Setter完成不同类型属性的注入

实体类Student

  1. package indi.stitch.pojo;
  2. import java.util.*;
  3. public class Student {
  4. private String name;
  5. private Address address;
  6. private String[] books;
  7. private List<String> hobbys;
  8. private Set<String> games;
  9. private Map<String, String> card;
  10. private Properties info;
  11. private String wife;
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public Address getAddress() {
  19. return address;
  20. }
  21. public void setAddress(Address address) {
  22. this.address = address;
  23. }
  24. public String[] getBooks() {
  25. return books;
  26. }
  27. public void setBooks(String[] books) {
  28. this.books = books;
  29. }
  30. public List<String> getHobbys() {
  31. return hobbys;
  32. }
  33. public void setHobbys(List<String> hobbys) {
  34. this.hobbys = hobbys;
  35. }
  36. public Set<String> getGames() {
  37. return games;
  38. }
  39. public void setGames(Set<String> games) {
  40. this.games = games;
  41. }
  42. public Map<String, String> getCard() {
  43. return card;
  44. }
  45. public void setCard(Map<String, String> card) {
  46. this.card = card;
  47. }
  48. public String getWife() {
  49. return wife;
  50. }
  51. public void setWife(String wife) {
  52. this.wife = wife;
  53. }
  54. public Properties getInfo() {
  55. return info;
  56. }
  57. public void setInfo(Properties info) {
  58. this.info = info;
  59. }
  60. @Override
  61. public String toString() {
  62. return "Student{" +
  63. "name='" + name + '\'' + "\n" +
  64. ", address=" + address.toString() + "\n" +
  65. ", books=" + Arrays.toString(books) + "\n" +
  66. ", hobbys=" + hobbys + "\n" +
  67. ", games=" + games + "\n" +
  68. ", card=" + card + "\n" +
  69. ", info=" + info + "\n" +
  70. ", wife='" + wife + '\'' +
  71. '}';
  72. }
  73. }

实体类引用的复杂类型Address

  1. package indi.stitch.pojo;
  2. public class Address {
  3. private String address;
  4. public String getAddress() {
  5. return address;
  6. }
  7. public void setAddress(String address) {
  8. this.address = address;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Address{" +
  13. "address='" + address + '\'' +
  14. '}';
  15. }
  16. }

String字符串类型注入

  1. <property name="name" value = "stitch" />

复杂VO类型注入

配置文件中增加复杂类型bean(Address)的依赖配置

  1. <bean id = "address" class="indi.stitch.pojo.Address">
  2. <property name="address" value="北京" />
  3. </bean>
  4. <bean id = "student" class = "indi.stitch.pojo.Student">

实体类Student的bean属性依赖对其进行引用

  1. <property name="address" ref="address"/>

数组类型注入

  1. <property name="books">
  2. <array>
  3. <value>西游记</value>
  4. <value>三国演义</value>
  5. <value>红楼梦</value>
  6. <value>水浒传</value>
  7. </array>
  8. </property>

List集合类型注入

  1. <property name="hobbys">
  2. <list>
  3. <value>唱歌</value>
  4. <value>跳舞</value>
  5. <value>打篮球</value>
  6. </list>
  7. </property>

Set集合类型注入

  1. <property name="games">
  2. <set>
  3. <value>英雄联盟</value>
  4. <value>穿越火线</value>
  5. <value>刺激战场</value>
  6. </set>
  7. </property>

Map键值对类型注入

  1. <property name="card">
  2. <map>
  3. <entry key="学生卡" value="123456"/>
  4. <entry key="身份证" value="111111222222223333" />
  5. </map>
  6. </property>

Properties类型注入

  1. <property name="info">
  2. <props>
  3. <prop key="sex"></prop>
  4. <prop key="age">18</prop>
  5. </props>
  6. </property>

null类型注入

  1. <property name="wife">
  2. <null />
  3. </property>

整体配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id = "address" class="indi.stitch.pojo.Address">
  6. <property name="address" value="北京" />
  7. </bean>
  8. <bean id = "student" class = "indi.stitch.pojo.Student">
  9. <!-- String字符串类型注入-->
  10. <property name="name" value = "stitch" />
  11. <!--复杂VO类型注入-->
  12. <property name="address" ref="address"/>
  13. <!--数组类型注入-->
  14. <property name="books">
  15. <array>
  16. <value>西游记</value>
  17. <value>三国演义</value>
  18. <value>红楼梦</value>
  19. <value>水浒传</value>
  20. </array>
  21. </property>
  22. <!--List集合类型注入-->
  23. <property name="hobbys">
  24. <list>
  25. <value>唱歌</value>
  26. <value>跳舞</value>
  27. <value>打篮球</value>
  28. </list>
  29. </property>
  30. <!--Set集合类型注入-->
  31. <property name="games">
  32. <set>
  33. <value>英雄联盟</value>
  34. <value>穿越火线</value>
  35. <value>刺激战场</value>
  36. </set>
  37. </property>
  38. <!--Map键值对类型注入-->
  39. <property name="card">
  40. <map>
  41. <entry key="学生卡" value="123456"/>
  42. <entry key="身份证" value="111111222222223333" />
  43. </map>
  44. </property>
  45. <!--Properties类型注入-->
  46. <property name="info">
  47. <props>
  48. <prop key="sex"></prop>
  49. <prop key="age">18</prop>
  50. </props>
  51. </property>
  52. <!--null类型注入-->
  53. <property name="wife">
  54. <null />
  55. </property>
  56. </bean>
  57. </beans>

测试类

  1. import indi.stitch.pojo.Student;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  7. Student student = (Student) context.getBean("student");
  8. System.out.println(student.toString());
  9. }
  10. }

输出结果:

在这里插入图片描述

Spring解决setter方式的循环依赖的原理

1.通过构造函数创建A对象 (A对象是半成品,还没有注入属性和调用init方法)

2.将半成品A对象封装成工厂对象存入三级缓存

3.A对象需要注入B对象,发现缓存里还没有B对象,开始创建B对象

4.通过构造函数创建B对象(B对象是半成品,还没有注入属性和调用init方法)同样在三级缓存中创建B工厂对象

5.B对象需要注入A对象;从三级缓存中获取A工厂对象,使用工厂对象获取半成品A对象同时放入

二级缓存中,提前曝光A对象,同时删除A工厂对象

6.B对象继续注入其它属性和初始化,之后将完成品B对象放入完成品缓存一级缓存,同时删除B工厂对象

7.A对象获取单例B的引用完成属性注入

8.B对象继续注入其它属性和初始化,之后将完成品A对象放入完成品缓存一级缓存同时删除二级缓存中的A

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号