• 基于Xml方式Bean的配置-Bean的依赖注入以及·自动装配


    Bean的依赖注入配置

    • Bean的依赖注入方式

      • 注入方式配置方式
        通过Bean的set方法注入
        
        
        通过构造Bean的方法进行注入
        
        
        • 其中,ref是reference的缩写形式,翻译为:涉及,参考的意思,用于引用其它Bean的id,value用于指定属性值

    • 注入数据类型

      • 普通数据类型:String、int、boolean,通过value属性指定

      • 引用数据类型:UserDAOImpl,通过ref属性指定

      • 集合数据类型:List、Map、Properties等

        • 普通数据类型的集合

          • 配置文件

              1. <bean name="UserService" class="com.example.Service.Impl.UserServiceImpl">
              2. <property name="stringList">
              3. <list>
              4. <value>aaaavalue>
              5. <value>bbbbvalue>
              6. <value>ccccvalue>
              7. list>
              8. property>
              9. bean>
        • 引用数据类型的集合

          • 配置文件(两种方式都可以)

            1. <property name="DAOList">
            2. <list>
            3. <ref bean="DAO1">ref>
            4. <ref bean="DAO2">ref>
            5. <ref bean="DAO3">ref>
            6. <ref bean="DAO4">ref>
            7. list>
            8. property>
            9. bean>
            10. <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
            11. <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
            12. <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
            13. <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>

            1. <property name="DAOList">
            2. <list>
            3. <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
            4. <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
            5. <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
            6. <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>
            7. list>
            8. property>
        • bean的对应类

            1. package com.example.Service.Impl;
            2. import com.example.DAO.UserDAO;
            3. import com.example.Service.UserService;
            4. import java.util.List;
            5. public class UserServiceImpl implements UserService {
            6. private List stringList;
            7. private List DAOList;
            8. public void setStringList(List stringList) {
            9. this.stringList = stringList;
            10. }
            11. public void setDAOList(List daoList) {
            12. this.DAOList = daoList;
            13. }
            14. public void showStringList() {
            15. System.out.println(stringList);
            16. System.out.println(DAOList);
            17. }
            18. }
        • 测试代码

            1. package com.example.Test;
            2. import com.example.Service.Impl.UserServiceImpl;
            3. import org.springframework.context.support.ClassPathXmlApplicationContext;
            4. public class TestApplicationContext {
            5. public static void main(String[] args) {
            6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
            7. UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService");
            8. userServiceImpl.showStringList();
            9. }
            10. }
        • 运行结果

    以上介绍的是List集合

    下列直接展示Set、Map、Properties

    • 配置文件
        1. "1.0" encoding="UTF-8"?>
        2. <beans xmlns="http://www.springframework.org/schema/beans"
        3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
        4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
        5. <bean name="UserService" class="com.example.Service.Impl.UserServiceImpl">
        6. <property name="stringList">
        7. <list>
        8. <value>aaaavalue>
        9. <value>bbbbvalue>
        10. <value>ccccvalue>
        11. list>
        12. property>
        13. <property name="DAOList">
        14. <list>
        15. <ref bean="DAO1">ref>
        16. <ref bean="DAO2">ref>
        17. <ref bean="DAO3">ref>
        18. <ref bean="DAO4">ref>
        19. list>
        20. property>
        21. <property name="StringSet">
        22. <set>
        23. <value>aaaavalue>
        24. <value>bbbbvalue>
        25. <value>ccccvalue>
        26. set>
        27. property>
        28. <property name="DAOSet">
        29. <set>
        30. <ref bean="DAO1"/>
        31. <ref bean="DAO2"/>
        32. <ref bean="DAO3"/>
        33. <ref bean="DAO4"/>
        34. set>
        35. property>
        36. <property name="StringMap">
        37. <map>
        38. <entry key="StringMap1" value="aaaa">entry>
        39. <entry key="StringMap2" value="bbbb">entry>
        40. <entry key="StringMap3" value="cccc">entry>
        41. map>
        42. property>
        43. <property name="DAOMap">
        44. <map>
        45. <entry key="DAOMap1" value-ref="DAO1">entry>
        46. <entry key="DAOMap1" value-ref="DAO2">entry>
        47. <entry key="DAOMap1" value-ref="DAO3">entry>
        48. <entry key="DAOMap1" value-ref="DAO4">entry>
        49. map>
        50. property>
        51. bean>
        52. <bean name="DAO1" class="com.example.DAO.Impl.UserDAOImpl">bean>
        53. <bean name="DAO2" class="com.example.DAO.Impl.UserDAOImpl">bean>
        54. <bean name="DAO3" class="com.example.DAO.Impl.UserDAOImpl">bean>
        55. <bean name="DAO4" class="com.example.DAO.Impl.UserDAOImpl">bean>
        56. beans>
    • Bean对应的类

        1. package com.example.Service.Impl;
        2. import com.example.DAO.UserDAO;
        3. import com.example.Service.UserService;
        4. import java.util.List;
        5. import java.util.Map;
        6. import java.util.Set;
        7. public class UserServiceImpl implements UserService {
        8. // todo 无参构造方法
        9. // public UserServiceImpl() {
        10. // System.out.println("无参构造方法");
        11. // }
        12. // todo 有参构造方法
        13. // public UserServiceImpl(String name) {
        14. // System.out.println("有参构造方法");
        15. // System.out.println(name);
        16. // }
        17. // private UserDAO userDAO;
        18. //
        19. // public void setUserDAO(UserDAO userDAO) {
        20. // }
        21. private List stringList;
        22. private List DAOList;
        23. private Set StringSet;
        24. private Set DAOSet;
        25. private Map StringMap;
        26. private Map DAOMap;
        27. public void setStringList(List stringList) {
        28. this.stringList = stringList;
        29. }
        30. public void setStringSet(Set stringSet) {
        31. this.StringSet = stringSet;
        32. }
        33. public void setDAOList(List daoList) {
        34. this.DAOList = daoList;
        35. }
        36. public void show() {
        37. System.out.println("StringList:" + stringList);
        38. System.out.println("DAOList:" + DAOList);
        39. System.out.println("StringSet:" + StringSet);
        40. System.out.println("DAOSet:" + DAOSet);
        41. System.out.println("StringMap:" + StringMap);
        42. System.out.println("DAOMap:" + DAOMap);
        43. }
        44. public void setDAOSet(Set daoSet) {
        45. this.DAOSet = daoSet;
        46. }
        47. public void setStringMap(Map stringMap) {
        48. this.StringMap = stringMap;
        49. }
        50. public void setDAOMap(Map daoMap) {
        51. this.DAOMap = daoMap;
        52. }
        53. }
    • 测试类

        1. package com.example.Test;
        2. import com.example.Service.Impl.UserServiceImpl;
        3. import org.springframework.context.support.ClassPathXmlApplicationContext;
        4. public class TestApplicationContext {
        5. public static void main(String[] args) {
        6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
        7. UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserService");
        8. userServiceImpl.show();
        9. }
        10. }
    • 运行结果

     上述Bean的值都需要自己手动注入,下列展示自动注入

    • Bean的自动装配

      • 如果被注入的属性类型是Bean引用的话,那么可以在标签中使用autowire属性去装配自动注入方式,属性值有两个
        • byName:通过属性名自动装配,即去匹配SetXxx与id=“xxx”(name=“xxx”)是否一致;
        • byType:通过Bean的类型从容器中匹配,匹配出多个相同的Bean类型时会报错
        • <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType"/>
      • 通过byNmae
      • 配置文件

          1. <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byName"/>
          2. <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
      • Bean对应的类

        • 在该类总注入UserDAO(代码中体现在SetXxx()方法中)

          1. package com.example.Service.Impl;
          2. import com.example.DAO.UserDAO;
          3. import com.example.Service.UserService;
          4. import java.util.List;
          5. import java.util.Map;
          6. import java.util.Set;
          7. public class UserServiceImpl implements UserService {
          8. private UserDAO userDAO;
          9. public void setUserDAO(UserDAO userDAO) {
          10. this.userDAO = userDAO;
          11. }
          12. public void show() {
          13. System.out.println(userDAO);
          14. }
          15. }
      • 测试类代码

          1. package com.example.Test;
          2. import com.example.Service.Impl.UserServiceImpl;
          3. import org.springframework.context.support.ClassPathXmlApplicationContext;
          4. public class TestApplicationContext {
          5. public static void main(String[] args) {
          6. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
          7. UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userService");
          8. userServiceImpl.show();
          9. Object userDAO = context.getBean("userDAO");
          10. System.out.println(userDAO);
          11. }
          12. }
      • 运行结果

        •    对比发现,成功通过名称将UserDAO完成了bean对象的注入
      • 手动注入配置文件如下
          1. <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
          2. <property name="userDAO">
          3. <ref bean="userDAO">ref>
          4. property>
          5. bean>
          6. <bean id="userDAO" class="com.example.DAO.Impl.UserDAOImpl">bean>
      • 通过byType
        • 配置文件如下
            1. "userService" class="com.example.Service.Impl.UserServiceImpl" autowire="byType">
            2. "userDAO" class="com.example.DAO.Impl.UserDAOImpl">

     

  • 相关阅读:
    基于SqlSugar的开发框架循序渐进介绍(20)-- 在基于UniApp+Vue的移动端实现多条件查询的处理
    (六)Vue之MVVC
    基于上下文的业务流建模法(三)
    java毕业设计车险销售管理系统mybatis+源码+调试部署+系统+数据库+lw
    学习阶段单片机买esp32还是stm32?
    Vue业务组件封装(二)Form表单
    四十六、AJAX
    基于PSO算法的功率角摆动曲线优化研究(Matlab代码实现)
    容联云首发基于统信UOS的Rphone,打造国产化联络中心新生态
    操作系统实验——进程与线程
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/133040414