//定义枚举类型
public enum ReportType {
MONTH,WEEK,DAY
}
//使用枚举类型
public class ReportJob {
private ReportType reportType;
}
//spring配置文件注入
<bean id="DAY" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.test.ReportType.DAY" />
</bean>
<bean id="dayReportJob" class="com.test.ReportJob">
<property name="reportType" ref="DAY" />
</bean>
注意:
枚举类型要想注入到类中,一定要先使用org.springframework.beans.factory.config.FieldRetrievingFactoryBean类将枚举类型进行转换,即
<bean id="DAY" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.test.ReportType.DAY" />
</bean>将ReportType.Day转换为DAY这个bean,然后在要注入的bean中使用<property name="reportType" ref="DAY" />引用即可。
枚举类型在Spring中的注入与使用
本文详细解释了如何在Spring框架中通过org.springframework.beans.factory.config.FieldRetrievingFactoryBean将枚举类型转换为bean,并成功注入到类中。通过实例演示了从枚举类型Day到实际bean DAY的转换过程,以及如何在类中引用注入的枚举类型实例。
1337

被折叠的 条评论
为什么被折叠?



