67,517
社区成员
发帖
与我相关
我的任务
分享
package com.commons;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
public class PropertiesUtils {
private static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
private static final String DEFAULT_RESOURCE_PATTERN = "classpath:*-tag.properties";
private static final Map<Object, Object> all = new HashMap<Object, Object>();
public static Map<Object, Object> getAll() {
return all;
}
static {
try {
Resource[] resources = resourcePatternResolver
.getResources(DEFAULT_RESOURCE_PATTERN);
if (resources != null) {
for (Resource r : resources) {
Reader in = new InputStreamReader(r.getInputStream(),
"UTF-8");
Properties p = new Properties();
p.load(in);
all.putAll(p);
in.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
test.userName=121313132132
test.password=after-encrypt:pppppppppppp
package com.zhangyz.www.spring;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class MinePropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private String encrypt="after-encrypt:";
private static Logger logger = Logger
.getLogger(MinePropertyPlaceholderConfigurer.class.getName());
protected String convertPropertyValue(String originalValue) {
if(originalValue!=null&&originalValue.length()>encrypt.length()&&originalValue.indexOf(encrypt)>=0){
return originalValue.substring(encrypt.length());
}else{
return originalValue;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="" class="com.zhangyz.www.spring.MinePropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:com/zhangyz/www/spring/jdbc.properties</value>
</property>
</bean>
<bean name="test" class="com.zhangyz.www.spring.Test">
<property name="userName" value="${test.userName}">
</property>
<property name="password" value="${test.password}">
</property>
</bean>
</beans>
package com.zhangyz.www.spring;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private static Logger logger = Logger.getLogger(Test.class.getName());
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"/com/zhangyz/www/spring/application.xml");
Test test=(Test) applicationContext.getBean("test");
logger.error(test.getUserName());
logger.error(test.getPassword());
}
}
package com.zhangyz.www.spring;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
public class MinePropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private static Logger logger = Logger
.getLogger(MinePropertyPlaceholderConfigurer.class.getName());
protected String convertPropertyValue(String originalValue) {
return originalValue+"哥哥来解密!";
}
}
test.userName=121313132132
test.password=pppppppppppp