Spring @Value用法

it2022-05-05  187

Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。

使用方式必须在当前类使用@Component,xml文件内配置的是通过pakage扫描方式例如:<context:component-scan base-package="pakage_name" /> 2.

Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码。

1、在applicationContext.xml文件中配置properties文件

<bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <value>classpath:app.properties</value> </array> </property> </bean>

2、在bean中使用@value注解获取配置文件的值

@Value("${chengmi_crawl_timer_enable}") private Boolean timerEnabled;

即使给变量赋了初值也会以配置文件的值为准。

转载于:https://www.cnblogs.com/panxuejun/p/6116802.html


最新回复(0)