前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot获取properties配置

SpringBoot获取properties配置

作者头像
王念博客
发布2019-07-24 10:32:55
3.1K0
发布2019-07-24 10:32:55
举报
文章被收录于专栏:王念博客王念博客

前言:在项目中,很多时候需要把配置写在properties里,部署的时候也需要切换不同的环境来选择正确的配置的参数,也有时候需要将mq redis等第三方配置新建一个properties文件在项目中引用。

1.因为是Spring的环境,当然首先需要搭建好Spring环境。

代码语言:javascript
复制
package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2016/10/13.
 */
@Component
public class ValueTest {
    public String name = "注入对象的的属性";
    @Autowired
    public Environment env;//当前环境的application.properties的 配置
    @Value("注入普通字符串")//注入普通字符串
    public String test1;
    @Value("#{systemProperties['os.name']}")//系统属性配置
    public String test2;
    @Value("#{ T(java.lang.String).valueOf(111)}")//执行某个类的方法
    public String test3;
    @Value("#{valueTest.name}")//某个类的公有属性
    public String test4;
    @Value("${name}")//Springboot的properties,或者配置在PropertySourcesPlaceholderConfigurer Bean里的properties文件的值
    public String test5;

}

需要注意的是通过 Environment 对象只能获取 Springboot的propertie文件的参数,比如 application-dev.properties。如果是不是application开头的的配置文件,需要单独指定properties的路径

代码语言:javascript
复制
@PropertySource("classpath:config.properties")//引用其他单独的properties

如果前置一样可以统一配置

@ConfigurationProperties(prefix = "spring.wnagnian",locations = "classpath:config/xxx.properties")

2.如果直接用 @Value("${name}") 来取配置的值需要配置 PropertySourcesPlaceholderConfigurer 用来引入properties文件

代码语言:javascript
复制
package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

/**
 * Created by Administrator on 2016/10/13.
 */
@Configuration
public class PropertiesConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer() {
        ClassPathResource resource = new ClassPathResource("config.properties");
        PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setLocation(resource);
        return propertyPlaceholderConfigurer;
    }
}

如果是Spring xml配置

代码语言:javascript
复制
   <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
       <property name="locations">  
           <list>  
               <value>classpath:config.properties</value>  
           </list>  
       </property>  
    </bean>  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">  
        <property name="properties" ref="configProperties" />  
    </bean>  

取值

代码语言:javascript
复制
@Value("#{configProperties['name']}")
private String name
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档