使用spring加载properties文件
发布日期:2022-02-09 20:39:06 浏览次数:4 分类:技术文章

本文共 2151 字,大约阅读时间需要 7 分钟。

[list]
[*]在spring的配置文件中,配置如下:
[/list]
classpath:sysconf.properties
classpath:db-config.properties
classpath:security-config.properties
classpath:source.properties
[list]
[*]com.common.PropertiesBean实现此类
[/list]
package com.common; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import com.util.LifeCycleManage; public class PropertiesBean extends PropertyPlaceholderConfigurer {
private static final Logger LOG = LoggerFactory.getLogger(PropertiesBean.class); @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
// LifeCycleManage 需要自己实现,单例模式 LifeCycleManage.setProperty(props); super.processProperties(beanFactoryToProcess, props); } }
[list]
[*]添加一个单例模式的读取配置累
[/list]
package com.util; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LifeCycleManage {
private static final Logger LOGGER = LoggerFactory.getLogger(LifeCycleManage.class); private static Properties propertyConfigurer; public static void setProperty(Properties ipropertyConfigurer) {
propertyConfigurer = ipropertyConfigurer; } public static String getProperty(String proName) {
String str = ""; if (propertyConfigurer == null) {
return str; } str = propertyConfigurer.getProperty(proName); return str; } }
[list]
[*]然后就可以到java类中、jsp的小脚本中使用了
[/list]
String servicename = LifeCycleManage.getProperty( "servicename");
<%@page import="com.util.LifeCycleManage" %> <%     String servicename = LifeCycleManage.getProperty( "servicename"); 	String version = LifeCycleManage.getProperty( "version"); %>

转载地址:https://blog.csdn.net/iteye_12884/article/details/82673550 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:hutool-poi
下一篇:httpclient4.5调用接口

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月19日 10时39分53秒