美文网首页Ping说SSM
Spring之依赖注入

Spring之依赖注入

作者: Ping开源 | 来源:发表于2021-07-09 00:00 被阅读0次

    六、依赖注入

    目录:构造器注入、set注入、拓展注入实现、Bean的作用域
    依赖注入(Dependency Injection,DI)
    依赖:指Bean对象的创建依赖于容器,Bean对象的依赖资源。
    注入:指Bean对象所依赖的资源,由容器来设置和装配。

    1.构造器注入

    在之前IoC创建对象方式中已详细给出。

    2.set注入(重点)

    要求被注入的属性必须有set方法,set方法的方法名由set+属性首字母大写。如果属性是boolean类型,没有get方法,使用is方法。
    测试pojo类
    Address.java:

    public class Address { 
      private String address; 
      public String getAddress() { 
        return address; 
      }
      public void setAddress(String address) { 
        this.address = address; 
      } 
    }
    

    Student.java:

    package com.ping.pojo; 
    import java.util.List; 
    import java.util.Map; 
    import java.util.Properties; 
    import java.util.Set; 
    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+">>\t"); 
        }
        System.out.println("\n爱好:"+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.ping.pojo.Student">
      <property name="name" value="ping"/>
    </bean>
    

    测试

    @Test 
    public void test1(){ 
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
      Student student = (Student) context.getBean("student"); 
      System.out.println(student.getName()); 
    }
    

    2)Bean注入
    注意:如果这里的值为引用,用ref。

    <bean id="address" class="com.ping.pojo.Address">
      <property name="address" value="nanjing"/>
    </bean> 
    <bean id="student" class="com.ping.pojo.Student">
      <property name="name" value="ping"/>
      <property name="address" ref="address"/>
    </bean>
    

    3)数组注入

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

    4)List注入

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

    5)Map注入

    <property name="card">
      <map>
        <entry key="中行" value="7987946416132131"/>
        <entry key="建行" value="1516546516546546"/>
      </map>
    </property>
    

    6)set注入

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

    7)Null注入

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

    8)Properties注入

    <property name="info">
      <props>
        <prop key="学号">20200706</prop>
        <prop key="性别">男</prop>
        <prop key="姓名">ping</prop>
      </props>
    </property>
    

    3.拓展注入实现

    User.java:
    注意:这里没有有参构造器。

    public class User { 
      private String name; 
      private int age; 
      public void setName(String name) { 
        this.name = name; 
      }
      public void setAge(int age) {   
        this.age = age; 
      }
      @Override 
      public String toString() { 
        return "User{" + 
                    "name='" + name + '\'' + 
                    ", age=" + age + 
                    '}'; 
      } 
    }
    

    1)P命名空间注入 : 需要在头文件中导入约束
    导入约束xmlns:p="http://www.springframework.org/schema/p"

    <!--P(属性: properties)命名空间,属性依然要设置set方法-->
    <bean id="user" class="com.ping.pojo.User" p:name="ping" p:age="18"/>
    

    2)C命名空间注入 : 需要在头文件中导入约束
    导入约束xmlns:c="http://www.springframework.org/schema/c"

    <!--C(构造: Constructor)命名空间,属性依然要设置set方法-->
    <bean id="user" class="com.ping.pojo.User" c:name="ping" c:age="18"/>
    

    如果没有有参构造,这里就会爆红!
    解决:把有参构造器加上,C就是所谓的构造器注入。
    测试

    @Test
    public void test2(){
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      User user = (User) context.getBean("user");
      System.out.println(user); 
    }
    

    4.Bean的作用域

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


    Bean的作用域

    几种作用域中,request、session作用域仅在基于Web的应用中使用(不必管所采用的是什么web应用框架),只能用在基于Web的Spring ApplicationContext环境。
    1)Singleton
    当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管是否使用,它都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:

    <bean id="ServiceImpl" class="com.ping.service.ServiceImpl" scope="singleton">
    

    测试

    @Test 
    public void test3() { 
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
      User user = (User) context.getBean("user"); 
      User user2 = (User) context.getBean("user"); System.out.println(user==user2); 
    }
    

    2) Prototype
    当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在创建容器的时候并没有实例化,而是当获取bean的时候才会去创建一个对象,而且每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:

    <bean id="account" class="com.ping.DefaultAccount" scope="prototype"/>
    

    或者

    <bean id="account" class="com.ping.DefaultAccount" singleton="false"/>
    

    3) Request
    当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

    <bean id="loginAction" class=com.ping.LoginAction" scope="request"/>
    

    针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。
    4)Session
    当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域
    仅在基于Web的Spring ApplicationContext情形下有效。考虑下面bean定义:

    <bean id="userPreferences" class="com.ping.UserPreferences" scope="session"/>
    

    针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

    相关文章

      网友评论

        本文标题:Spring之依赖注入

        本文链接:https://www.haomeiwen.com/subject/subgultx.html