在项目中有些参数经常需要修改,或者后期可能会有改动时,那我们最好把这些参数放到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详解(十)加载配置文件的更多相关文章

  1. 详解ListView加载网络图片的优化,让你轻松掌握!

    详解ListView加载网络图片的优化,让你轻松掌握! 写博客辛苦了,转载的朋友请标明出处哦,finddreams(http://blog.csdn.net/finddreams/article/de ...

  2. 微服务架构 | *2.3 Spring Cloud 启动及加载配置文件源码分析(以 Nacos 为例)

    目录 前言 1. Spring Cloud 什么时候加载配置文件 2. 准备 Environment 配置环境 2.1 配置 Environment 环境 SpringApplication.prep ...

  3. 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

  4. linux modprobe命令参数及用法详解--linux加载模块命令

    转:http://www.linuxso.com/command/modprobe.html modprobe(module probe) 功能说明:自动处理可载入模块. 语 法:modprobe [ ...

  5. Spring的 classpath 通配符加载配置文件

    http://www.cnblogs.com/taven/archive/2012/10/24/2737556.html classpath:app-Beans.xml 说明:无通配符,必须完全匹配 ...

  6. 详解Class加载过程

    1.Class文件内容格式 2.一个class文件是被加载到内存的过程是怎样的? loading 把一个class文件装到内存里,class文件是一个二进制,一个个的字节 linking Verifi ...

  7. 详解ListView加载网络图片的优化

    我们来了解一些ListView在加载大量网络图片的时候存在的常见问题: 1.性能问题,ListView的滑动有卡顿,不流畅,造成非常糟糕的用户体验. 2.图片的错位问题. 3.图片太大,加载Bitma ...

  8. Google推荐——Glide使用详解(图片加载框架)

    零.前言 本文所使用的Glide版本为3.7.0 一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的goo ...

  9. spring boot 使用 EnvironmentAware 加载配置文件

    @Configuration public class PropertiesUtils implements EnvironmentAware { private int redisExpireTim ...

随机推荐

  1. printf函数返回值

    //返回值:正确返回输出的字符总数,错误返回负值,与此同时,输入输出流错误标志将被置值,可由指示器ferror来检查输入输出流的错误标志. #include <stdio.h> #defi ...

  2. SpringBoot总结之属性配置

    一.SpringBoot简介 SpringBoot是spring团队提供的全新框架,主要目的是抛弃传统Spring应用繁琐的配置,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...

  3. 简单配置nginx反向代理,实现跨域请求

    简单配置nginx去做反向代理,实现跨域请求 简单介绍nginx的nginx.conf最核心的配置,去做反向代理,实现跨域请求. 更多详细配置,参考nginx官方文档 先介绍几个nginx命令 打开n ...

  4. python + mysql 实现查询表数据

    实例如下: import pymysqldef select_form(): # 打开数据库连接 db = pymysql.connect("localhost", "r ...

  5. Go LRU Cache 抛砖引玉

    目录 1. LRU Cache 2. container/list.go 2.1 list 数据结构 2.2 list 使用例子 3. transport.go connLRU 4. 结尾 正文 1. ...

  6. PHP获取当日或本月时间戳范围

    在mysql数据库中使用int类型保存时间戳时,一般在程序中可能会用到统计当日,当月的一些数据.那么可以用如下的方式限定时间范围:   //当日销售 $today_start = strtotime( ...

  7. informix错误代码小结

    informix错误代码小结 所有错误可以用finderr+错误代码查到,英文的,这里中文注释便于理解. -100 错误的描述:C-ISAM错误:向具有唯一索引的字段加入一个重复值.  系统的操作:该 ...

  8. jvm源码解读--18 Java的start()方法解读 以及 wait 和notify流程图

    drawwed by 张艳涛 and get info from openjdk8 还有一个图

  9. 除了Swagger UI,你还能选择 IGeekFan.AspNetCore.RapiDoc

    IGeekFan.AspNetCore.RapiDoc 看到博客园上的这个文章,说了下Knife4J,评论里有人推荐RapiDoc,放了几个图,看了下,还不错. 心里 便有个想法,借着上次研究 Kni ...

  10. RHCE_DAY06

    iptables防火墙 ----匹配及停止 nerfilter/iptables:工作在主机或网络的边缘,对于进出本主机或网络的报文根据事先定义好的检查规则作匹配检测,对于能够被规则所匹配到的报文做出 ...