1.Properties文件的内容
dbName=sampledb driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/${dbName} #userName=root #password=1234 userName=WnplV/ietfQ= password=gJQ9O+q34qk\=
2.Spring配置
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!--1.使用传统的PropertyPlaceholderConfigurer引用属性文件 --> <!-- bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:fileEncoding="utf-8"> <property name="locations"> <list> <value>classpath:com/baobaotao/placeholder/jdbc.properties</value> </list> </property> </bean --> <!--2.使用context命名空间的配置引用属性文件 --> <!--context:property-placeholder location="classpath:com/baobaotao/placeholder/jdbc.properties" file-encoding="utf8"/> <bean id="utf8" class="java.lang.String"> <constructor-arg value="utf-8"></constructor-arg> </bean--> <!--3.使用加密版的属性文件 --> <bean class="com.baobaotao.placeholder.EncryptPropertyPlaceholderConfigurer" p:location="classpath:com/baobaotao/placeholder/jdbc.properties" p:fileEncoding="utf-8"/> <context:component-scan base-package="com.baobaotao.placeholder"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${driverClassName}" p:url="${url}" p:username="${userName}" p:password="${password}" /> </beans>
3.加密的java Class文件EncryptPropertyPlaceholderConfigurer.java
package com.baobaotao.placeholder; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String[] encryptPropNames ={"userName","password"}; @Override protected String convertProperty(String propertyName, String propertyValue) { if(isEncryptProp(propertyName)){ String decryptValue = DESUtils.getDecryptString(propertyValue); System.out.println(decryptValue); return decryptValue; }else{ return propertyValue; } } /** * 判断是否是加密的属性 * @param propertyName * @return */ private boolean isEncryptProp(String propertyName){ for(String encryptPropName:encryptPropNames){ if(encryptPropName.equals(propertyName)){ return true; } } return false; } }
使用到的加密工具DESUtils.java
package com.baobaotao.placeholder; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class DESUtils { private static Key key; private static String KEY_STR = "myKey"; static { try { KeyGenerator generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom(KEY_STR.getBytes())); key = generator.generateKey(); generator = null; } catch (Exception e) { throw new RuntimeException(e); } } /** * 对str进行DES加密 * * @param str * @return */ public static String getEncryptString(String str) { BASE64Encoder base64en = new BASE64Encoder(); try { byte[] strBytes = str.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptStrBytes = cipher.doFinal(strBytes); return base64en.encode(encryptStrBytes); } catch (Exception e) { throw new RuntimeException(e); } } /** * 对str进行DES解密 * * @param str * @return */ public static String getDecryptString(String str) { BASE64Decoder base64De = new BASE64Decoder(); try { byte[] strBytes = base64De.decodeBuffer(str); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptStrBytes = cipher.doFinal(strBytes); return new String(decryptStrBytes, "UTF8"); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception { // if (args == null || args.length < 1) { // System.out.println("请输入要加密的字符,用空格分隔."); // } else { // for (String arg : args) { // System.out.println(arg + ":" + getEncryptString(arg)); // } // } System.out.println(getDecryptString("WnplV/ietfQ=")); System.out.println(getDecryptString("gJQ9O+q34qk=")); } }
4.在基于注解的java类中使用属性
package com.baobaotao.placeholder; import org.apache.commons.lang.builder.ToStringBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; @Component public class MyDataSource { @Value("${driverClassName}") private String driverClassName; @Value("${url}") private String url; @Value("${userName}") private String userName; @Value("${password}") private String password; public String toString(){ return ToStringBuilder.reflectionToString(this); } }
相关推荐
Spring 读取properties文件key+value方式.rar
spring读取properties.
Spring3.0 配置文件中加载Properties文件的小例子
Spring 自定义注解注入properties文件的值jar包,下面为使用方法 在xml配置文件中,这样加载properties文件 ...
spring4.0框架引用系统properties文件的实例
SpringBoot2.x properties全部配置文件总计1500+,每个配置文件写有中文解释。适用于系统学习springboot 深入学习springboot的同学。
这是一个简单的案例用来实现Spring中jdbc.properties属性文件进行密文处理,代码中的学生实体自己根据需要在数据库中进行创建
本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring读取properties配置文件的key值。
主要介绍了spring无法读取properties文件数据问题详解,需要的朋友可以参考下
NULL 博文链接:https://nannan408.iteye.com/blog/2256177
主要为大家详细介绍了Spring加载properties文件的两种方法,一是通过xml方式,另一种方式是通过注解方式,感兴趣的小伙伴们可以参考一下
主要介绍了Spring Boot的properties配置文件读取,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
spring-mvc.xml spring-mybatis.xml web.xml log4j.properties,项目中需要用到的配置文件。直接可用。
本套配置本人亲测可用 里面包含了 jedis2.9.0.jar 最新redis发布的jar...redis.properties redis的配置项,ip一级连接池参数,注意:redis默认没有密码,所以我的spring配置文件把密码注释掉了,如果有密码放开注释即可
主要介绍了Spring中属性文件properties的读取与使用详解的相关资料,需要的朋友可以参考下
主要介绍了Spring加载properties文件的两种方式,需要的朋友可以参考下
主要介绍了详解SpringMVC加载配置Properties文件的几种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
config.properties:数据库配置文件 log4j.properties:mybatis日志文件 spring-mvc.xml:spring-MVC配置文件 spring-mybatis.xml:mybatis的配置文件 spring.xml
主要介绍了Spring用代码来读取properties文件实例解析,具有一定借鉴价值,需要的朋友可以参考下