Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中 
一些动态设定的值(value),在XML中替换为占位该键($key$)的值, 
.properties文件可以根据客户需求,自定义一些相关的参数,这样的设计可提供程序的灵活性。

xml中的配置文件

<bean id="propertyConfigurer"
class="com.datasure.util.filter.EncryptablePropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:jdbc.properties</value></list>
</property>
</bean>
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static final String key = "helloworld"; protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
Des des = new Des();
String username = props.getProperty("jdbc.username");
if (username != null) {
props.setProperty("jdbc.username", des.Decrypt(username, des.hex2byte(key)));
} String password = props.getProperty("jdbc.password");
if (password != null) {
props.setProperty("jdbc.password", des.Decrypt(password, des.hex2byte(key)));
} String url = props.getProperty("jdbc.url");
if (url != null) {
props.setProperty("jdbc.url", des.Decrypt(url, des.hex2byte(key)));
} String driverClassName = props.getProperty("jdbc.driverClassName");
if(driverClassName != null){
props.setProperty("jdbc.driverClassName", des.Decrypt(driverClassName, des.hex2byte(key)));
} super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}

通过如上配置就可以对配置文件进行解密操作

解密Spring加载的Properties文件的更多相关文章

  1. 【转载】加密Spring加载的Properties文件

    目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...

  2. SpringMVC加载配置Properties文件的几种方式

    最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...

  3. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  4. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  5. Java实现动态加载读取properties文件

    问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties ...

  6. spring加载hibernate映射文件的几种方式。转自:http://blog.csdn.net/huiwenjie168/article/details/7013618

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessionF ...

  7. java web项目启动时自动加载自定义properties文件

    首先创建一个类 public class ContextInitListener implements ServletContextListener 使得该类成为一个监听器.用于监听整个容器生命周期的 ...

  8. spring加载hibernate映射文件的几种方式 (转)

    在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个 Bean实例中进行的,若配置的映射文件较少时,可以用session ...

  9. spring加载hibernate映射文件的几种方式

    1. 在Spring的applicationContext.xml中配置映射文件,通常是在<sessionFactory>这个Bean实例中进行的,若配置的映射文件较少时,可以用sessi ...

随机推荐

  1. BJFU 1551 ——delightful world——————【暴搜】

    delightful world 时间限制(C/C++):20000MS/30000MS          运行内存限制:65536KByte总提交:33            测试通过:10 描述 ...

  2. bzoj 4771: 七彩树

    Description 给定一棵n个点的有根树,编号依次为1到n,其中1号点是根节点.每个节点都被染上了某一种颜色,其中第i个节 点的颜色为c[i].如果c[i]=c[j],那么我们认为点i和点j拥有 ...

  3. c#做的查找文件夹内内容的小工具

    第一次写博客有点激动啊QAQ 来新单位,一直没活干,公司代码控制器太多,其中有很多文件夹,每次找一个控制器都老找不到,我又不愿意用VS的全局搜索,想着没事就做了个查找控制器的小工具.代码如下: 先添加 ...

  4. Observer(观察者)设计模式[转]

    Observer设计模式中主要包括如下两类对象: Subject:监视对象,它往往包含着其他对象所感兴趣的内容.在本范例中,热水器就是一个监视对象,它包含的其他对象所感兴趣的内容,就是tempratu ...

  5. Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配

    1 解析 1.1 什么是泛型? 1.2 泛型有什么作用? 1.3 泛型装配样式? 2 代码演练 2.1 泛型应用 1 解析 1.1 什么是泛型? Java泛型设计原则:只要在编译时期没有出现警告,那么 ...

  6. 001Angular2环境准备

    01.安装Node.js 和 npm 安装完成后,在命令行依次使用node -v 和 npm -v可以看到版本号. 02.安装cnpm npm需要连接谷歌服务器,国内不能正常使用,需安装cnpm.命令 ...

  7. DrawerLayout侧滑

    DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说DrawerLayout是因为第三方控件如SlidingMenu等出现之后,google借鉴而出现的产物.D ...

  8. qt打开url

    QDesktopServices::openUrl(QUrl(QLatin1String(“http://blog.const.net.cn“)))

  9. Qt之QSS(Q_PROPERTY-自定义属性)

    版权声明:进步始于交流,收获源于分享!纯正开源之美,有趣.好玩.靠谱...作者:一去丶二三里 博客地址:http://blog.csdn.net/liang19890820   目录(?)[+]   ...

  10. mongodb 3.4 学习 (二)命令

    # 使用或切换数据库 use <database name> # 显示所有数据库 show dbs # 显示所有collection show collections # 显示所有user ...