概述

在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. C语言位操作的算法

    1.头文件 #ifndef _INC_BITOPERATION #define _INC_BITOPERATION #endif /* 封装了所有的位操作运算 */ #include<stdio ...

  2. Xamarin截取/删除emoji表情bug解决方案

    大家都知道,一个英文=1字节,一个汉字2字节,而一个emoji表情=4个字节,在有这三种混用的时候,比如app聊天界面,那么删除和截取便成了很头痛的事情. 问题描述 截取导致乱码,如下图: 解决方案 ...

  3. 搜索模式| 系列2——KMP算法

    给定一个文本txt [0..n-1]和一个模式pat [0..m-1],写一个搜索函数search(char pat [],char txt []),在txt中打印所有出现的pat [] [].可以假 ...

  4. Spring 自动装配及自动注册的相关配置

    Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...

  5. Oracle学习笔记_10_判断是否为日期类型

    FUNCTION isdate (datestr VARCHAR2, format VARCHAR2) RETURN number IS p_date DATE; BEGIN SELECT TO_DA ...

  6. ubuntu 安装 pythonenv

    This will get you going with the latest version of pyenv and make it easy to fork and contribute any ...

  7. 基于阿里云的JavaEE系统框架介绍

    基于阿里云的系统框架展望 1) CDN 用于缓存静态文件等等.七牛和阿里的都还可以. 七牛要做的久一点,各种图片处理的接口要完善一些 阿里的CDN要稍微好一点点,但是没有不安全的访问方式,访问稍微没有 ...

  8. 房上的猫:了解java与学习java前的准备

    一.java 概述:  1.通常指完成某些事情的一种既定方式和过程  2.程序可以看做对一系列动作执行过程的描述  3.计算机按照某种顺序完成一系列指令的集合称为程序  4.计算机仅识别二进制低级语言 ...

  9. Gradle、Gradle Wrapper与Android Plugin for Gradle

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  10. iPhone X 适配

    背景 iPhone X 刘海机于9月13日发布,给科技小春晚带来一波高潮.作为开发人员却多出来一份忧虑,iPhone X 怎么适配?我们 App 的脑袋会不会也长一刘海出来?Tabbar 会不会被圆角 ...