• Spring中的依赖注入、setter与构造器注入、自动装配与集合注入详细描述及使用(XML版中篇)



    💨更多相关知识👇

    💖Java版本新零售小程序saas商城全开源系统

    💖Spring中的bean的配置、作用范围、生命周期详细描述及使用(XML版上篇)

    💖异常处理与解决方案详解上篇

    💖异常处理与解决方案详解下篇

    💖Math类与System类的常用方法使用

    💖JavaEE中的静态方法定义、方法重载要求、return作用详解

    💖List接口的常用方法,精华总结

    💖JavaEE中的Stream流的常用方法

    💖JavaEE中的Stream流知识点使用,精华总结,一文直接上手


    🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
     
    🍂个人博客首页: KJ.JK
     
    欢迎大家点赞👍收藏💖评论💬关注🔒
     
    💖源码获取 | 💻学习交流 | 🤝商务合作 | 💨私信作者
     
    🍂项目实战gitee开源地址: weiit/weiit-saas
     
    🍂项目实战github开源地址: weiit/weiit-saas
     
    ⭐希望大家可以在gitee和github点个star呀⭐


    一、setter注入和构造器注入


    🍂setter注入—引用类型


              "setter注入—引用类型步骤"
    ------------------------------------------------------------------------------------------------------------
                  
                         1.在bean中定义引用类型属性并"提供可访问的set方法"
                              public class BookServiceImpl implements BookService{
                                  private BookDao bookDao;
                                  public void setBookDao(BookDao bookDao) {
                                      this.bookDao = bookDao;
                                  }
                              }
    
                         2.配置中使用"property"标签"ref"属性注入引用类型对象 (references)
                             
                             <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
                                 <property name="bookDao" ref="bookDao"/>
                             </bean>
                                 
                            <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
    
    /*
      property标签:依赖注入,通过set方法输入
             name:类中的成员变量
             ref:注入的对象
    
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    ⭐代码演示

    // 业务层实现类
    
    public class BookServiceImpl implements BookService {
        private BookDao bookDao;
    
        public BookServiceImpl() {
        }
    
        public BookServiceImpl(BookDao bookDao) {
            System.out.println("BookServiceImpl 带参构造器注入");
            this.bookDao = bookDao;
        }
    
        public void setBookDao(BookDao bookDao) {
            System.out.println("BookServiceImpl set注入");
            this.bookDao = bookDao;
        }
    
        @Override
        public void save() {
    
            bookDao.save();
        }
    
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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">
    
    
    
         <!--   等价于 book =new BookServiceImpl
                bean其实就是用来创建对象的
                                                -->
        <bean class="com.itheima.service.impl.BookServiceImpl" id="bookService"
                         name="b1,b2 ; b3 b4"
                         scope="singleton"
    
                        >
    
    
            <!--BookServiceImpl的bookDao成员变量赋值  ref:引用IOC容器中对的bookDao
               property标签:依赖注入,通过set方法输入
                      name:类中的成员变量
                      ref:注入的对象-->
            <property name="bookDao" ref="bookDao"></property>
    
    
    
        </bean>
    
    
        <!--IOC容器创建BookDaoImpl对象-->
    
        <!--  bookDao =new BookDaoImpl()  -->
        <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao"></bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
    @Test
        public void testDI() {
    
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
    
            //2.从IoC容器中获取Bean对象
    
            BookService bookService = (BookService) ctx.getBean("bookService");
    
            //3.调用Bean对象(BookService对象)的方法
            bookService.save();
    
            //4.关闭容器
            ctx.close();
    
        }        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    请添加图片描述


    🍂setter注入—简单类型(八种基本数据类型+String)


               "setter注入—简单类型步骤"
    ------------------------------------------------------------------------------------------------------------
                   
                          1.在bean中"定义简单类型属性""提供可访问的set方法"
                                 public class BookDaoImpl implements BookDao {
                                     private int connectionNumber;
                                     private String databaseName;
                                     public void setConnectionNumber(int connectionNumber) {                                                          this.connectionNumber = connectionNumber;
                                   }
                                 }
    
                          2.配置中使用"property标签"中的"value属性注入简单类型数据"
                              
                                 <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
                                     <property name="connectionNumber" value="10"/>
                                     <property name="databaseName" value="mysql"/>
                                 </bean>
                                     
    /*
        value属性: 注入简单类型的值  (基本数据类型+String)
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ⭐代码演示

    // 数据访问层实现类
    public class BookDaoImpl implements BookDao {
        private int connectionNumber;
        private String databaseName;
    
    
        public BookDaoImpl(){
    
            System.out.println("BookDaoImpl 无参构造器创建对象");
        }
    
        public BookDaoImpl(int connectionNumber, String databaseName) {
    
            System.out.println("BookDaoImpl 带参构造器创建对象");
            this.connectionNumber = connectionNumber;
            this.databaseName = databaseName;
        }
    
    
        public void setConnectionNumber(int connectionNumber) {
            this.connectionNumber = connectionNumber;
        }
    
        public void setDatabaseName(String databaseName) {
            this.databaseName = databaseName;
        }
    
    
    
        @Override
        public void save() {
    
        System.out.println("保存 <<富婆通讯录>>");
        }
    
    
        @Override
        public String toString() {
            return "BookDaoImpl{" +
                    "connectionNumber=" + connectionNumber +
                    ", databaseName='" + databaseName + '\'' +
                    '}';
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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 class="com.itheima.service.impl.BookServiceImpl" id="bookService" autowire="byType">
    
    
            <property name="bookDao" ref="bookDao"></property>
    
        </bean>
    
    
       <!--
    
          property标签: 依赖注入,通过set方法注入
              name属性: 成员变量名
              ref属性: 注入引用类型
              value属性: 注入简单类型的值  (基本数据类型+String)
       -->
    
    
            <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao">
    
            <property name="connectionNumber" value="10"/>
            <property name="databaseName" value="mysql"/>
    
    
        </bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
        @Test
        public void testDI() {
    
            // 1.根据 applicationContext.xml 去创建一个IoC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-DI.xml");
    
            // 2.从IoC容器中获取对象(Bean)
            BookService service = (BookService) context.getBean("bookService");
    
            BookDao bookDao = (BookDao) context.getBean("bookDao");
            System.out.println(bookDao);
    
            // 4.关闭容器
            context.close();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    请添加图片描述


    🌿构造器注入—引用类型


                  "构造器注入—引用类型步骤"
    ------------------------------------------------------------------------------------------------------------
                          
                        1.在bean中定义引用类型属性并"提供可访问的构造方法"
                           public class BookServiceImpl implements BookService{
                               private BookDao bookDao;
                               public BookServiceImpl(BookDao bookDao) {
                                   this.bookDao = bookDao;
                               }
                           }
    
    
                        2. 配置中使用"constructor-arg标签"中的"ref属性注入引用类型对象"
                            
                            <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
                                <constructor-arg name="bookDao" ref="bookDao"/>
                            </bean>
                                
                            <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
    
                                
    /*
    
    constructor-arg标签: 构造器注入, 用构造器给成员变量赋值
    
                   ref: 注入的对象,引用数据类型
                   name: 构造器的参数名, 耦合很紧
     
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    ⭐代码演示

    // 业务层实现类
    
    public class BookServiceImpl implements BookService {
        private BookDao bookDao;
    
        public BookServiceImpl() {
        }
    
        public BookServiceImpl(BookDao bookDao) {
            System.out.println("BookServiceImpl 带参构造器注入");
            this.bookDao = bookDao;
        }
    
        public void setBookDao(BookDao bookDao) {
            System.out.println("BookServiceImpl set注入");
            this.bookDao = bookDao;
        }
    
        @Override
        public void save() {
    
            bookDao.save();
        }
    
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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">
    
    
    
         <!--   等价于 book =new BookServiceImpl
                bean其实就是用来创建对象的
                                                -->
        <bean class="com.itheima.service.impl.BookServiceImpl" id="bookService"
                         name="b1,b2 ; b3 b4"
                         scope="singleton"
    
                        >
    
    
        <!--constructor-arg标签: 构造器注入, 用构造器给成员变量赋值
    
                  ref: 注入的对象,引用数据类型
                   name: 构造器的参数名, 耦合很紧
           -->
    
               <constructor-arg name="bookDao" ref="bookDao"/>
    
    
           </bean>
    
    
           <!--IOC容器创建BookDaoImpl对象-->
    
        <!--  bookDao =new BookDaoImpl()  -->
        <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao"></bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
        @Test
        public void testDI() {
    
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
    
            //2.从IoC容器中获取Bean对象
    
            BookService bookService = (BookService) ctx.getBean("bookService");
    
            //3.调用Bean对象(BookService对象)的方法
            bookService.save();
    
            //4.关闭容器
            ctx.close();
    
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    请添加图片描述


    🌿构造器注入—简单类型


                  "构造器注入—简单类型步骤"
    ------------------------------------------------------------------------------------------------------------
                      
                         1.在bean中"定义简单类型的属性""提供构造方法方法"
                      
                             public class BookDaoImpl implements BookDao {
                                 private int connectionNumber;
                                 private String databaseName;
                                 public BookDaoImpl(int connectionNumber, int databaseName) {                                                    this.connectionNumber = connectionNumber;
                                     this. databaseName = databaseName;
                              }
                             }
    
    
                          2.配置中使用"constructor-arg标签"中的"value属性注入简单类型数据"
                              
                              <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
                                  <constructor-arg name="connectionNumber" value="10"/>
                                  <constructor-arg name=“databaseName" value="mysql"/>
                              </bean>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ⭐代码演示

    <?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 class="com.itheima.service.impl.BookServiceImpl" id="bookService" autowire="byType">
    
    
            <property name="bookDao" ref="bookDao"></property>
    
        </bean>
        
        <!--constructor-arg标签: 构造器注入, 用构造器给成员变量赋值
                ref: 注入的对象,引用数据类型
    
                name: 构造器的参数名, 耦合很紧
    
               value属性:注入简单类型数据
                -->
    
    
            <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao">
    
    
                <constructor-arg name="connectionNumber" value="20"/>
                <constructor-arg name="databaseName" value="mysql2"/>
        </bean>
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    请添加图片描述


    🌿构造器注入—参数适配


                   * 配置中使用"constructor-arg"标签"type属性"设置"按形参类型注入"  (要有相应的构造方法)
                       
                       例子:
    
                          <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
                              <constructor-arg type="int" value="10"/>
                              <constructor-arg type="java.lang.String" value="mysql"/>
                           </bean>
             
    ------------------------------------------------------------------------------------------------------------   
    
                   * 配置中使用"constructor-arg"标签"index属性"设置"按形参位置注入" (要有相应的构造方法)
                            
                        例子:
                            <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
                                <constructor-arg index="0" value="10"/>
                                <constructor-arg index="1" value="mysql"/>
                            </bean>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ⭐type属性参数适配代码演示

    
    <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 class="com.itheima.service.impl.BookServiceImpl" id="bookService" autowire="byType">
    
    
            <property name="bookDao" ref="bookDao">property>
    
        bean>
    
     
    
            <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao">
    
                <constructor-arg type="int" value="30"/>
                <constructor-arg type="java.lang.String" value="mysql3"/>
    
            bean>
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    请添加图片描述


    ⭐index属性参数适配代码演示

    
    <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 class="com.itheima.service.impl.BookServiceImpl" id="bookService" autowire="byType">
    
            <property name="bookDao" ref="bookDao">property>
    
        bean>
    
    
    
    
            <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao">
    
    
                <constructor-arg index="0" value="40"/>
                <constructor-arg index="1" value="mysql4"/>
    
            bean>
    
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    请添加图片描述


    🔥系列热门专栏🔥:
     
    《JavaEE进阶序列 | 从小白到工程师》系列文章
     
    《JavaEE项目实战》系列文章
     
    《JavaSwing项目合集》系列文章
     
    《数据分析中的pandas》系列文章


    ⭕setter注入和构造器注入总结


           * setter注入:     
                       使用"property标签",提供对应的"set方法"
                           
                             " name属性: 成员变量名
                           
                             " ref属性: 注入引用类型
                           
                             " value属性: 注入简单类型的值  (基本数据类型+String)
              * 例子:
    
                   * set注入引用类型:
                                    <property name="bookDao" ref="bookDao"/>
                           
                   * set注入简单类型:
                                    <property name="connectionNumber" value="10"/>
                                        
    ------------------------------------------------------------------------------------------------------------
    
          * 构造器注入:
                        使用"constructor-arg标签",提供"构造方法方法"
                            
                              " name: 构造器的参数名
                                   
                              " ref: 注入引用类型 
                            
                              " value: 注入简单类型的值  (基本数据类型+String)
                                   
                              " type:  根据构造器的参数类型注入
                                   
                              " index: 根据构造器的参数位置注入,0开始
                            
               * 例子:
                      
                  * 构造器注入引用类型:
                                     <constructor-arg name="bookDao" ref="bookDao"/>     
                                         
                  * 构造器注入简单类型:
                                    <constructor-arg name="connectionNumber" value="20"/>
                                        
         * 根据构造器的参数类型type注入:
                                    <constructor-arg type="int" value="30"/>
                                    <constructor-arg type="java.lang.String" value="mysql3"/>
                                        
         * 根据构造器的参数位置index注入:
                                    <constructor-arg index="0" value="40"/>
                                    <constructor-arg index="1" value="mysql4"/>
    
    ------------------------------------------------------------------------------------------------------------                                    
    /*
      通常情况,建议使用set注入,除非特殊情况没有set方法无法set注入,再使用构造器注入
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    二、自动装配


    🌴自动装配介绍

                  * 概念: IoC容器根据bean所依赖的资源在容器中"自动查找并注入到bean中的过程"称为"自动装配"
                      
                      
                  * 配置中使用bean标签"autowire属性设置自动装配的类型":
                       <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
                           
                       <bean id="bookService" class="com.itheima.service.impl.BookServiceImpl"                                       autowire="byType"/>
    
    
                      
                  * 自动装配类型
                              * byType:"类型装配(最常用)", 自动在IoC容器中找"类型一样的的对象赋值, 默认使用set注入"
                      
                              * byName:"名字装配, 默认使用set注入"
                      
                              * constructor: "构造器装配, 找名字一样的"
                      
                              * no:不使用自动装配
    /*
    
    "同一实现类" 或者 "同一个接口" 都算同一类型
    
    */
                                  
                                  
    ------------------------------------------------------------------------------------------------------------                              
    /*
      注意事项:
             1.自动装配用于"引用类型依赖注入" , 不能对简单类型进行操作
             
             2.使用按类型装配时(byType)必须保障容器中相同类型的bean唯一,推荐使用
             
             3.使用按名称装配时(byName)必须保障容器中具有指定名称的bean,因变量名与配置耦合,不推荐使用
             
             4.自动装配优先级低于setter注入与构造器注入,同时出现时自动装配配置失效
             
    
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    ⭐byType装配演示


    请添加图片描述


    ⚡byType装配注意⚡


    请添加图片描述


    ⭐byName装配演示


    请添加图片描述


    ⭐constructor装配演示


    请添加图片描述


    三、集合注入


                       1.注入"数组"对象
                           
                           <property name="数组的属性名">
                              <array>
                                <value>注入的值</value>
                                <value>注入的值</value>
                                <value>注入的值</value>
                              </array>
                           </property>
                           
    ------------------------------------------------------------------------------------------------------------                       
                       2.注入"List"对象
                           
                            <property name="list">
                                <list>
                                  <value>注入的值</value>
                                   <value>注入的值</value>
                                   <value>注入的值</value>
                                 </list>
                           </property>
                           
    ------------------------------------------------------------------------------------------------------------
    
                        3.注入"Set"对象
                           
                           <property name="set">
                                <set>
                                   <value>注入的值</value>  
                                   <value>注入的值</value> 
                                   <value>注入的值</value>
                               </set>
                           </property>
                           
    ------------------------------------------------------------------------------------------------------------
                        
                         4.注入"Map"对象
                           
                           <property name="map">
                             <map>
                               <entry key="要注入的键" value="要注入的值"/>
                               <entry key="要注入的键" value="要注入的值"/>
                               <entry key="要注入的键" value="要注入的值"/>
                            </map>
                           </property>
                           
    ------------------------------------------------------------------------------------------------------------
                         
                         5.注入"Properties"对象
                           
                            <property name="properties">
                                <props>
                                   <prop key="要注入的键">要注入的值</prop>
                                   <prop key="要注入的键">要注入的值</prop>
                                   <prop key="要注入的键">要注入的值</prop>
                               </props>
                           </property>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    ⭐代码演示

    //Person类
    
    import java.util.*;
    
    public class Person {
        private int[] score;  // 数组
        private List<String> list;  // 列表
        private Set<String> set;  // 集合
        private Map<String, String> map;
        private Properties properties;  // 属性类型
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public Map<String, String> getMap() {
            return map;
        }
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    
        public Set<String> getSet() {
            return set;
        }
    
        public void setSet(Set<String> set) {
            this.set = set;
        }
    
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        public int[] getScore() {
            return score;
        }
    
        public void setScore(int[] score) {
            this.score = score;
        }
    
        @Override
        public String toString() {
            return "Person{" +
                    "\n\tscore=" + Arrays.toString(score) +
                    ", \n\tlist=" + list +
                    ", \n\tset=" + set +
                    ", \n\tmap=" + map +
                    ", \n\tproperties=" + properties +
                    "\n}";
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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 class="com.itheima.service.impl.BookServiceImpl"
              id="bookService"
              >
    
    
        </bean>
    
    <!--       BookDaoImpl这个类实现了 BookDao 接口-->
            <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao">
    
                <constructor-arg index="0" value="40"/>
                <constructor-arg index="1" value="mysql4"/>
    
            </bean>
    
    
        <!--person = new Person()-->
        <bean class="com.itheima.pojo.Person" id="person">
            <property name="score">
                <!--new int[] {100, 200, 300}-->
                <array>
                    <value>100</value>
                    <value>200</value>
                    <value>300</value>
                </array>
            </property>
    
            <property name="list">
                <list>
                    <value>广州</value>
                    <value>东莞</value>
                    <value>深圳</value>
                </list>
            </property>
    
            <property name="set">
                <set>
                    <value>小蛮腰</value>
                    <value>太子酒店</value>
                    <value>世界之窗</value>
                </set>
            </property>
    
            <property name="map">
                <map>
                    <entry key="aa" value="11"/>
                    <entry key="bb" value="22"/>
                    <entry key="cc" value="33"/>
                </map>
            </property>
    
            <property name="properties">
                <props>
                    <prop key="xx">77</prop>
                    <prop key="yy">88</prop>
                    <prop key="zz">99</prop>
                </props>
            </property>
        </bean>
    
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    请添加图片描述


    💨项目综合实战文章👇

    💖Java版本新零售小程序saas商城全开源系统


    在这里插入图片描述


    在这里插入图片描述


    在这里插入图片描述


    🍂项目实战gitee开源地址: weiit/weiit-saas
     
    🍂项目实战github开源地址: weiit/weiit-saas
     
    ⭐希望大家可以在gitee和github点个star呀⭐


    作者:KJ.JK

    文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

  • 相关阅读:
    Labs‘Codes review(AVR)(3)
    uniapp 安卓保活插件 Ba-KeepAlive
    ElasticSearch从入门到精通--第四话(核心概念篇)
    初识动态内存分配
    netty系列之:HashedWheelTimer一种定时器的高效实现
    【Docker安装部署Kafka+Zookeeper详细教程】
    Kafka消费者使用案例
    Chrome浏览器插件开发v3版本第二篇:改变页面布局案例
    调用API接口获取淘宝商品数据:实践指南与代码解析
    MindSponge分子动力学模拟——体系控制(2024.05)
  • 原文地址:https://blog.csdn.net/m0_47384542/article/details/126111088