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 ...
随机推荐
- 小白学flask之静态文件
引入css的方式有两种 1 那在flask中,如何处理静态文件? 做法很简单,只要在你的包或模块旁边创建一个名为 static 的文件夹就行了. flask的静态文件是位于应用的 /static 中的
- ccf-201709-2 公共钥匙盒
问题描述 有一个学校的老师共用N个教室,按照规定,所有的钥匙都必须放在公共钥匙盒里,老师不能带钥匙回家.每次老师上课前,都从公共钥匙盒里找到自己上课的教室的钥匙去开门,上完课后,再将钥匙放回到钥匙盒中 ...
- mockito測試框架
1. code package com.springinaction.knights; import static org.mockito.Mockito.*; import org.junit.Te ...
- webpack简单学习的入门教程
前言,如果按照官网的安装办法: npm install webpack -g 安装的是最新版的,然后就莫名其妙的有问题(可以安装,但运行有问题).我是小白,我也不知道具体原因,所以我换成2.5.1版本 ...
- 学习CSS制作菜单列表,举一反三
1.普通的二三级菜单 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...
- OpenCV中Mat属性step,size,step1,elemSize,elemSize1
Mat的step,size,step1,elemSize,elemSize1这几个属性非常容易混淆. OpenCV的官方参考手册也没有解释清楚这几个概念. 前一段时间研究了一下每个属性的含义,如果有什 ...
- 02_dubbo实例_多版本号
[多版本作用] 当一个接口实现,出现不兼容升级时,可以用版本号过渡. 版本号不同的服务之间不能引用. [版本迁移方式] 1.在低压时间段,先升级一半Provider为新版本. 2.再将所有消费者升级为 ...
- _itoa atoi、atof、itoa、itow _itoa_s 类型转换使用说明
原文:http://www.cnblogs.com/lidabo/archive/2012/07/10/2584706.html _itoa 功能:把一整数转换为字符串 用法:char * _itoa ...
- 【Leetcode】【Easy】Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- Android-删除指定包名的App
/** * check and delete the old package app if it exists. */ private void checkOldPackage() { String ...