IDEA社区版搭建Spring工程(04-加载配置文件及加解密)
Spring MVC 加载配置文件的几种方式
- 通过 context:property-placeholde 实现加载配置文件
在 springmvc.xml 配置文件里加入 context 相关引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
引入config配置文件 <context:property-placeholder location="classpath:config.properties"/>
- 通过context:property-placeholde加载多个配置文件,只需将多个配置文件以逗号分隔即可。
config配置文件内容如下:
hello.content=hello,everyone.
在Java类中调用
@Value("${hello.content}")
private String content;
- 通过util:properties实现配置文件加载
在springmvc.xml中加入util相关引用
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
引入config配置文件 <util:properties id="settings" location="classpath:config.properties"/>
在Java类中调用
@Value("#{settings['hello.content']}")
private String content;
- 直接在Java类中通过注解实现配置文件加载
在java类中引入配置文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(value="classpath:config.properties")
public class Config {
@Value("${hello.content}")
public String content;
}
在springmvc配置文件里添加
<context:component-scan base-package="springmvc"/>
<!-- 配置springMVC开启注解支持 -->
<mvc:annotation-driven/>
在controller中调用
@Autowired
private Config Config; //引用统一的参数配置类
@RequestMapping(value={"/hello"})
public ModelMap test(ModelMap modelMap) {
modelMap.put("message", Config.content);
return modelMap;
配置文件加解密
这里使用了阿里巴巴 Druid 实现加解密
- 添加依赖
<druid.version>1.2.11</druid.version>
<commons-io.version>2.8.0</commons-io.version>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
- 新建配置类及工具类
Global类
package springmvc.config;
import springmvc.security.CodeUtils;
import springmvc.utils.PropertiesLoader;
public class Global {
/**
* 属性文件加载对象
*/
private static PropertiesLoader loader = new PropertiesLoader("config.properties");
/**
* 获取配置
* @param key
* @return
*/
public static String getConfig(String key) {
String value = loader.getProperty(key);
if (key.startsWith("encode")) {
value = CodeUtils.decode(value);
}
return value;
}
}
CodeUtils类
package springmvc.security;
import com.alibaba.druid.filter.config.ConfigTools;
/**
* DES加密处理工具类.主要用来对数据进行DES加密/解密操作
*
*/
public class CodeUtils {
public static String encode(String str) {
String encodeStr = null;
try {
encodeStr = ConfigTools.encrypt(str);
} catch (Exception e) {
e.printStackTrace();
}
return encodeStr;
}
public static String decode(String str) {
String decodeStr = null;
try {
decodeStr = ConfigTools.decrypt(str);
} catch (Exception e) {
e.printStackTrace();
}
return decodeStr;
}
public static void main(String[] args) {
String value = "hello,everyone.";
System.out.println(encode(value));
}
}
PropertiesLoader类
package springmvc.utils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties;
/**
* Properties文件载入工具类. 可载入多个properties文件, 相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
* @author calvin
* @version 2013-05-15
*/
public class PropertiesLoader {
private static ResourceLoader resourceLoader = new DefaultResourceLoader();
private final Properties properties;
public PropertiesLoader(String... resourcesPaths) {
properties = loadProperties(resourcesPaths);
}
/**
* 取出Property,但以System的Property优先,取不到返回空字符串.
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
if (properties.containsKey(key)) {
return properties.getProperty(key);
}
return "";
}
/**
* 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
}
/**
* 载入多个文件, 文件路径使用Spring Resource格式.
*/
private Properties loadProperties(String... resourcesPaths) {
Properties props = new Properties();
for (String location : resourcesPaths) {
InputStream is = null;
try {
Resource resource = resourceLoader.getResource(location);
is = resource.getInputStream();
props.load(is);
} catch (IOException ex) {
} finally {
IOUtils.closeQuietly(is);
}
}
return props;
}
}
文件位置截图
修改配置文件,属性名改为encode.
开头,属性值改为加密值
encode.hello.content=Mh0g4Dn5+iuhkcnEcCx2l1Tt92vvo+b97nJmV/5c4rKdQUefifpRGKoRKUqXdLCU3FPwT0xQ6vgFSEEr0glQZA==
在java类中使用配置信息
private static final String content= Global.getConfig("encode.hello.content");
大功告成。
生生不息