spring注入配置文件属性到java类
更新时间:2023年07月20日 11:36:00 作者:毛宇鹏
这篇文章主要为大家介绍了spring注入配置文件属性到java类实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
引言
在许多时候,我们需要把一些全局的参数配置到配置文件里面,提供给java程序使用,为了减少代码量及高阅读性,理想的是把我们所需要的全局属性注入到类里面,由程序代码直接引用.
普通引入properties方法
在spring的配置文件applicationContext.xml配置
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>改进后的properties引入方法
在spring的配置文件applicationContext.xml配置
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"></property>
</bean>application.properties文件配置内容
# 默认头像 userDefaultHeaderUrl=http://www.maoyupeng.com/Male.png
java类的使用示例
@Controller
@RequestMapping(value = "/userController")
public class userController {
private static final Logger logger = Logger.getLogger(UserProjectController.class);
@Value("#{configProperties['userDefaultHeaderUrl']}")
private String userDefaultHeaderUrl;
}以上就是spring注入配置文件属性到java类的详细内容,更多关于spring注入配置文件属性到java类的资料请关注脚本之家其它相关文章!
相关文章
Java restTemplate发送get请求query参数传递问题解决
这篇文章主要为大家介绍了Java restTemplate发送get请求query参数传递问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-11-11
MyBatisPlus TypeHandler自定义字段类型转换Handler
这篇文章主要为大家介绍了MyBatisPlus TypeHandler自定义字段类型转换Handler示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-08-08
Springboot +redis+谷歌开源Kaptcha实现图片验证码功能
这篇文章主要介绍了Springboot +redis+⾕歌开源Kaptcha实现图片验证码功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-01-01


最新评论