zoukankan      html  css  js  c++  java
  • Spring中获取外部配置文件中的属性值

    很多时候需要将配置信息从程序中剥离粗来,Spring现在提供的方法是通过@Value注解和<context:placeholder>来获取配置文件中的配置信息。这里给出一个简单的例子。

    首先在resources文件夹下简历配置文件spring.biz.properties,文件内容为:

    dataId=test
    versionId=1.0.1.daily

    然后在xml文件中读入该属性值,spring-config.xml文件的内容如下:

    <context:property-placeholder location="classpath:spirng.biz.properties"/>

    第三步是定义需要这些属性的类,要使用注解必须在xml文件中打开注解驱动,代码为:<context:annotation-config/>。@Value注解中使用${key}取出key对应的value。TestConfig.java的内容为如下。

    package com.javadu.core;
    
    import org.springframework.beans.factory.annotation.Value;
    
    /**
     * Created by duqi on 15/9/14.
     */
    public class TestConfig {
        @Value("${dataId}")
        private String dataId;
        @Value("${versionId}")
        private String versionId;
    
        private String other;
    
        public void setOther(String other){
            this.other = other;
        }
    
        public String getDataId(){
            return dataId;
        }
    
        public String getVersionId(){
            return versionId;
        }
    }
    

    在xml文件中定义TestConfig对应的bean,完整的spring-config.xml文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
           <context:annotation-config/>
           <context:property-placeholder location="classpath:spirng.biz.properties"/>
    
           <bean id="configBean" class="com.javadu.core.TestConfig">
                  <property name="other" value="otherother"/>
           </bean>
    </beans>

    最后,在App.java类中:启动IoC容器,获取TestBean的实例,调用其开放的接口,代码如下:

    package com.javadu.common;
    
    
    import com.javadu.core.TestConfig;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by duqi on 15/9/8.
     */
    public class App {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            TestConfig configBean = (TestConfig)context.getBean("configBean");
            System.out.println(configBean.getDataId());
            System.out.println(configBean.getVersionId());
        }
    }
    

    最后的运行结果如下:
    这里写图片描述

  • 相关阅读:
    Windows server 2016 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同。”
    Windows Server 2016 辅助域控制器搭建
    Windows Server 2016 主域控制器搭建
    Net Framework 4.7.2 覆盖 Net Framework 4.5 解决办法
    SQL SERVER 2012更改默认的端口号为1772
    Windows下彻底卸载删除SQL Serever2012
    在Windows Server2016中安装SQL Server2016
    SQL Server 创建索引
    C#控制台或应用程序中两个多个Main()方法的设置
    Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
  • 原文地址:https://www.cnblogs.com/javaadu/p/11742615.html
Copyright © 2011-2022 走看看