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. vue.js 初步学习

    跟着b站上的视频来学 首先什么是vue.js? 跟着b站上视频来学:(o゚v゚)ノ <!DOCTYPE html> <html lang="en"> < ...

  2. 将mysql从5.5.25升级到8.0.12

    将mysql从5.5.25升级到8.0.12过程中遇到几个问题,记录如下: 将数据库安装好后,导入原来的数据,启动tomcat,报错unable to load authentication   ca ...

  3. CodeForces 822C Hacker, pack your bags!

    题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...

  4. 【异常】airflow-psutil/_psutil_common.c:9:20: fatal error: Python.h: No such file or directory

    1 异常信息 usr/include/python3.6m -c psutil/_psutil_common.c -o build/temp.linux-x86_64-3.6/psutil/_psut ...

  5. linux设置自动同步服务器时间

    最近遇到一个问题,由于两台服务器时间的问题,经常导致用户登录由于时间差问题而报错,再三百度,最后整理了一下修改linux定时同步的操作(本方法适用于有自己时间服务器,没有的只限于借鉴) 首先确认,我们 ...

  6. 用js刷剑指offer(二叉树的镜像)

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  7. Java锁--Condition

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496716.html Condition介绍 Condition的作用是对锁进行更精确的控制.Condi ...

  8. node.js接收异步任务结果的两种方法----callback和事件广播

    事件广播 发送方调用emit方法,接收方调用on方法,无论发送方或是接收方,都会工作在一个频道 声明了一个模块,用于读取mime.json中的记录 var fs = require('fs'); va ...

  9. hdu5183Negative and Positive (NP))——手写Hash&&模板

    题意:问是否存在一段区间其加减交错和为K. 显然,我们可以用set保存前缀和,然后枚举一个端点查找.具体的 若在st1中查找 $t$,为 $sum-t=-k$,在st2中则是 $sum-t=k$. 注 ...

  10. Self install windows service in .NET c#

    http://stackoverflow.com/questions/4144019/self-install-windows-service-in-net-c-sharp using System; ...