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. MYSQL8.0以上版本ROOT密码报错及修改

    在登录数据库过程中,如果遇到忘记root密码时,该如何解决? 1.使用管理员权限打开命令提示符,在命令行中输入: net stop mysql  2.待mysql服务停止后,输入: mysqld -- ...

  2. mac使用sourcetree跳过注册

    转自https://blog.csdn.net/qq_32890891/article/details/89216954 打开sourcetree 关闭sourcetree 命令终端输入default ...

  3. pymysql 1064, 'You have an error in your SQL syntax; check the manual that corresponds to

    在python 连接mysql时,最近一直出现了 1064, 'You have an error in your SQL syntax; check the manual that correspo ...

  4. C# 直播录制视频

    //项目引用 ffmpeg.exe 下载地址http://ffmpeg.org/ var time = DateTime.Now; ; //录制分钟 var fileName = Guid.NewGu ...

  5. ZZNUOJ-2157: 水滴来袭-【干扰阅读-卡模糊精度1e-8的问题】

    ZZNUOJ-2157: 水滴来袭 那是一个冷雨霏霏的秋天的下午,当罗辑拿着枪威胁三体文明的时候,如果过了三十秒三体人还没有同他展开谈判,罗辑就会扣动扳机即刻结束自己的生命,随后他身上的核弹控制器就会 ...

  6. js 同步 异步 宏任务 微任务 文章分享

    分享一篇 写的很好的 宏任务 微任务  同步异步的文章 文章原地址: https://juejin.im/post/59e85eebf265da430d571f89 这一次,彻底弄懂 JavaScri ...

  7. 2019牛客暑期多校训练营(第五场)F maximum clique 1 二分图求最大独立集

    https://ac.nowcoder.com/acm/contest/885/F #include <bits/stdc++.h> //CLOCKS_PER_SEC #define se ...

  8. JS BOM基础 全局对象 window location history screen navigator

    全局变量声明的两种方式:1,window.变量名=值;2,var 变量名=值; 全局函数声明的两种方式:1,window.函数名=function(){}2,function 函数名=function ...

  9. Linux下shell命令执行过程简介

    Linux是如何寻找命令路径的:http://c.biancheng.net/view/5969.html Linux上命令运行的基本过程:https://blog.csdn.net/hjx5200/ ...

  10. fputcsv

    1.a是從結尾添加,w是從頭添加,不知爲什麽 這樣寫最後一行會是錯的,似乎不會自動添加換行符,只有在最後以行寫上"\r\n",才會正確. $fp = sugar_fopen('fi ...