spring学习08:DI依赖注入


spring学习08:DI依赖注入

  • 依赖注入:

    • 构造器注入;

    • set方式注入;

    • 拓展方式注入【C命名和P命名空间注入】;

 



 

  • set方式注入【重点】:

    • 依赖注入:本质上set注入;

      • 依赖:bean对象的创建依赖容器;

      • 注入:bean对象中的所有属性,由容器来注入;

         

    • 【环境搭建】:

      • 复杂类型:

        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;
           
        }

         

 

  • 代码案例:beans.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 id="address" class="com.ljxdemo.pojo.Address">
           <property name="address" value="海南省xxxx号"/>
       bean>
    ?
       <bean id="student" class="com.ljxdemo.pojo.Student">
           
           <property name="name" value="张三"/>
           
           <property name="address" ref="address"/>
           
           <property name="books">
               <array>
                   <value>book1value>
                   <value>book2value>
                   <value>book3value>
               array>
           property>
           
           <property name="hobbys">
               <list>
                   <value>list1value>
                   <value>list2value>
                   <value>list3value>
               list>
           property>
    ?
           
           <property name="card">
               <map>
                   <entry key="001" value="校园卡"/>
                   <entry key="002" value="银行卡"/>
               map>
           property>
           
           <property name="games">
               <set>
                   <value>踢球value>
                   <value>看球value>
               set>
           property>
           
           <property name="wife">
               <null/>
           property>
           
           <property name="info">
               <props>
                   <prop key="邮箱">22222@qq.comprop>
                   <prop key="学号">11122333prop>
               props>
           property>
       bean>
    ?
    beans>
  • 代码案例:测试类

    public class MyTest {
    ?
       @Test
       public void test(){
           ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
           Student student = (Student) context.getBean("student");
           String name = student.getName();
           System.out.println(student.toString());
      }
    }

     

 



 

  • 拓展方式注入【P命名空间注入】:

    • xml配置文件:导入p命名空间:xmlns:p="http://www.springframework.org/schema/p"

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p="http://www.springframework.org/schema/p"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd">
      ?
         
         <bean id="user" class="com.ljxdemo.pojo.User" p:age="11" p:name="demo"/>
      ?
      beans>
    • 测试类:

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

       

 

  • 拓展方式注入【C命名空间注入】:

    • xml配置文件:导入p命名空间:xmlns:c="http://www.springframework.org/schema/c"

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:p="http://www.springframework.org/schema/p"
            xmlns:c="http://www.springframework.org/schema/c"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd">
      ?

         
         <bean id="user2" class="com.ljxdemo.pojo.User" c:age="11" c:name="张三" />
      ?
      beans>
    • 测试类:

      public class MyTest2 {
      ?
         @Test
         public void test(){
             ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
             User user = (User) context.getBean("user2");
             System.out.println(user);
        }
      }

       

  • 注意点:

    • p命名和c命名空间不能直接使用,需要导入约束;

      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:c="http://www.springframework.org/schema/c"