zoukankan      html  css  js  c++  java
  • 读取properties的简单方法,使用@Configuration

    1. 配置类代码如下
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class Config {
        @Value("${test}")
        private String test;
    
        public String getTest() {
            return test;
        }
        
    }

      2.Spring配置

    <!-- 获取配置属性值 -->
        <bean id="configProperties"
            class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:app/env/config.properties</value>
                    <value>classpath:app/config/database.properties</value>
                    <value>classpath:app/config/redis.properties</value>            
                </list>
            </property>
        </bean>
        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
            <property name="properties" ref="configProperties" />
        </bean>        

      3.测试接口类

    @Service
    public class SsoInterface {
    
        @Autowired
        private SsoConfig SsoConfig;
    
    
      public void test(){
              System.out.println(SsoConfig.getTest());
        }  
    
    }

    最后就能输出 需要的配置文件对应信息

    版权声明:如需转载,请注明!PS:如是转载随便,请忽略
  • 相关阅读:
    学生排序,使用三层优先级
    利用类计算学生成绩和排序
    join()函数
    对象
    015_eclipse开发环境的使用
    013_运算符_算术
    012_变量
    011_jdk7新特性
    010_类型提升问题
    008_浮点数误差问题
  • 原文地址:https://www.cnblogs.com/zwdx/p/7682953.html
Copyright © 2011-2022 走看看