Spring读取外部属性-properties
概述
在Spring中处理外部值最简常用的方法就是外部创建name.properties文件,并在其中声明变量值,供Java进行读取。比如数据源信息配置,Java固定属性位置等。读取的方式一般由三种:
- 通过Spring的Environment检索属性
- 通过占位符读取属性(Java和xml两种方式)
- 通过表达式装配(xml)
1. 通过Spring Envrionment检索属性
简单示例:
- 创建property.properties文件,并编写属性值:
name=this is properties
title=properties
num=100
- 创建目标Java类
PropertyImpl.java,包含property.properties中属性对应的成员变量:
private String title;
private String name;
public PropertyImpl(String ti, String na) {
// TODO Auto-generated constructor stub
title = ti;
name = na;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("title:" + title + "\n" + "name:" + name);
}
- 创建Java配置类,显示声明Bean,并声明包含properties中声明属性的Spring Bean工厂@Configuration等价于xml中的beans,@ComponentScan自动扫描该包和子包添加注释的Java类注入到Bean工厂,@PropertySource Spring解析指定的properties文件属性:
@Bean
public Proterty property(){
String[] activSsize = env.getActiveProfiles();
for(int i = 0; i < activSsize.length; i++){
System.out.println("i=" + i + " || profile:" + activSsize[i]);
}
String[] defaultSize = env.getActiveProfiles();
for(int i = 0; i < defaultSize.length; i++){
System.out.println("i=" + i + " || profile:" + defaultSize[i]);
}
Integer num = env.getProperty("num", Integer.class, 30);
System.out.println("num=" + num);
num = env.getProperty("num1", Integer.class, 30);
System.out.println("num1=" + num);
return new PropertyImpl(env.getProperty("title"), env.getProperty("name"));
}
}
- 通过Java配置Spring形式读取properties文件属性值基本结束,下面通过junit进行测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfiguration.class)
public class Main {
@Autowired
private Proterty proterty;
@Test
public void test(){
proterty.show();
}
}
- 对于environment包含的其它方法介绍:
- T getProterty(String key, Class type, T defaultValue)如果不存在,设定默认值
- T getProterty(String key, Class type)将key属性值转化为固定类对象
- String getProterty(String key, String defaultValue)读取属性值不存在,设置为默认值
- getRequiredProperty(String key)如果对应属性不存在,跑出异常
- constainsProperty(String key)检查属性是否存在
- getPropertyAsClass(String key, name.class)将属性值解析为类
- getActiveProfiles()
- getdefaultProfiles()
- acceptsProfiles()
2. 通过占位符获取属性值
- 首先,创建properties属性文件
property.proterties,property2.properties(加载多propeerties文件):
name=this is properties
title=properties
num=100
url=localhost:8080
drive=c3p0
usename=chen
password=abcd1234
- 创建对应读取属性值的Java类
XmlProperties.java
public class XmlProperties {
private String url;
private String drive;
private String usename;
private String password;
private String title;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDrive() {
return drive;
}
public void setDrive(String drive) {
this.drive = drive;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void show(){
System.out.println("url:" + url + "\nderive:" + drive + "\nusename:" + usename +
"\npassword:" + password + "\ntitle:" + title);
}
}
- xml配置文件:创建
PropertyPlaceholderConfigurer Bean,并加载properties文件。
<bean id="propertyConfligurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/com/tidas/spring/proterties/property2.properties</value>
<value>/com/tidas/spring/proterties/property.proterties</value>
</list>
</property>
</bean>
- 通过占位符读取属性值,这种也是在web开发中配置数据源最常用的方法
<bean id="xmlProperties" class="com.tidas.spring.property.XmlProperties">
<property name="url" value="${url}"/>
<property name="drive" value="${drive}"/>
<property name="usename" value="${usename}"/>
<property name="password" value="${password}"/>
<property name="title" value="${title}"/>
</bean>
- 当然,如果是通过javaconfigration进行配置Bean工厂的,也可以使用如下方法读取属性:
@Component
public class PropertyImpl implements Proterty {
private String title;
private String name;
public PropertyImpl(@Value("${title}") String ti, @Value("${name}") String na) {
// TODO Auto-generated constructor stub
title = ti;
name = na;
}
...
这种创建方式在自动扫描的时候注入场景中使用最为合适
Spring读取外部属性-properties的更多相关文章
- Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置
通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...
- Spring读取加密属性文件处理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...
- spring 使用外部属性文件
一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...
- Spring读取加密属性文件处理--待整理
引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...
- Spring 使用外部属性文件配置
1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...
- Spring使用外部属性文件
一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...
- Spring 应用外部属性文件 配置 context 错误
在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...
- Spring_Bean的作用域---和使用外部属性文件
<!-- 使用 bean的scope属性来配置bean的作用域 singleton:默认值.容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean单例 prototype:原型的 ...
- Spring配置文件外部化配置及.properties的通用方法
摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...
随机推荐
- springMVC(3)---利用pdf模板下载
springMVC(3)---利用pdf模板下载 在实际开发中,很多时候需要通过把数据库中的数据添加到pdf模板中,然后供客户下载,那我们该如何中呢? 本文主要内容是:用java在pdf模板中加入数据 ...
- shell编辑crontab任务
crontab是Linux下执行定时任务的工具,之前偶尔需要用到时都是通过执行crontab -e命令或者通过root身份直接编辑/etc/cron.*/下的文件来添加定时任务.这段时间遇到了需要通过 ...
- VS源码编译QuaZip(Windows下)
最近写个Qt demo,想要使用压缩和解压多个文件的功能,并不使用额外进程.网上参考了很多资料,发现只有QuaZip比较适合我的需求.但是QuaZip只提供源码,因此需要自己来编译. QuaZip简介 ...
- 如何用jQuery实现五星好评
jQuery是js的一个库,封装了我们开发过程中常用的一些功能,方便我们来调用,提高了我们的开发效率. Js库是把我们常用的功能放到一个单独的文件中,我们用的时候,直接引用到页面里面来就可以了. 接下 ...
- ES6 Proxy和Reflect(下)
construct() construct方法用于拦截new命令. var handler = { construct (target, args) { return new target(...ar ...
- precmd:6: job table full or recursion limit exceeded
使用GDC Data Transfer Tool下载10999个isoforms.quantification.txt文件时,写了shell循环的小脚本: cat all_id_file |while ...
- JAVA 用数组实现 ArrayList
我们知道 ArrayList 是一个集合,它能存放各种不同类型的数据,而且其容量是自动增长的.那么它是怎么实现的呢? 其实 ArrayList 的底层是用 数组实现的.我们查看 JDK 源码也可以发现 ...
- K:java中的hashCode和equals方法
hashCode和equals方法是Object类的相关方法,而所有的类都是直接或间接的继承于Object类而存在的,为此,所有的类中都存在着hashCode和equals.通过翻看Object类 ...
- 使用C#开发数据库应用系统 习题
错题积累 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
- 启用composer镜像服务
使用composer下载东西,需要FQ时,可使用其镜像服务 安装composer后,命令行执行全局配置 composer config -g repo.packagist composer https ...