Spring学习——DI依赖注入

ゝ一世哀愁。 2022-05-14 13:26 413阅读 0赞

Spring DI(Dependency Injection,依赖注入)GitHub demo源码

  1. 是指程序运行过程中,如果需要调用另一个对象协助时,**无须在代码中创建被调用者,而是依赖于外部的注入**。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理。

1、基于构造函数的依赖注入

java 类

  1. package com.spring.DI.example;
  2. public class AConstructor {
  3. private B b;
  4. public AConstructor(B b){
  5. System.out.println("AConstructor 构造函数");
  6. this.b = b;
  7. }
  8. public void aConstructorSpeack(){
  9. b.bSpeack();
  10. }
  11. }

Beans1.xml配置文件 使用指定标签声明

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean id="AConstructor" class="com.spring.DI.example.AConstructor">
  6. <!-- 构造函数依赖注入 -->
  7. <constructor-arg ref="B"/>
  8. </bean>
  9. <bean id="B" class="com.spring.DI.example.B">
  10. </bean>
  11. </beans>

2、基于get/set函数的依赖注入

java类:

  1. package com.spring.DI.example;
  2. public class AFunction {
  3. private B b;
  4. public void setB(B b){
  5. System.out.println("AFunction 设置B");
  6. this.b = b;
  7. }
  8. public B getB(){
  9. return b;
  10. }
  11. public void aFunctionSpeack(){
  12. b.bSpeack();
  13. }
  14. }

Beans2.xml:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean id="AFunction" class="com.spring.DI.example.AFunction">
  6. <!-- get/set函数依赖注入 -->
  7. <property name="b" ref="B"/>
  8. </bean>
  9. <bean id="B" class="com.spring.DI.example.B">
  10. </bean>
  11. </beans>

3、基于注入内部bean的依赖注入

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean id="AFunction" class="com.spring.DI.example.AFunction">
  6. <!-- 内部beans依赖注入 -->
  7. <property name="b">
  8. <bean id="B" class="com.spring.DI.example.B"/>
  9. </property>
  10. </bean>
  11. </beans>

4、java集合依赖注入

java类

  1. package com.spring.DI.example;
  2. import java.util.*;
  3. public class JavaCollection {
  4. private List list;
  5. private Set set;
  6. private Map map;
  7. private Properties properties;
  8. public List getList() {
  9. return list;
  10. }
  11. public void setList(List list) {
  12. this.list = list;
  13. }
  14. public Set getSet() {
  15. return set;
  16. }
  17. public void setSet(Set set) {
  18. this.set = set;
  19. }
  20. public Map getMap() {
  21. return map;
  22. }
  23. public void setMap(Map map) {
  24. this.map = map;
  25. }
  26. public Properties getProperties() {
  27. return properties;
  28. }
  29. public void setProperties(Properties properties) {
  30. this.properties = properties;
  31. }
  32. }

Beans4.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean id="JavaCollection" class="com.spring.DI.example.JavaCollection">
  6. <!-- list依赖注入 -->
  7. <property name="list">
  8. <list>
  9. <value>list1</value>
  10. <value>list2</value>
  11. <value>list3</value>
  12. <value>list4</value>
  13. </list>
  14. </property>
  15. <!-- set依赖注入 -->
  16. <property name="set">
  17. <set>
  18. <value>set1</value>
  19. <value>set2</value>
  20. <value>set3</value>
  21. <value>set4</value>
  22. </set>
  23. </property>
  24. <!-- map依赖注入 -->
  25. <property name="map">
  26. <map>
  27. <entry key="1" value="map1"/>
  28. <entry key="2" value="map2"/>
  29. <entry key="3" value="map3"/>
  30. <entry key="4" value="map4"/>
  31. </map>
  32. </property>
  33. <!-- property依赖注入 -->
  34. <property name="properties">
  35. <props>
  36. <prop key="one">prop1</prop>
  37. <prop key="two">prop2</prop>
  38. <prop key="three">prop3</prop>
  39. <prop key="four">prop4</prop>
  40. </props>
  41. </property>
  42. </bean>
  43. </beans>

主程序:SpringMain

  1. package com.spring.DI.example;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class SpringMain {
  5. public static void main(String[] arg){
  6. System.out.println("-------读取beans1文件---------");
  7. ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans1.xml");
  8. System.out.println("----构造函数依赖注入------");
  9. AConstructor ac =(AConstructor) context1.getBean("AConstructor");
  10. ac.aConstructorSpeack();
  11. System.out.println("-------读取beans2文件---------");
  12. ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans2.xml");
  13. System.out.println("----get/set依赖注入------");
  14. AFunction af = (AFunction) context2.getBean("AFunction");
  15. af.aFunctionSpeack();
  16. System.out.println("-------读取beans3文件---------");
  17. ApplicationContext context3 = new ClassPathXmlApplicationContext("Beans3.xml");
  18. System.out.println("----get/set依赖注入------");
  19. AFunction af1 = (AFunction) context3.getBean("AFunction");
  20. af.aFunctionSpeack();
  21. System.out.println("-------读取beans4文件---------");
  22. ApplicationContext context4 = new ClassPathXmlApplicationContext("Beans4.xml");
  23. System.out.println("----集合依赖注入------");
  24. JavaCollection js = (JavaCollection) context4.getBean("JavaCollection");
  25. System.out.println("list"+js.getList());
  26. System.out.println("map"+js.getMap());
  27. System.out.println("set"+js.getSet());
  28. System.out.println("properties"+js.getProperties());
  29. }
  30. }

发表评论

表情:
评论列表 (有 0 条评论,413人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring---DI依赖注入

    上一篇介绍了关于IOC的内容:IOC称为控制反转,简单来说就是讲对象的创建的权利以及对象的生命周期的管理过程交给Spring容器来管理,从此在开发的过程中不需要关注对象的创建以

    相关 Spring依赖注入DI

    开头语:本文是我整理网上各种资料后整合而成的一篇文章。文中涉及到了代码重构、面向接口编程、持久化和工厂设计模式的内容。 1. Spring是什么?