• spring 装配集合


    1:创建pojo,属性包含集合,集合元素为基本类型

      

    package com.liyafei.pojo;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    public class ComplexAssemble {
    
        private Long id;
        private List<String> list;
        private Map<String,String> map;
        private Properties properties;
        private Set<String> set;
        private String[] array;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public List<String> getList() {
            return list;
        }
        public void setList(List<String> list) {
            this.list = list;
        }
        public Map<String, String> getMap() {
            return map;
        }
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
        public Properties getProperties() {
            return properties;
        }
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
        public Set<String> getSet() {
            return set;
        }
        public void setSet(Set<String> set) {
            this.set = set;
        }
        public String[] getArray() {
            return array;
        }
        public void setArray(String[] array) {
            this.array = array;
        }
    }
    package com.liyafei.pojo;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    public class ComplexAssemble {
    
        private Long id;
        private List<String> list;
        private Map<String,String> map;
        private Properties properties;
        private Set<String> set;
        private String[] array;
        
            setter and getter
    }

    2:装配bean

      

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    <bean id="complexAssembly" class="com.liyafei.pojo.ComplexAssemble">
        <property name="id" value="1" />
        <property name="list">
            <list>
                <value>value-list-1</value>
                <value>value-list-2</value>
                <value>value-list-3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="key1" value="value-key-1"/>
                <entry key="key2" value="value-key-2"/>
                <entry key="key3" value="value-key-3"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="prop1">value-prop-1</prop>
                <prop key="prop2">value-prop-2</prop>
                <prop key="prop3">value-prop-3</prop>
            </props>
        </property>
        <property name="set">
            <set>
                <value>value-set-1</value>
                <value>value-set-2</value>
                <value>value-set-3</value>
            </set>
        </property>
        <property name="array">
            <array>
                <value>value-array-1</value>
                <value>value-array-2</value>
                <value>value-array-3</value>
            </array>
        </property>
    </bean>
    </beans>

    3:创建pojo,属性包含集合,集合元素为对象类型

      

    public class Role {
    
        private Long id;
        private String roleName;
        private String note;
        public Role(){
            
        }
        public Role(Long id, String roleName, String note) {
            this.id = id;
            this.roleName = roleName;
            this.note = note;
        }
              setter and getter  
    }
    public class User {
    
        private Long id;
        private String userName;
            setter and getter  
    }

        装配类:

    public class UserRoleAssembly {
    
        private Long id;
        private List<Role> list;
        private Map<Role, User> map;
        private Set<Role> set;
           setter and getter
    }

    4:装备bean

      

      <bean id="role1" class="com.liyafei.pojo.Role">
          <property name="id" value="1"></property>
          <property name="roleName" value="role_name_1"></property>
          <property name="note" value="role_note_1"></property>
      </bean>
      <bean id="role2" class="com.liyafei.pojo.Role">
          <property name="id" value="2"></property>
          <property name="roleName" value="role_name_2"></property>
          <property name="note" value="role_note_2"></property>
      </bean>
      <bean id="user1" class="com.liyafei.pojo.User">
          <property name="id" value="1"></property>
          <property name="userName" value="user_name_1"></property>
          <property name="note" value="role_note_1"></property>
      </bean>
      <bean id="user2" class="com.liyafei.pojo.User">
          <property name="id" value="2"></property>
          <property name="userName" value="user_name_2"></property>
          <property name="note" value="role_note_2"></property>
      </bean>
    
        <bean id="userRoleAssembly" class="com.liyafei.pojo.UserRoleAssemply">
            <property name="id" value="1"></property>
            <property name="list">
                <list>
                    <ref bean="role1"/>
                    <ref bean="role2"/>
                </list>
            </property>
            <property name="map">
                <map>
                    <entry key-ref="role1" value-ref="user1"></entry>
                    <entry key-ref="role2" value-ref="user2"></entry>
                </map>
            </property>
            <property name="set">
                <set>
                    <ref bean="role1"/>
                    <ref bean="role2"/>
                </set>
            </property>
        </bean>
    本博客为非营利性个人原创,除部分有明确署名的作品外,所刊登的所有作品的著作权均为本人所拥有,本人保留所有法定权利。违者必究
  • 相关阅读:
    一个爬虫的练习(妹子图)
    安装模块出现的编译问题(解决)
    基于套接字通信的简单练习(FTP)
    Python3 之选课系统
    数据爬取后台(PHP+Python)联合作战
    让pip 使用国内镜像源
    为啥学蛇和python10年后的变化
    模拟登陆百度以及Selenium 的基本用法
    冒泡排序及其效率分析(无聊搞来玩玩)
    LLVM编译器
  • 原文地址:https://www.cnblogs.com/liyafei/p/7975839.html
Copyright © 2020-2023  润新知