欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring的pojo类属性的注入

程序员文章站 2022-05-29 15:13:01
...

spring的pojo类属性的注入

1、写一个类,它有各种类型的属性,将属性的get,set方法补全:

package com.sky.spring.sysmanage.entity;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * pojo对象常见属性的注入
 * @author Administrator
 *
 */
public class User {

    private String strValue;
    private int intValue;
    private List listValue;
    private Set setValue;
    private String[] strArrayValue;
    private Map mapValue;

    public String getStrValue() {
        return strValue;
    }
    public void setStrValue(String strValue) {
        this.strValue = strValue;
    }
    public int getIntValue() {
        return intValue;
    }
    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }
    public List getListValue() {
        return listValue;
    }
    public void setListValue(List listValue) {
        this.listValue = listValue;
    }
    public Set getSetValue() {
        return setValue;
    }
    public void setSetValue(Set setValue) {
        this.setValue = setValue;
    }
    public String[] getStrArrayValue() {
        return strArrayValue;
    }
    public void setStrArrayValue(String[] strArrayValue) {
        this.strArrayValue = strArrayValue;
    }
    public Map getMapValue() {
        return mapValue;
    }
    public void setMapValue(Map mapValue) {
        this.mapValue = mapValue;
    }




}

2、在spring容易中实现该类的属性注入

<bean class="com.sky.spring.sysmanage.entity.User" id="user">
        <property name="strValue" value="this is stringVal">
        </property>
        <property name="intValue" value="123">
        </property>

        <property name="listValue">
            <list>
                <value>listValue1</value>
                <value>listValue2</value>
            </list>
        </property>

         <property name="setValue">
                <set>
                    <value>set1</value>
                    <value>set2</value>
                    <value>set3</value>
                </set>
         </property>
         <property name="strArrayValue">
            <list>
                <value>strArray1</value>
                <value>strArray1</value>
                <value>strArray1</value>
            </list>
         </property>
         <property name="mapValue">
            <map>
                <entry key="key1" value= "map1"></entry>
                <entry key="key2" value= "map2"></entry>
                <entry key="key3" value= "map3"></entry>
            </map>
         </property>     
   </bean>      
</bean>

spring的pojo类属性的注入