【spring boot】4.spring boot配置多环境资源文件

一个spring boot 项目在开发环境、测试环境、生产环境下,好多的配置都是不尽相同的。所以配置多分的资源文件,仅仅在部署在不同环境的时候,选择激活不同的资源文件就可以实现多环境的部署。

 

项目结构如下:

 

1.配置多个环境下的不同的资源文件

多个资源文件的格式如下:

application-{profile}.properties

{profile}自定义的不同环境标识,本项目中分别对应如下:

==========================================================================

 列出各个环境下的资源文件内容:

application-dev.properties  开发资源文件

application-pro.properties  生产资源文件

application-test.properties     测试资源文件

 

2.主资源文件中 选择激活一种环境下的资源文件

spring.profiles.active=dev

 dev就是上面一种资源文件的自定义标识

 

3.绑定到一个bean,提供给程序中使用

package com.sxd.beans;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "com.sxd")
public class ConfigBean {

    private String ip;
    private String value;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

View Code

 

 

4.程序主入口,激活绑定的bean,顺便使用了

package com.sxd.firstdemo;

import com.sxd.beans.ConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@EnableConfigurationProperties({ConfigBean.class})
public class FirstdemoApplication {

    @Autowired
    ConfigBean configBean;

    @RequestMapping("/")
    public String index(){

        return "IP:"+configBean.getIp()+"\n环境:"+configBean.getValue();
    }
    public static void main(String[] args) {
        SpringApplication.run(FirstdemoApplication.class, args);
    }
}

View Code

 

 

5.启动并访问  ,当前激活的是开发环境资源文件

 

 

 

==================================================================================================================

spring.profiles.active=dev

是选择一种资源文件

 

spring.profiles.include=dev,test,pro

可以叠加多个资源文件

 

时间: 2024-08-01 11:46:20

【spring boot】4.spring boot配置多环境资源文件的相关文章

Spring Boot 探索系列 - 自动化配置篇

26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging,Log4J, Log4J2 an

使用Spring Session实现Spring Boot水平扩展

本文使用Spring Session实现了Spring Boot水平扩展,每个Spring Boot应用与其他水平扩展的Spring Boot一样,都能处理用户请求.如果宕机,Nginx会将请求反向代理到其他运行的Spring Boot应用上,如果系统需要增加吞吐量,只需要再启动更多的Spring Boot应用即可. Spring Boot应用通常会部署在多个Web服务器上同时提供服务,这样做有很多好处: 单个应用宕机不会停止服务,升级应用可以逐个升级而不必停止服务. 提高了应用整体的吞吐量.

使用JWT保护你的Spring Boot应用 - Spring Security实战

作者 freewolf 原创文章转载请标明出处 关键词 Spring Boot.OAuth 2.0.JWT.Spring Security.SSO.UAA 写在前面 最近安静下来,重新学习一些东西,最近一年几乎没写过代码.整天疲于奔命的日子终于结束了.坐下来,弄杯咖啡,思考一些问题,挺好.这几天有人问我Spring Boot结合Spring Security实现OAuth认证的问题,写了个Demo,顺便分享下.Spring 2之后就没再用过Java,主要是xml太麻烦,就投入了Node.js的怀

【spring Boot】Spring中@Controller和@RestController之间的区别

spring Boot入手的第一天,看到例子中的@RestController ............. 相同点:都是用来表示Spring某个类的是否可以接收HTTP请求 不同点:@Controller标识一个Spring类是Spring MVC controller处理器 @RestController:  a convenience annotation that does nothing more than adding the@Controller and @ResponseBody 

让Spring架构减化事务配置

Spring颠覆了以前的编程模式,引入了IOC等全新的概念,广受大家的喜爱.目前大多数j2ee项目都已经采用Spring框架.Spring最大的问题是太多的配置文件,使得你不仅需要维护程序代码,还需要额外去维护相关的配置文件.最典型的就是事务配置(注:这里的"事务配置"都指"声明式事务配置"),在Spring中进行事务配置除了定义对象自身的bean外,还需要定义一个进行事务代理的bean.如果你有n个类需要引入事务,那么你就必须定义2n个bean.维护这些bean的

spring事务-spring AOP 拦截器方式配置事务失效

问题描述 spring AOP 拦截器方式配置事务失效 Spring.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:conte

Spring事务——Spring 2.X的事务配置策略

Spring 2.X的事务配置策略     虽然前面介绍的TransactionProxyFactoryBean配置策略简单易懂,但配置起来极为麻烦:每个目标Bean都需要配置一个TransactionProxyFactoryBean代理,这种方式将导致配置文件急剧增加.     Spring 2.X的XMLSchema方式提供了更简洁的事务配置策略,Spring 2.X提供了tx命名空间来配置事务管理,tx命名空间提供了<tx:advice../>元素来配置事务增强处理,一旦使用该元素配置了

mvc注解-spring MVC注解 + hibernate +jpa配置 出错 具体如下

问题描述 spring MVC注解 + hibernate +jpa配置 出错 具体如下 这个是错误 No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 下面为XML配置文件 web.xml 导入hibernate <filter> <filter-name>openEntityManagerInViewFilter&

spring 多个数据库怎么配置

问题描述 求spring多个数据库怎么配置 解决方案 解决方案二:配置多个数据源解决方案三:一个数据库还不够用吗解决方案四:http://wujt.javaeye.com/blog/685018去看看!!解决方案五:记忆中是在classPath中用通配符配置的多个spring配置文件.也可以在classPath中将每个spring的配置文件名写出,用逗号分隔.好久没自己配了.你找到了答案通知一下我哦解决方案六:在Spring中配置多个数据源,需要那个就注入那个.<beanid="dataS