1、pom.xml直接添加一下配置

    <profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
<!-- 这里代表默认使用dev环境配置文件 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>

2、选择环境

3、在application.yml添加配置

#默认使用配置,@activatedProperties@为激活的配置
spring:
profiles:
active: @activatedProperties@ #公共配置与profiles选择无关
mybatis-plus:
typeAliasesPackage: com.cn.commodity.entity
mapperLocations: classpath:mapper/*.xml logging:
level:
com.cn.commodity.dao : debug --- #开发配置
spring:
profiles: dev datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
#url: jdbc:mysql://localhost:3306/test
username: root
password: yang156122
driver-class-name: com.mysql.jdbc.Driver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource #jasypt:
# encryptor:
# password: yangwj ---
#开发配置
spring:
profiles: test datasource:
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
#url: jdbc:mysql://localhost:3306/test
username: root
password: yang156122
driver-class-name: com.mysql.jdbc.Driver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource

4、在service或controller层,通过@Profile(value="{pro,test}"),控制不同环境访问不同的api

package com.cn.commodity.controller;

import com.cn.commodity.entity.User;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@EnableAutoConfiguration
@Profile("dev")
@RequestMapping("/testboot")
public class TestBootController {
@RequestMapping("getuser")
public User getUser() {
User user = new User();
user.setUserName("test");
return user;
}
}

Java动态修改运行环境的更多相关文章

  1. Java开发和运行环境的搭建

    Java开发需要准备的东西? JDK+Eclipse 其中JDK的意思是Java开发工具包,Eclipse是进行用于做Java程序开发的工具(当然你也可以用记事本什么的去做). 其他开发工具:JCre ...

  2. Mac OS 您需要安装旧 Java SE 6 运行环境才能打开“XXX” 问题

    问题描述: 今天在Mac OS上安装一个app的时候提示 “您需要安装旧 Java SE 6 运行环境才能打开 XXX” ,在网上搜索了一下,有说要改app的info.plist文件的,将其中的JVM ...

  3. JAVA运行环境 和 Java Applet的运行环境 的区别

    Java小程序,也就是Java Applet,可以在Web浏览器中运行.Java Applet必须以<applet>脚本的形式嵌入到HTML页面中,才能在web浏览器中运行. 之前总以为本 ...

  4. Docker学习笔记之搭建 Java Web 项目运行环境

    0x00 概述 Java Web 泛指以 Java 程序为基础向外提供 Web 服务的技术及相关工具,狭义上来说,我们也可以说 Java Web 是由 Servlet 程序提供的 Web 服务. 对我 ...

  5. linux(center OS7)安装JDK、tomcat、mysql 搭建java web项目运行环境

    一.安装JDK 1.卸载旧版本或者系统自带的JDK (1)列出所有已安装的JDK rpm -qa | grep jdk (2)卸载不需要的JDK yum -y remove 安装包名称 2.下载并解压 ...

  6. 03 sublime text3下配置Java的编译运行环境

    参考如下文章,加入了自己的干货: https://blog.csdn.net/qq_38295511/article/details/81140069 https://blog.csdn.net/qq ...

  7. Linux上安装java+tomcat+mysql运行环境

    centos6.5jdk"1.7.0_79"mysql5.6apache-tomcat-7.0.53 安装jdk:1.先到oracle下载rpm包:jdk-7u80-linux-x ...

  8. linux环境下配置java WEB项目运行环境,jdk8+tomcat8+mysql5.7.11 新手向

    一:安装jdk 1.下载jdk  在oracle下载东西的时候因为oracle的一些验证机制,所以需要在链接前面添加一些参数 wget --no-check-certificate --no-cook ...

  9. 运行monitor提示需要安装旧JAVA SE 6运行环境

    MAC系统下运行monitor命令 ➜ tools git:(master) ✗ monitor 提示如下: 若要打开Eclipse.app,您需要Java SE 6 runtime,您想现在安装一个 ...

随机推荐

  1. 排序方法——python

    1.冒泡排序法(Bubble Sort) 比较相邻的元素.如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针对所 ...

  2. IIS搭建ASP站点

    1. 进入控制面板悬着打开或者关闭Windows功能. 2. 手工选择需要的功能进行安装. 3. 打开运行Internet信息服务(IIS)管理工具. 4. 展开左侧栏看到“Default Web S ...

  3. 【python】使用plotly生成图表数据

    安装 在 ubuntu 环境下,安装 plotly 很简单 python 版本2.7+ pip install plotly 绘图 在 plotly 网站注册后,可以直接将生成的图片保存到网站上,便于 ...

  4. java之jvm

    1.JVM内存模型 线程独占:栈,本地方法栈,程序计数器线程共享:堆,方法区 回答以上问题是需回答两个要点:1. 各部分功能2. 是否是线程共享 2.JMM与内存可见性JMM是定义程序中变量的访问规则 ...

  5. MVVM框架-MVVMLight

    项目URL:http://www.mvvmlight.net/ 一.安装MVVMLight 在NuGet程序包中搜索MVVMLight,然后安装. 二.使用 安装完MVVMLight后项目中会自动生成 ...

  6. webpack安装,常见问题和基本插件使用

    一:webpack认识 本质上,webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler).当 webpack 处理应用程序时,它会递归地构建一个依赖关 ...

  7. loaction.reload(false)和location.reload(true)的区别

    loaction.reload(false)和location.reload(true)有差别啊,一个是先判断页面有没修改,有的话就从服务器下载页面,没有就直接从缓存里拿(这个会提升响应性能)而把该方 ...

  8. How to change the default browser in Visual Studio

    http://www.hanselman.com/blog/HowToChangeTheDefaultBrowserInVisualStudioProgrammaticallyWithPowerShe ...

  9. MyBatisPLus入门项目实战各教程目录汇总

    https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194 http://www.imooc.com/article/details/id/ ...

  10. Comet OJ - Contest #3 (A 比赛 加强版)二分答案

    考试的时候同届神犇 JZYshurak 出了一个  n=$10^5$ 的数据加强版. 刚开始没什么思路,但是忽然想到这个可以转成二分判定+暴力枚举的模型. 二分 ans, 使得大于等于 ans 的值小 ...