springboot学习1
2024-08-23 22:39:19
gradle环境配置
https://www.w3cschool.cn/gradle/ctgm1htw.html
Spring profile 多环境配置管理
参考:https://www.cnblogs.com/pangguoming/p/5888871.html
本地、测试、开发、产品等不同环境文件配置
现象
如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法。
解决
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
使用也是非常方便。只要在applicationContext.xml中添加下边的内容,就可以了
<!-- 开发环境配置文件 --> <beans profile="test"> <context:property-placeholder location="/WEB-INF/test-orm.properties" /> </beans> <!-- 本地环境配置文件 --> <beans profile="local"> <context:property-placeholder location="/WEB-INF/local-orm.properties" /> </beans>
profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样
激活 profile
spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里我们通过定义 JVM 参数实现。
1、ENV方式:
ConfigurableEnvironment.setActiveProfiles("test")
2、JVM参数方式:
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件
set JAVA_OPTS="-Dspring.profiles.active=test"
eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传git追踪管理
-Dspring.profiles.active="local"
3、web.xml方式:
<init-param> <param-name>spring.profiles.active</param-name> <param-value>production</param-value> </init-param>
4、标注方式(junit单元测试非常实用):
@ActiveProfiles({"unittest","productprofile"})
下载项目demo
链接:https://start.spring.io/
gradle命令行构建项目
E:\myfiles\springboot\helloworld>gradle build E:\myfiles\springboot\helloworld\build\libs>java -jar demo-1.0.0.jar
访问 localhost:8080
build.gradle文件配置
plugins { id 'org.springframework.boot' version '2.1.5.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '1.0.0'//版本号根据需要改 sourceCompatibility = '1.8'//编译时的jdk版本 repositories { //mavenCentral() maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}//配置阿里云仓库 } //依赖 dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
最新文章
- Delphi写的DLL回调C#
- “Ceph浅析”系列之七——关于Ceph的若干想法
- CentOS_PHP_NGINX_FastCGI
- Android菜鸟成长记2-内部类
- poj2780Linearity(多点共线)
- 拆分ABBYY FineReader 12文档的方法
- static和public
- Delphi读取Word
- MS SQL SERVER: msdb.dbo.MSdatatype_mappings &; msdb.dbo.sysdatatypemappings
- MVC小系列(二十一)【带扩展名的路由可能失效】
- git版本工具(团队开发常用)
- SQLite3的使用(用到了dll)good
- 关于SVN工具的配置及使用
- JAVA_新建一个方法并且求三个数中的最大值
- c3p0数据源的第一次尝试
- log4.net 配置-控制台输出带颜色的日志
- 从Git上导入Maven 项目到Eclipse
- 振兴中华|2013年蓝桥杯A组题解析第三题-fishers
- Docker深入浅出2
- [ldap]ldap server安装以及图形化操作
热门文章
- 20175215 2018-2019-2 第六周java课程学习总结
- java期末总结
- ggsci: error while loading shared libraries: libnnz11.so: cannot open shared object file
- CSS 有序或者无序列表的前面的标记 list-style-type 属性
- Java学习之==>;JDBC
- 使用IDEA工具创建本地项目并且上传到码云
- SSH整合项目----在线商城
- 【css】常用的几种水平垂直居中方式与盒子模型,面试经常问到!
- 【miscellaneous】监狱智能视频监控系统设计解决方案
- Android 透明主题