SpringBoot获取properties配置
来源:互联网 发布:郭达 刁光斗 知乎 编辑:程序博客网 时间:2024/05/16 15:58
前言:在项目中,很多时候需要把配置写在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配置 classpath:config.properties 取值@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通用属性配置大全
- cocos2dx多个按钮同时响应点击事件
- 自组织神经网络介绍:自组织特征映射SOM(Self-organizing feature Map),第二部分
- 基于springmvc的简单下载
- Ajax+SpringMVC+Spring+Mybatis+MySql+js用户注册实例
- FTPrep, 33 Search in Rotated Sorted Array
- SpringBoot获取properties配置
- 杭电ACM基础编程知识点(2)
- 自组织神经网络介绍:自组织特征映射SOM(Self-organizing feature Map),第三部分
- 网络编程中的阻塞与非阻塞,同步与异步概念的理解
- 创建js对象的几种方式
- 处理异常
- (ubuntu系列)一些必备好玩的东西
- js通过字节长度截取中英文混合字符串
- 引用iview ui 时报错,很有可能是你写的不对!!!