- Spring依赖注入枚举值、常量
- 构造器注入、Setter注入效果相同
- 利用FieldRetrievingFactoryBean实现
- xml配置方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="demoTimeUnit1" class="test.DemoTimeUnit">
<property name="time" value="60"/>
<!-- 注入枚举值 -->
<property name="timeUnit">
<bean id="java.util.concurrent.TimeUnit.SECONDS"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" />
</property>
</bean>
<bean id="demoTimeUnit2" class="test.DemoTimeUnit">
<property name="time" value="1"/>
<!-- 注入枚举值 -->
<property name="timeUnit">
<util:constant static-field="java.util.concurrent.TimeUnit.MINUTES"/>
</property>
</bean>
</beans>
- 执行效果
- 详细说明查见Spring文档
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#xsd-schemas-util-constant