概述

在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的更多相关文章

  1. Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...

  2. Spring读取加密属性文件处理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  3. spring 使用外部属性文件

    一.PropertyPlaceholderConfigurer spring提供的PropertyPlaceholderConfigurer实现类能够使Bean在配置时引用外部属性文件. Proper ...

  4. Spring读取加密属性文件处理--待整理

    引言:Spring框架俨然已经是目前Java WEB项目开发的一个宠儿,更有人将Spring, Struts,和Hibernage称之为Java WEB项目开发的3件利器.Spring的依赖.注入.A ...

  5. Spring 使用外部属性文件配置

    1.Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean的配置的部分内容 移到属性文件中.可以在Bean配置 ...

  6. Spring使用外部属性文件

    一.在 Spring Config 文件中配置 Bean 时,有时候需要在 Bean 的配置里添加 系统部署的细节信息, 如文件路径,数据源配置信息.而这些部署细节实际上需要在配置文件外部来定义. 二 ...

  7. Spring 应用外部属性文件 配置 context 错误

    在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...

  8. Spring_Bean的作用域---和使用外部属性文件

    <!-- 使用 bean的scope属性来配置bean的作用域 singleton:默认值.容器初始时创建bean实例,在整个容器的生命周期内只创建这一个bean单例 prototype:原型的 ...

  9. Spring配置文件外部化配置及.properties的通用方法

    摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...

随机推荐

  1. 【java】计算一段代码执行时长java.lang.System类里的public static long currentTimeMillis()方法

    public class Test_currentTimeMillis { public static void main(String[] args) { long start=System.cur ...

  2. 商城项目整理(三)JDBC增删改查

    商品表的增加,修改,删除,订单表的增加,确认,用户表的查看,日志表的增加,查看 商品表建表语句: create table TEST.GOODS_TABLE ( gid NUMBER not null ...

  3. Data Base mongodb driver2.5环境注意事项

    mongodb driver2.5环境注意事项 一.问题: 如果使用vs2012开发就会报这个错误: 未能加载文件或程序集“System.Runtime.InteropServices.Runtime ...

  4. Structural Inference of Hierarchies in Networks(网络层次结构推断)

    Structural Inference of Hierarchies in Networks(网络层次结构推断) 1. 问题 层次结构是一种重要的复杂网络性质.这篇文章给出了层次结构的精确定义,给出 ...

  5. nova创建虚拟机源码分析系列之三 PasteDeploy

    上一篇博文介绍WSGI在nova创建虚拟机过程的作用是解析URL,是以一个最简单的例子去给读者有一个印象.在openstack中URL复杂程度也大大超过上一个例子.所以openstack使用了Past ...

  6. 安装supervisord

    一:简介 supervisord是一个进程管理工具,提供web页面管理,能对进程进行自动重启等操作. 优点: - 可以将非后台运行程序后台运行 - 自动监控,重启进程 缺点: - 不能管理后台运行程序 ...

  7. Python函数篇(7)-正则表达式

    1.正则表达式   正则表达式为高级的文本模式匹配,抽取,与/或文本形式的搜索和替换功能提供了基础,简单的来说,正则表达式是由一些字符和特殊符号组成的字符串.Python通过标准库中的re模块来支持正 ...

  8. eclipse在debug模式下总是自动进入到ThreadPoolExecutor类中

    当我们将web项目发布到tomcat服务器中,并且以debug模式启动的时候,总是自动跳转到 ThreadPoolExecutor 类中,如下: 解决办法 在eclipse中点击Window-> ...

  9. Netty之心跳检测技术(四)

    Netty之心跳检测技术(四) 一.简介 "心跳"听起来感觉很牛X的样子,其实只是一种检测端到端连接状态的技术.举个简单的"栗子",现有A.B两端已经互相连接, ...

  10. Netty入门之客户端与服务端通信(二)

    Netty入门之客户端与服务端通信(二) 一.简介 在上一篇博文中笔者写了关于Netty入门级的Hello World程序.书接上回,本博文是关于客户端与服务端的通信,感觉也没什么好说的了,直接上代码 ...