Spring详解(十)加载配置文件
在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源码。下面讨论spring两种加载方式,基于xml和基于注解的加载方式。
1. 通过xml方式加载properties文件
以Spring实例化dataSource为例,先在工程目录的src下新建一个conn.properties文件,里面写上上面dataSource的配置:
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/shop
username=root
password=root
然后在只需要在beans.xml中做如下修改即可:
<context:property-placeholder location="classpath:conn.properties"/><!-- 加载配置文件 --> <!-- com.mchange.v2.c3p0.ComboPooledDataSource类在c3p0-0.9.5.1.jar包的com.mchange.v2.c3p0包中 -->
<bean id="dataSource" class="${dataSource}"> <!-- 这些配置Spring在启动时会去conn.properties中找 -->
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
</bean>
标签也可以用下面的标签来代替,可读性更强:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->
<array>
<value>classpath:conf.properties</value>
</array>
</property>
</bean>
2. 通过注解方式加载properties文件
首先新建一个资源文件:public.properties
shop.url=http://magic/shop
第一种配置方式:
<!-- 确保可在@Value中, 使用SeEL表达式获取资源属性 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config/*.properties</value>
</list>
</property>
</bean>
在java代码中用@Value获取配置属性值
@Value("${shop.url}")
private String url;
还有一种方式更简洁:
<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="fileEncoding" value="UTF-8"/>
<property name="locations"><!-- 这里是PropertiesFactoryBean类,它也有个locations属性,也是接收一个数组,跟上面一样 -->
<array>
<value>classpath:public.properties</value>
</array>
</property>
</bean>
<!--或者-->
<context:property-placeholder location="classpath:public.properties" /> //注意,这种表达式要有set方法才能被注入进来,注解写在set方法上即可
private String url;
@Value("#{prop.shop.url}")
//@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=shop.url的对应的value值
public void setUrl(String url) {
this.token= url;
}
3.通过 @PropertySource和@Value 来读取配置文件
@Component
@PropertySource(value = {"classpath:common.properties", "classpath:abc.properties"})
public class Configs { @Value("${connect.api.apiKeyId}")
public String apiKeyId; @Value("${connect.api.secretApiKey}")
public String secretApiKey; public String getApiKeyId() {
return apiKeyId;
} public String getSecretApiKey() {
return secretApiKey;
}
}
我们来具体分析下:
1、@Component注解说明这是一个普通的bean,在Component Scanning时会被扫描到并被注入到Bean容器中;我们可以在其它引用此类的地方进行自动装配。@Autowired这个注解表示对这个bean进行自动装配。 比如:
@Controller
public class HomeController { @Autowired
private Configs configs;
}
2、@PropertySource注解用来指定要读取的配置文件的路径从而读取这些配置文件,可以同时指定多个配置文件;
3、@Value("${connect.api.apiKeyId}")用来读取属性key=connect.api.apiKeyId所对应的值并把值赋值给属性apiKeyId;
在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到properties文件中,在源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源码。下面讨论spring两种加载方式,基于xml和基于注解的加载方式。
Spring详解(十)加载配置文件的更多相关文章
- 详解ListView加载网络图片的优化,让你轻松掌握!
详解ListView加载网络图片的优化,让你轻松掌握! 写博客辛苦了,转载的朋友请标明出处哦,finddreams(http://blog.csdn.net/finddreams/article/de ...
- 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)
目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- linux modprobe命令参数及用法详解--linux加载模块命令
转:http://www.linuxso.com/command/modprobe.html modprobe(module probe) 功能说明:自动处理可载入模块. 语 法:modprobe [ ...
- Spring的 classpath 通配符加载配置文件
http://www.cnblogs.com/taven/archive/2012/10/24/2737556.html classpath:app-Beans.xml 说明:无通配符,必须完全匹配 ...
- 详解Class加载过程
1.Class文件内容格式 2.一个class文件是被加载到内存的过程是怎样的? loading 把一个class文件装到内存里,class文件是一个二进制,一个个的字节 linking Verifi ...
- 详解ListView加载网络图片的优化
我们来了解一些ListView在加载大量网络图片的时候存在的常见问题: 1.性能问题,ListView的滑动有卡顿,不流畅,造成非常糟糕的用户体验. 2.图片的错位问题. 3.图片太大,加载Bitma ...
- Google推荐——Glide使用详解(图片加载框架)
零.前言 本文所使用的Glide版本为3.7.0 一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的goo ...
- spring boot 使用 EnvironmentAware 加载配置文件
@Configuration public class PropertiesUtils implements EnvironmentAware { private int redisExpireTim ...
随机推荐
- 电脑通过WIFI连接手机ADB
1.搜索adb wifi 2.安装并开启:根据提示 3.电脑:adb connect 192.168.1.134 a安装ADB TOOLS b安装ADB DRIVER c将ADB TOOLS复制到c: ...
- C语言:外部函数
//main.c #include <stdio.h> extern void func(); extern int m; int n = 200; int main(){ func(); ...
- WinForm使用DataGridView实现类似Excel表格的查找替换
在桌面程序开发过程中我们常常使用DataGridView作为数据展示的表格,在表格中我们可能要对数据进行查找或者替换. 其实要实现这个查找替换的功能并不难,记录下实现过程,不一定是最好的方式,但它有用 ...
- IO编程之对象序列化
对象序列化的目标是将对象保存在磁盘中或者允许在网络中直接传输对象.对象序列化机制循序把内存中的java对象转换成平台无关的二进制流,从而允许把这种二进制流持久的保存在磁盘上,通过网络将这种二进制流传输 ...
- 就这?一篇文章让你读懂 Spring 事务
什么是事务 ▲ 百度百科 概括来讲,事务是一个由有限操作集合组成的逻辑单元.事务操作包含两个目的,数据一致以及操作隔离.数据一致是指事务提交时保证事务内的所有操作都成功完成,并且更改永久生效:事务回滚 ...
- odoo12里面的RPC【远程过程调用】
odoo的RPC有两种:RPC API:1.xml-rpc 2.json-rpc 案例 x ...
- 第三篇 -- IDEA 创建Springboot Web项目并用Jmeter测试
上一节使用Django写的一个接口程序,这一节讲用IDEA配合Springboot创建web项目.一个是python,一个是java. 参考链接:http://www.uxys.com/html/Ja ...
- python 之爬虫基本流程
python 之爬虫基本流程 一 用户获取网络数据的方式: 方式1:浏览器提交请求--->下载网页代码--->解析成页面 方式2:模拟浏览器发送请求(获取网页代码)->提取有用的数据 ...
- jvm源码解读--12 invokspecial指令的解读
先看代码 package com.zyt.jvmbook; public class Girl extends Person{ public Girl() { int a; } @Override p ...
- ETL数仓测试
前言 datalake架构 离线数据 ODS -> DW -> DM https://www.jianshu.com/p/72e395d8cb33 https://www.cnblogs. ...