`
knight_black_bob
  • 浏览: 821375 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

(三种方式 获取 properties)自定义PropertyPlaceholderConfigurer 获取properties值 spring

阅读更多

 

 第一种方式

一句代码解决 constants.properties 

jar 是 java 自带的 rj.jar

public static String getTextByCode(String code){
		
		ResourceBundle bundle = ResourceBundle.getBundle("constants");
		
		return bundle.getString(code);
		
	}

 

 

第二种方式:

<dependency>
    <groupId>com.typesafe</groupId>
    <artifactId>config</artifactId>
    <version>1.2.0</version>
</dependency>

 

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2016年11月2日 下午1:54:13
 * desc:
 */
public class ConfigUtil {

    public static ConfigUtil instance = new ConfigUtil();

    private Config mConfig;

    private ConfigUtil() {
        mConfig = ConfigFactory.parseResources("config.properties");
    }

    public String getStr(String key) {
        return instance.mConfig.getString(key);
    }
}

 

 

 

 第三种方式

 user_tag_template.properties

000=000,\u6240\u6709\u7528\u6237,001.png
001=001,\u6D41\u5931\u7528\u6237,002.png

 

 applicationContext.xml

<bean id="propertyConfigurer" class="com.fusionability.web.utils.UserTagConfigurer">
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath:user_tag_template.properties</value>
			</list>
		</property>
	</bean>
    

 

 

package com.fusionability.web.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.fusionability.web.bean.UserTagKeyAndValue;

/**
 * @author baoyou E-mail:curiousby@163.com
 * @version 创建时间:2015年8月10日 上午11:37:19 des:
 */
public class UserTagConfigurer extends PropertyPlaceholderConfigurer {
	private static Logger logger = Logger.getLogger(UserTagConfigurer.class);
	
	private static Map<String,UserTagKeyAndValue> ctxPropertiesMap;

	@Override
	protected void processProperties( ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		ctxPropertiesMap = new HashMap<String,UserTagKeyAndValue>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			if(value != null && value != "" && value.contains(",") ) {
				UserTagKeyAndValue utkav =  new UserTagKeyAndValue();
				utkav.setCode(value.split(",")[0]);
				utkav.setName(value.split(",")[1]);
				utkav.setPic(value.split(",")[2]);
				ctxPropertiesMap.put(keyStr, utkav);
			}  
			
		}
		logger.info(" ctxPropertiesMap.size() "+ctxPropertiesMap.size());
	}

	public static UserTagKeyAndValue getContextProperty(String name) {
		return ctxPropertiesMap.get(name);
	} 
	
	public  static Map<String,UserTagKeyAndValue> getConfigurerMap (){
         return ctxPropertiesMap;
	}
	
}

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

分享到:
评论

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    5.4.1. 设置和获取属性值以及嵌套属性 5.4.2. 内建的PropertyEditor实现 5.4.2.1. 注册用户自定义的PropertyEditor 6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 ...

    Spring MVC 入门实例

    如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring MVC 的基本概念. 基于 Spring 的 Web 应用...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    #连接超时时间阀值,获取连接时,超出阀值时间,则获取失败,毫秒为单位 bonecp.connectionTimeout = 10000 #连接池助手线程数量,可设置为0,该参数会降低运行速度,但程序有大量连接时,有助于提升高并发程序...

    spring3.2+strut2+hibernate4

    spring3.2+strut2+hibernate4 注解方式。 spring.xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;WEB-INF/jdbc.properties class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-...

    SPRING API 2.0.CHM

    PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    ssh框架在application.xml中配置数据源所需jar

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:/deploy.properties &lt;!-- 配置sessionFactory ...

    SpringEnvironmentsAndProfiles:研究如何使用Spring管理平台特定的属性文件

    什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    SSI框架整合实例

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;value&gt;classpath:jdbc.properties ${driver}"/&gt; ${url}"/&gt; ${username}"/&gt; ${password}"/&gt; &lt;!-- 配置...

    Red5 0.9与现有tomcat项目整合

    &lt;bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="/WEB-INF/red5-web.properties" /&gt; &lt;!-- ROOT web ...

    springmvcmybatis

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:jdbc.properties" /&gt; &lt;!-- ${jdbc.driver}" /&gt; ${jdbc.url}" /&gt; $...

Global site tag (gtag.js) - Google Analytics