Spring Bean 枚举属性注入

1、名称空间配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

2、JAVA

package com.demo.bean;

class TestBean {

    private Gender gender

    public void setGender(Gender gender){
        this.gender = gender;
    }

    public Gender getGender(){
        return gender;
    }

}

package com.demo.utils;

public enum Gender {
    MALE, FEMALE, UNSPECIFIED
}

3、Bean 配置

<bean id="testBean" class="com.demo.bean.TestBean">
    <property name="gender" ref="gender" />
</bean>
<util:constant id="gender" static-field="com.demo.utils.MALE" />




你可能感兴趣的:(Spring Bean 枚举属性注入)