spring访问外部属性--property

概述

在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;
    }
...

这种创建方式在自动扫描的时候注入场景中使用最为合适

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值