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">
           <!--第一种:普通值注入:value -->
           <property name="name" value="张三"/>
           <!--第二种:bean注入:ref -->
           <property name="address" ref="address"/>
           <!--第三种:bean注入:array标签 -->
           <property name="books">
               <array>
                   <value>book1</value>
                   <value>book2</value>
                   <value>book3</value>
               </array>
           </property>
           <!--第四种:bean注入:list标签 -->
           <property name="hobbys">
               <list>
                   <value>list1</value>
                   <value>list2</value>
                   <value>list3</value>
               </list>
           </property>

           <!--第五种:bean注入:map标签 -->
           <property name="card">
               <map>
                   <entry key="001" value="校园卡"/>
                   <entry key="002" value="银行卡"/>
               </map>
           </property>
           <!--第六种:bean注入:map标签 -->
           <property name="games">
               <set>
                   <value>踢球</value>
                   <value>看球</value>
               </set>
           </property>
           <!--第七种:bean注入:null值 -->
           <property name="wife">
               <null/>
           </property>
           <!--第八种:bean注入:props标签 -->
           <property name="info">
               <props>
                   <prop key="邮箱">22222@qq.com</prop>
                   <prop key="学号">11122333</prop>
               </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">

         <!-- p命名空间注入,可以直接注入属性的值: -->
         <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">


         <!-- c命名空间注入,通过构造器注入:constructor-arg -->
         <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"

       

 

 

 

posted @   gzs1024  阅读(135)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示