Spring:(三) 依赖注入(DI)

一、前言

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

二、依赖注入分类

  1. 构造器注入(有参,无参构造)
  2. setter注入(重点)
    • 要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .

三、实现

  1. 实体类

    public class Address {
    
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
    public class Student {
    
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobbys;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public void setHobbys(List<String> hobbys) {
            this.hobbys = hobbys;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        public void setInfo(Properties info) {
            this.info = info;
        }
    
        public void show(){
            System.out.println("name="+ name
                    + ",address="+ address.getAddress()
                    + ",books="
            );
            for (String book:books){
                System.out.print("<<"+book+">>	");
            }
            System.out.println("
    爱好:"+hobbys);
    
            System.out.println("card:"+card);
    
            System.out.println("games:"+games);
    
            System.out.println("wife:"+wife);
    
            System.out.println("info:"+info);
    
        }
    }
    
    1. 注入方式实现

      常量注入

      <bean id="student" class="com.ry.pojo.Student">
          <property name="name" value="小明"/>
      </bean>
      
      @Test
      public void test01(){
          ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      
          Student student = (Student) context.getBean("student");
      
          System.out.println(student.getName());
      
      }
      

      Bean注入

      <bean id="addr" class="com.ry.pojo.Address">
          <property name="address" value="河北"/>
      </bean>
      
      <bean id="student" class="com.kuang.pojo.Student">
          <property name="name" value="小明"/>
          <property name="address" ref="addr"/>
      </bean>
      

      数组注入

      <bean id="student" class="com.ry.pojo.Student">
          <property name="name" value="小明"/>
          <property name="address" ref="addr"/>
          <property name="books">
              <array>
                  <value>西游记</value>
                  <value>红楼梦</value>
                  <value>水浒传</value>
              </array>
          </property>
      </bean>
      

      list注入

      <property name="hobbys">
          <list>
              <value>听歌</value>
              <value>看电影</value>
              <value>爬山</value>
          </list>
      </property>
      

      map注入

      <property name="card">
          <map>
              <entry key="中国邮政" value="456456456465456"/>
              <entry key="建设" value="1456682255511"/>
          </map>
      </property>
      

      set注入

      <property name="games">
          <set>
              <value>LOL</value>
              <value>BOB</value>
              <value>COC</value>
          </set>
      </property>
      

      null注入

      <property name="wife"><null/></property>
      

      properties注入

      <property name="info">
          <props>
              <prop key="学号">20190604</prop>
              <prop key="性别">男</prop>
              <prop key="姓名">小明</prop>
          </props>
      </property>
      

      p命名空间注入

      导入约束 : xmlns:p="http://www.springframework.org/schema/p"
      
      <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
      <bean id="user" class="com.ry.pojo.User" p:name="路飞" p:age="20"/>
      

      c命名空间注入(相当于构造器注入,需要创建有参构造)

      导入约束 : xmlns:c="http://www.springframework.org/schema/c"
      <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
      <bean id="user" class="com.ry.pojo.User" c:name="路飞" c:age="20"/>
      

      四、bean的作用域

      组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .

  • Singleton

    • 当bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例
    • 对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例
    • Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象
    • <bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
  • Prototype

    • bean的作用域为Prototype,表示一个bean定义对应多个对象实例

    • Prototype作用域的bean会导致在每次对该bean请求时都会创建一个新的bean实例

    • Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象

    • 对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域

    • <bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>  
       或者
      <bean id="account" class="com.foo.DefaultAccount" singleton="false"/> 
      
  • Request

    • 当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例

    • 每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成

    • 作用域仅在基于web的Spring ApplicationContext情形下有效

    • <bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
      
  • session

    • 一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例
原文地址:https://www.cnblogs.com/dreamzone/p/12360159.html