100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 用 @Value(“${xxxx}“)注解从配置文件读取值的用法

用 @Value(“${xxxx}“)注解从配置文件读取值的用法

时间:2022-10-06 04:21:53

相关推荐

用 @Value(“${xxxx}“)注解从配置文件读取值的用法

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

1. 用法:

从配置properties文件中读取init.password 的值。

@Value("${init.password}")private String initPwd;

2. 在spring的配置文件中加载配置文件dbconfig.properties :

<!-- 加载配置文件 --><bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="fileEncoding" value="UTF-8"/><property name="locations"><list><value>classpath:dbconfig.properties</value></list></property></bean>

或者这样加载:

<context:property-placeholder location="classpath:dbconfig.properties" />

或者这样加载:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"><property name="location"><value>dbconfig.properties</value></property></bean>

3.dbconfig.properties 文件:

#MD5password.algorithmName=md5password.hashIterations=2#initpwdinit.password=admin

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。