Spring复杂类型属性注入(XML)

  • 实体类:

    CollectionBean.java
    package cn.jjs.c_injection;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    
    public class CollectionBean {
      private Object[] arr;
      private List list;
      private Map map;
      private Properties prop;
    
      public Object[] getArr() {
          return arr;
      }
    
      public void setArr(Object[] arr) {
          this.arr = arr;
      }
    
      public List getList() {
          return list;
      }
    
      public void setList(List list) {
          this.list = list;
      }
    
      public Map getMap() {
          return map;
      }
    
      public void setMap(Map map) {
          this.map = map;
      }
    
      public Properties getProp() {
          return prop;
      }
    
      public void setProp(Properties prop) {
          this.prop = prop;
      }
    
      @Override
      public String toString() {
          return "CollectionBean{" +
                  "arr=" + Arrays.toString(arr) +
                  ", list=" + list +
                  ", map=" + map +
                  ", prop=" + prop +
                  '}';
      }
    }
    
  • Array类型

    如果array数组中只注入一个值|对象,直接使用value|ref即可

    
    <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-3.0.xsd">
    
      <bean name="user4" class="cn.jjs.c_injection.CollectionBean">
          <property name="arr" value="Tom">property>
      bean>
    beans>

    数组多个元素注入:

    
    <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-3.0.xsd">
      
      <bean name="user" class="cn.jjs.bean.User">
          
          <property name="name" value="Tom">property>
          <property name="age" value="18">property>
          <property name="car" ref="carRef">property>
      bean>
    
      
      <bean name="carRef" class="cn.jjs.bean.Car">
          <property name="name" value="Toyota">property>
          <property name="color" value="white">property>
      bean>
    
    
      <bean name="user2" class="cn.jjs.bean.User" p:name="jack" p:age="30" p:car-ref="carRef">bean>
    
      <bean name="user3" class="cn.jjs.bean.User">
          <property name="name" value="#{user.name}">property>
          <property name="age" value="#{user2.age}">property>
          <property name="car" ref="carRef">property>
      bean>
    
      <bean name="cdd" class="cn.jjs.c_injection.CollectionBean">
          <property name="arr">
              <array>
                  <value>Jerryvalue>
                  <value>Tomvalue>
                  <ref bean="user3"/>
              array>
          property>
      bean>
    beans>
  • list类型

    list同array。

    <property name="list">
      <list>
          <value>Liutaovalue>
          <value>Hallevalue>
        <ref bean="user3"/>
    list>
    property>
  • map类型

<property name="map">
    <map>
        
        <entry key="url" value="http://www.baidu.com">entry>
        
        <entry key="user" value-ref="user3">entry>
        
        <entry key-ref="user" value-ref="user2">entry>
    map>
property>
  • properties类型

    <property name="prop">
      <props>
          <prop key="driveClass">com.jdbc.mysql.Driverprop>
          <prop key="username">rootprop>
          <prop key="password">1234prop>
      props>
    property>

你可能感兴趣的:(Spring)