spring加载属性(properties)文件
一、注解方式加载
jdbc.driver=org.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://localhost:3306/kt
jdbc.user=root
jdbc.password=12345

创建配置类:
package com.wbg.springAnnotaion.config; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; @Configuration
//ignoreResourceNotFound 为false时候找不到会忽略
@PropertySource(value = {"classpath:database-config.properties"},ignoreResourceNotFound = true)
public class ApplicationConfig {
}

测试:
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
String url=context.getEnvironment().getProperty("jdbc.url");
System.out.println(url);
使用PropertySourcesPlaceholderConfigurer注解来占位符
1、在原来类上加代码
package com.wbg.springAnnotation.annotation.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration
@ComponentScan(basePackages = {"com.wbg.springAnnotation.annotation"})
@PropertySource(value = {"classpath:database-config.properties"},ignoreResourceNotFound = true)
public class ApplicationConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}

2、创建DataSourceBean类:
类上的@Value注解使用$占位符
package com.wbg.springAnnotation.annotation.config; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; @Component
public class DataSourceBean {
@Value("${jdbc.driver}")
private String driver = null;
@Value("${jdbc.url}")
private String url = null;
@Value("${jdbc.user}")
private String user = null;
@Value("${jdbc.password}")
private String password = null; @Override
public String toString() {
return "DataSourceBean{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", user='" + user + '\'' +
", password='" + password + '\'' +
'}';
} @Bean("dataSource")
public String getDataSourceBean(){
System.out.println(toString());
return this.toString();
}
}

测试:

二、使用xml进行加载:
创建xml文件:
database-config.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.wbg.springAnnotation.annotation"/>
<!--ignore-resource-not-found:true允许不存在,否则异常-->
<context:property-placeholder
ignore-resource-not-found="true"
location="classpath:database-config.properties"/>
</beans>

测试:

如果配置多个:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value> classpath:database-config.properties </value>
<value> classpath:log4j.properties </value>
</array>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>

测试:


demo:https://github.com/weibanggang/springannotationproperties.git
spring加载属性(properties)文件的更多相关文章
- 【转载】加密Spring加载的Properties文件
目标:要加密spring的jdbc配置文件的密码口令. 实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密. 1.扩展 package com.rail.comm; import j ...
- 解密Spring加载的Properties文件
Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key ...
- SpringMVC加载配置Properties文件的几种方式
最近开发的项目使用了SpringMVC的框架,用下来感觉SpringMVC的代码实现的非常优雅,功能也非常强大, 网上介绍Controller参数绑定.URL映射的文章都很多了,写这篇博客主要总结一下 ...
- 【Java Web开发学习】Spring加载外部properties配置文件
[Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- spring 加载属性(properties)文件
在开发的过程中,配置文件往往就是那些属性(properties)文件,比如使用properties文件配置数据库文件,又如database-config.properties 代码清单:databas ...
- spring学习 十六 spring加载属性文件
第一步:创建一个properties文件,以数据库链接作为实例db.properties jdbc.url=jdbc:mysql://192.168.153.128:3306/mybaties?cha ...
- spring加载属性配置文件内容
在spring中提供了一个专门加载文件的类PropertyPlaceholderConfigurer,通过这个类我们只需要给定需要加载文件的路径就可以 通过该类加载到项目,但是为了后面在程序中需要使用 ...
- Java实现动态加载读取properties文件
问题: 当我们使用如下语句加载.properties时: ClassLoader classLoader = this.getClass().getClassLoader(); Properties ...
随机推荐
- [javaEE] 三层架构案例-用户模块(二)
使用junit测试框架,测试查找用户和添加用户功能 com.tsh.test.xmlUserDaoTest package com.tsh.test; import org.junit.Test; i ...
- 9、springboot之处理静态资源
在springboot项目中的resource根目录下建立三个文件夹static.public.resources 里面都放同样名字的图片 但是图片内容不一样 启动springboot之后输入 htt ...
- 1、springboot之HelloWorld
最基本的,官网copy 创建maven项目 maven中添加 <parent> <groupId>org.springframework.boot</groupId> ...
- ZT 为什么Java中继承多数是有害的?
大多数好的设计者象躲避瘟疫一样来避免使用实现继承(extends 关系).实际上80%的代码应该完全用interfaces写,而不是通过extends.“Java设计模式”一书详细阐述了怎样用接口继承 ...
- PHP DES解密 对应Java SHA1PRNG方式加密
背景及问题 背景:在和外部系统通过HTTP方式跳转时, 为保障传输参数安全性, 采用AES 加密参数. 关于对称加密中 AES, DES, CBC, ECB, PKCS5Padding 概念可参考ht ...
- setInterval 与 clearInterval详解
首先注意,setInterval与clearInterval都是直属于window对象的. 1.直接调用setInterval(即不通过函数调用) <div id="oDiv_show ...
- js 闪动元素
<style> #div1{width:500px;height:100px;background:#888;font-size:5px;margin:0 auto;color:yello ...
- C#子窗体闪烁问题解决
在父窗体随意为之添加 protected override CreateParams CreateParams { get { XtraForm xa = _App.Framework.MainFor ...
- Apache服务器运维笔记(5)----容器的处理顺序
容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的. ...
- c++开发ocx入门实践二
原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/51374355 IDE:vs2010,c++,测试工具,vs自带的TstCo ...