本文主要是介绍Spring依赖注入枚举值、常量,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- 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
这篇关于Spring依赖注入枚举值、常量的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!