前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >读取重写spring读取配置文件

读取重写spring读取配置文件

原创
作者头像
用户8671053
修改2021-09-26 17:40:10
修改2021-09-26 17:40:10
1.3K00
代码可运行
举报
文章被收录于专栏:码农的生活码农的生活
运行总次数:0
代码可运行

[Java]代码    

代码语言:javascript
代码运行次数:0
运行
复制
package com.templet.spring;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.core.io.Resource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;

public class DecryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private Resource[] locations;

    public void setLocations(Resource[] locations) {
        this.locations = locations;
    }

    public void loadProperties(Properties props) throws IOException {
        if (this.locations != null) {
            PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
            for (int i = 0; i < this.locations.length; i++) {
                Resource location = this.locations[i];
                if (logger.isInfoEnabled()) {
                    logger.info("Loading properties file from " + location);
                }
                InputStream is = null;
                try {
                    is = location.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    StringBuffer buffer = new StringBuffer();
                    String line = "";
                    while ((line = in.readLine()) != null) {
                        buffer.append(line);
                    }
                    String rs="";
                    if("dbconn.properties".equals(location.getFilename())){
                        DESUtil des = new DESUtil("1234567");
                        rs=des.decryptStr(buffer.toString());
                    }else{
                        rs =buffer.toString();
                    }
                    propertiesPersister.load(props, new ByteArrayInputStream(rs.getBytes()));
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            }
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [Java]代码    
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档