SpringBoot获取properties配置
来源:互联网 发布:windows文件共享端口 编辑:程序博客网 时间:2024/04/30 21:41
前言:在项目中,很多时候需要把配置写在properties里,部署的时候也需要切换不同的环境来选择正确的配置的参数,也有时候需要将mq redis等第三方配置新建一个properties文件在项目中引用。
1.因为是Spring的环境,当然首先需要搭建好Spring环境。
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. */@Componentpublic 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}")//读取配置在PropertySourcesPlaceholderConfigurer Bean里的properties文件的值 public String test5;}
需要注意的是通过 Environment 对象只能获取 Springboot的propertie文件的参数,比如 application-dev.properties。如果是不是application开头的的配置文件,需要单独指定properties的路径
@PropertySource("classpath:config.properties")//引用其他单独的properties
如果前置一样可以统一配置
@ConfigurationProperties(prefix = "spring.wnagnian",locations = "classpath:config/xxx.properties")
2.如果直接用 @Value("${name}") 来取配置的值需要配置 PropertySourcesPlaceholderConfigurer 用来引入properties文件
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. */@Configurationpublic class PropertiesConfig { @Bean public PropertySourcesPlaceholderConfigurer createPropertySourcesPlaceholderConfigurer() { ClassPathResource resource = new ClassPathResource("config.properties"); PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertyPlaceholderConfigurer.setLocation(resource); return propertyPlaceholderConfigurer; }}
如果是Spring xml配置
<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>
取值
@Value("#{configProperties['name']}")private String name;
0 0
- SpringBoot获取properties配置
- SpringBoot获取properties配置
- SpringBoot学习系列 | 获取properties配置
- springboot-properties配置
- 3.SpringBoot-properties配置
- springboot application.properties配置
- springBoot--properties配置
- SpringBoot-application.properties配置清单
- springBoot基础系列--properties配置
- springBoot基础系列--properties配置
- springBoot读取配置application.properties
- springboot的application.properties配置
- SpringBoot初窥:application.properties配置
- springBoot基础系列--properties配置
- Springboot 随笔-- Properties 配置一坑
- springboot配置文件application.properties中的配置项
- springboot 学习笔记(二)--- properties 配置
- SpringBoot properties通用属性配置大全
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- Ruby Rails入门——windows下搭建Ruby Rails Web开发环境
- web标准的理解
- github代码托管+git bash/android studio
- Qt5:QML:LocalStorage
- SpringBoot获取properties配置
- 一次mysql死锁的排查过程
- 用Altium Designer 在PCB板中制作LOGO的方法详解
- VS2012中自动变量的地址分配问题
- SYS_LAST_KMSG里的hw_status和fiq step的含义
- 构造素数表
- 2016 cocoapods的安装和使用以及版本升级遇到的问题 [转]
- winsock2.h的链接库文件问题
- Android Git代码版本控制