美文网首页
spring5(6) -DI 通过xml配置装配 -1

spring5(6) -DI 通过xml配置装配 -1

作者: 小白201808 | 来源:发表于2018-09-18 18:49 被阅读30次
    一. 通过XML配置装配的方式
     1.XML自动装配(不推荐)
     2.Setter注入
     3.构造器注入
     4.bean元素继承
     5.Property place holder
     6.注册案例
    
    代码演示

    (1)XML自动装配(不推荐)

    使用autowire modes:no ,default, byName,byType,constructor
    
    <?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 = "dog" class = "com.keen.xml_auto.Dog"/>
    <bean id = "person" class ="com.keen.xml_auto.Person" autowire="byName"/>
    
    </beans>
    

    (2)Setter注入(常用)

    1.简单类型(常量值)注入
      使用property,name ,value;
    
    <?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 = "employee" class ="com.keen.xml.setter.Employee">
        <property name="name" value = "keen"/>
        <property name="age" value = "23"/>
        <property name="salary" value = "6666"/>
    </bean> 
        
    </beans>
    

    Employee类

    package com.keen.xml.setter;
    
    public class Employee {
       private String name;
       private int age;
       private double salary;
       public void setName(String name) {
           this.name = name;
       }
       public void setAge(int age) {
           this.age = age;
       }
       public void setSalary(double salary) {
           this.salary = salary;
       }
       @Override
       public String toString() {
           return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
       }
       
    }
    
    2.对象类型注入(使用property 的ref 关联对象)
    
    <!-- 属性注入:对象类型 -->    
    <bean id = "cat1" class = "com.keen.xml.setter.Cat"/>
    <bean id = "person" class ="com.keen.xml.setter.Person">
       <property name="cat" ref = "cat1"/>
    </bean>
    
    ⚠️<!-- ref = "cat1" 与 bean id = “cat1”要一致 -->
    
     3.集合类型注入(使用对应集合元素<set> ,list>, <map>...)
    
    <!--属性注入: 集合类型 -->
    <bean id = "collectionbean1" class = "com.keen.xml.setter.CollectionBean1">
    <!-- set配置 -->
    <property name="set">
      <set>
         <value>set1</value>
         <value>set2</value>
      </set>
    </property>
    <!-- list配置 -->
      <property name="list">
        <list>
          <value>list1</value>
          <value>list2</value>
        </list>
      </property>
     <!-- array配置 -->
     <property name="array">
       <array>
         <value>array1</value>
         <value>array2</value>
       </array>
     </property>
     <!-- map配置 -->
     <property name="map">
     <map>
     <entry key="key1"  value = "value1"/>
     <entry key="key2"  value = "value2"/>
    </map>
     </property>
     <!--1 properties设置 -->
    <!-- <property name="prop">
    <props>
      <prop key="p1">v1</prop>
      <prop key="p2">v2</prop>
    
    </props>
    </property> -->
    <!-- 2 properties设置(比较简单,比较常用) -->
    <property name="prop">
    <value>
       p1 = v1;
       p2 = v2;
       p3 = v3;
    
    </value>
    
    </property>
    </bean>
    

    CollectionBean1类

    package com.keen.xml.setter;
    
    import java.util.*;
    
    public class CollectionBean1 {
        private Set<String> set;
        private List<String> list;
        private String[] array ;
        private Map<String,String> map;
        private Properties prop;
        public void setArray(String[] array) {
            this.array = array;
        }
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
        public void setProp(Properties prop) {
            this.prop = prop;
        }
        public void setSet(Set<String> set) {
            this.set = set;
        }
        public void setList(List<String> list) {
            this.list = list;
        }
        @Override
        public String toString() {
            return "CollectionBean1 [set=" + set + ", list=" + list + ", array=" + Arrays.toString(array) + ", map=" + map
                    + ", prop=" + prop + "]";
        }
       
    }
    
    

    (3)构造器注入(了解)

    <1>常量类型注入

    <!-- 构造器注入:常量类型 -->
    <bean id = "employee" class = "com.keen.xml.constructor.Employee1">
    <constructor-arg name = "name" value = "keen"/>
    <constructor-arg name = "age" value = "age"/>
    <constructor-arg name = "salary" value = "6666"/>
    
    </bean>
    

    Employee1类

    public class Employee1 {
        private String name;
        private int age;
        private double salary;
        
        public Employee1(String name, int age, double salary) {
            super();
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
    
        @Override
        public String toString() {
            return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
        }
    
    }
    

    <2>对象类型注入(了解)

    <!-- 构造器注入:对象类型 -->
    <bean id = "cat" class = "com.keen.xml.constructor.Cat1"/>  
    <bean id = "person" class = "com.keen.xml.constructor.Person1">
    <constructor-arg name = "cat1" ref = "cat"></constructor-arg>
    </bean>
    

    人类

    package com.keen.xml.constructor;
    
    public class Person1 {
        private Cat1 cat1;
    
        public Person1(Cat1 cat1) {
            super();
            this.cat1 = cat1;
        }
    
        @Override
        public String toString() {
            return "Person [cat=" + cat1 + "]";
        }
    
    }
    

    猫类

    package com.keen.xml.constructor;
    
    public class Cat1 {
    
    }
    

    <3>集合类型注入:

    (1)将seeter注入的集合注入类型中的property改为constructor-arg即可
    (2)将employee的setter方法改为带参数构造器即可
    
    <!--构造器注入: 集合类型 -->
    
    <bean id = "collectionbean2" class = "com.keen.xml.constructor.CollectionBean2">
    <!-- set配置 -->
    <constructor-arg name="set">
      <set>
         <value>set1</value>
         <value>set2</value>
      </set>
    </constructor-arg>
    <!-- list配置 -->
      <constructor-arg name="list">
        <list>
          <value>list1</value>
          <value>list2</value>
        </list>
      </constructor-arg>
     <!-- array配置 -->
     <constructor-arg name="array">
       <array>
         <value>array1</value>
         <value>array2</value>
       </array>
     </constructor-arg>
     <!-- map配置 -->
     <constructor-arg name="map">
     <map>
     <entry key="key1"  value = "value1"/>
     <entry key="key2"  value = "value2"/>
    </map>
     </constructor-arg>
     <!--1 properties设置 -->
    <!-- <constructor-arg name="prop">
    <props>
      <prop key="p1">v1</prop>
      <prop key="p2">v2</prop>
    
    </props>
    </constructor-arg> -->
    <!-- 2 properties设置(比较简单,比较常用) -->
    <constructor-arg name="prop">
    <value>
       p1 = v1;
       p2 = v2;
       p3 = v3;
    
    </value>
    
    </constructor-arg>
    </bean>
    

    添加:内部bean,外面无法访问

    <!-- 内部bean -->
     <bean id = "person" class = "com.keen.xml.constructor.Person1">
    <constructor-arg name = "cat1" >
       <bean class ="com.keen.xml.constructor.Cat1"/>
    </constructor-arg>
    
    <!-- 此时 cat1 仅服务于 person-->
    
    

    (4)bean元素的继承

     多个bean元素的共同配置部分的抽取,和Java的继承不同
     
     Java继承:把多个类共同的代码抽取到父类中
     bean元素的继承(inheritance):把多个bean元素共同的属性配置抽取到另外一个公用的bean元素中。
    

    xml原本的配置

    <bean id = "somebean1" class = "com.keen.bean.inheitance.Somebean1">
    <property name="name" value = "keen"/>
    <property name="age" value = "18"/>
    <property name="height" value = "160"/>
    </bean>
    <bean id = "somebean2" class = "com.keen.bean.inheitance.Somebean2">
    <property name="name" value = "keen"/>
    <property name="age" value = "18"/>
    <property name="weight" value = "45"/>
    </bean>
    

    bean元素的继承后配置

    <!-- bean元素的继承 -->
    <bean id ="base" abstract="true">
       <property name="name" value = "keen"/>
       <property name="age" value = "18"/>
    </bean>
    
    <!-- 配置somebean1 -->
    <bean id = "somebean1" class = "com.keen.bean.inheitance.Somebean1" parent="base">
    <property name="height" value = "160"/>
    </bean>
    
    <!-- 配置somebean2 -->
    <bean id = "somebean2" class = "com.keen.bean.inheitance.Somebean2" parent="base">
    <property name="weight" value = "45"/>
    </bean>
    

    相关文章

      网友评论

          本文标题:spring5(6) -DI 通过xml配置装配 -1

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