打开 Settings,找到 Editor 目录下的 Live Templates
选中 Java,点击右侧的加号,创建一个新的模板
在创建模板的相关位置,填上对应的值
Abbreviation:触发的关键字(此处我使用的是 logg)
Description:模板的描述
Template text:具体的代码
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger($className$.class);
点击下方 Define 选择生效的场景
点击右侧 Edit variables,在 Expression 中选择 className()
在需要使用的地方输入自定义的关键字即可
背景:实现无需在代码中通过修改 spring.profiles.active=dev 配置信息才能切换环境的需求
maven 中提供了一个 profile 配置项,可以在打包时动态的指定环境配置
在 配置文件 中,更改 spring.profiles.active
spring.profiles.active = @env@
注意:修改为这种形式,在项目启动时可能会报错( found character ‘@’ that cannot start any token. (Do not use @ for indentation) ),如果报错,可以在 pom.xml 文件中增加
src/main/resources true
修改 pom.xml文件
dev dev true test test prod prod
此时,我们就默认以 dev 为工作环境了,如果需要切换,只需点开 maven 面板(如果找不到,可通过菜单 View -> Tool Windows -> Maven Projects 展示出来),选择对应的环境即可
参考自idea使用