首页 > 编程知识 正文

springboot开发环境,springboot判断开发正式环境

时间:2023-05-06 08:03:55 阅读:223805 作者:2119

开发环境

IDEA
Maven

配置文件

由于springboot会默认加载application配置文件,所以我们需要在application修改配置参数。
以下为application.yml文件格式

spring profiles active: dev

这段配置代码的意思是,spingboot会加载项目中的名字为application-dev的配置文件。
所以如果需要在打包时打包生产环境的包,那么创建一个名为application-prod.yml的配置文件,然后修改application.yml如下:

spring profiles active: prod

这样就可以区分打包生产环境和测试环境了

Maven

由于每次打包都需要手动修改application配置文件,会很麻烦并且不安全,并且大多数项目都是使用maven,所以集成maven可以使我们方便很多。

先配置pom.xml:

<!-- 在maven中添加如下配置 --><profiles> <profile> <!-- 测试环境 --> <id>dev</id> <properties> <profiles.active>dev</profiles.active> </properties> </profile> <profile> <!-- 生产环境 --> <id>prod</id> <properties> <profiles.active>prod</profiles.active> </properties> </profile> </profiles>

对这段代码做下说明,首先在maven中配置了两个环境的配置文件,一个测试环境test,一个生产环境prod;
其中
<profiles.active></profiles.active>是变量的key,test是变量的value

接下来在application中引用该变量

spring profiles active: @profiles.active@ <!-- 这里引用的是pom.xml中配置的key --> 问题

1.=='@' that cannot start any token. (Do not use @ for indentation)
在本地启动该项目时有时候会报如下错误

found character '@' that cannot start any token. (Do not use @ for indentation) in 'reader', line 4, column 11: name: @profiles.active@

 意思是识别不了@profiles.active@这个变量,这是因为这个变量没有被替换成我们需要的参数,如test,prod等,所以在本地启动时要加上参数启动,这样springboot会自动替换掉这个变量。

作者使用的是idea,所以启动springboot时在右上Edit Configurations-->Active Profiles 增加一个参数,参数值为你需要运行的环境名称,如dev

但是好像并没有什么用!!!

我的解决办法是:在application.yml中的active: @profiles.active@'改成active: '@profiles.active@' ,是的加个单引号。

spring profiles active: '@profiles.active@' <!-- 这里引用的是pom.xml中配置的key -->

当然,网上好像还有其它的解决办法:(下面的两种方法对我来说没用)

《SpringBoot中出现'@' that cannot start any token. (Do not use @ for indentation)....》

《SpringBoot的yaml配置文件,提示Do not use @ for indentation》

那么,如何部署阿里切换启动: java -jar -Dspring.profiles.active=prod XXXX.jar

你是技术宅么?那就加入我们吧~本博主不一定长期在线,可以进群大家一起解决问题~

商务合作@满意的小蝴蝶,谢谢!

欢迎加入 CSDN技术交流群 一起学习交流~

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。