Spring中属性文件properties的读取与使用
实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理。例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.properties中。
其中部分配置信息(邮件发送相关):
- #邮件发送的相关配置
- email.host = smtp.163.com
- email.port = xxx
- email.username = xxx
- email.password = xxx
- email.sendFrom = xxx@163.com
在Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
- <!-- spring的属性加载器,加载properties文件中的属性 -->
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>/WEB-INF/configInfo.properties</value>
- </property>
- <property name="fileEncoding" value="utf-8" />
- </bean>
属性信息加载后其中一种使用方式是在其它bean定义中直接根据属性信息的key引用value,如邮件发送器bean的配置如下:
- <!-- 邮件发送 -->
- <bean id="mailSender"
- class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host">
- <value>${email.host}</value>
- </property>
- <property name="port">
- <value>${email.port}</value>
- </property>
- <property name="username">
- <value>${email.username}</value>
- </property>
- <property name="password">
- <value>${email.password}</value>
- </property>
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- <prop key="sendFrom">${email.sendFrom}</prop>
- </props>
- </property>
- </bean>
另一种使用方式是在代码中获取配置的属性信息,可定义一个javabean:ConfigInfo.java,利用注解将代码中需要使用的属性信息注入;如属性文件中有如下信息需在代码中获取使用:
- #生成文件的保存路径
- file.savePath = D:/test/
- #生成文件的备份路径,使用后将对应文件移到该目录
- file.backupPath = D:/test bak/
ConfigInfo.java 中对应代码:
- @Component("configInfo")
- public class ConfigInfo {
- @Value("${file.savePath}")
- private String fileSavePath;
- @Value("${file.backupPath}")
- private String fileBakPath;
- public String getFileSavePath() {
- return fileSavePath;
- }
- public String getFileBakPath() {
- return fileBakPath;
- }
- }
业务类bo中使用注解注入ConfigInfo对象:
- @Autowired
- private ConfigInfo configInfo;
需在bean.xml中添加组件扫描器,用于注解方式的自动注入:
- <context:component-scan base-package="com.my.model" />
(上述包model中包含了ConfigInfo类)。
通过get方法获取对应的属性信息,优点是代码中使用方便,缺点是如果代码中需用到新的属性信息,需对ConfigInfo.java做相应的添加修改。
Spring中属性文件properties的读取与使用的更多相关文章
- Spring的属性文件properties使用注意
Spring的属性文件properties使用注意 Spring 中属性文件的配置 通常我们会使用properties文件来设置一些属性,如数据库连接信息,避免进行硬编码, <bean clas ...
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
spring管理属性配置文件properties--使用PropertiesFactoryBean 对于属性配置,一般采用的是键值对的形式,如:key=value属性配置文件一般使用的是XXX.pr ...
- Spring 中无处不在的 Properties
转自:https://javadoop.com/post/spring-properties?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sou ...
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...
- Spring中使用属性文件properties的两种方式
实际项目中,通常将可配置的参数放到属性文件中,例如数据库连接信息.redis连接信息等,便于统一管理.然后通过IoC框架spring将其加载到上下文中,使得程序可以直接使用. 创建mysql.prop ...
- Spring MVC 属性文件读取注入到静态字段
目录(?)[-] servlet-contextxml configproperties 示例属性 ConfigInfo 对应的配置bean 使用 在项目中,有些参数需要配置到属性文件xxx.pr ...
- Java中如何获取spring中配置的properties文件内容
有2种方式: 一. 1.通过spring配置properties文件 [java] <bean id="propertyConfigurer" class=&qu ...
- Spring对外部属性文件指定的某个属性进行加密、解密
[From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...
- Spring配置属性文件
在项目开发阶段和交付阶段数据库的连接信息往往是不同的,可以把这些信息写成属性文件,再在Spring中导入即可引用 jdbc.properties属性文件如下: jdbc.driverClassName ...
随机推荐
- MySQL数据故障时备份与恢复
1.ib_logfile0和ib_logfile1是mysql用来存储操作执行的日志文件,用于事务暂存和回滚.当复制ibdata1数据文件到新的mysql中时,如果没有拷贝这两个日志文件,则会出现启动 ...
- Delphi cxGrid –--> RecordIndex out of Range
delphi 导出数据时常常出现这样一个错误 < RecordIndex out of Range > 处理办法: 设定 cxGridDBTableView 的 GridModeBuffe ...
- ajax加载表格数据
一.html代码 <style type="text/css"> .table-taskinfo table tr { border-top: 2px solid #9 ...
- Divide and conquer:K Best(POJ 3111)
挑选最美的珠宝 题目大意:挑选k个珠宝使得∑a/∑b最大,输出组合数 最大化平均值的标准题型,二分法就好了,一定要注意范围(10e-7),如果是10e-8就会tle,10e-6就是wa #inclu ...
- java获取客服端信息(系统,浏览器等)
String agent = request.getHeader("user-agent"); System.out.println(agent); StringTokenizer ...
- php数据访问(修改)
修改:跟添加相似,需要显示默认值 先嵌入php代码 查询数据库 $code = $_GET["c"]; $db = new MySQLi("localhost" ...
- iOS蓝牙4.0
iOS的蓝牙用到了 CoreBluetooth 框架 首先导入框架 #import <CoreBluetooth/CoreBluetooth.h> 我们需要一个管理者来管理蓝牙设备,CB ...
- TortoiseSVN中Branching和Merging实践
转自:http://blog.csdn.net/eggcalm/article/details/6606520 使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心,二来即使涉及 ...
- Ubuntu gcc编译报错:format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]
平时用的都是Centos系统,今天偶然在Ubuntu下编译了一次代码,发现报错了: 源码: #include <stdio.h> #include <sys/time.h> # ...
- js windows对象
一.DOM操作 windows对象操作 document对象操作 二.属性.事件 1.window的属性: window.shuxing(属性) window.fangfa()(方法) 方法后 ...