博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IDEA 2021 开发 springboot springcloud springcloud Alibaba应用时application.yml配置自动提示
阅读量:3958 次
发布时间:2019-05-24

本文共 1908 字,大约阅读时间需要 6 分钟。

文章目录

在这里插入图片描述

IDEA 2021 开发 springboot springcloud springcloud Alibaba应用时application.yml配置自动提示

方案一:

IDEA开发springboot应用时application.yml配置自动提示

在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题。不过我认为更优雅的方式是定义自己的属性类统一管理,这样在idea中,既能自动提示,又能对配置进行分类管理,显得有条不紊,下面是具体的配置步骤。

第一步:添加依赖(分为maven和gradle两种方式)

1.1 如果你使用的是maven

增加依赖

org.springframework.boot
spring-boot-configuration-processor

1.2 如果你使用的是gradle

增加依赖并且配置annotationProcessor

compileOnly 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor ‘org.springframework.boot:spring-boot-configuration-processor’

第二步:创建配置属性类

@Data @ConfigurationProperties(prefix = “myapp.prefix") public class MyAppProperties {
private String prop1; private int prop2; }

第三步:在配置类中增加注解

@Configuration @EnableConfigurationProperties(MyAppProperties.class) public class MyConfig {
}

第四步:使用属性类

@Component public class MyComponent {
private final MyAppProperties properties; public MyComponent(MyAppProperties properties) {
this.properties = properties; } // 现在可以使用了 }

对了,别忘记配置你的application.yml

myapp:  prefix:    prop1: 1111    prop2: 2222或 application.propertiesmyapp.prefix.prop1=1111 myapp.prefix.prop2=2222

另外需要注意:如果想要idea中自动提示生效,需要重新运行你的应用,有时候没那么快生效。

方案二:

安装插件 :spring assistant

1.file—>setting–>plugin 搜索spring assistant安装

确定是否允许annotation processing,打卡 Settings > Build, Execution & Deployment > Compiler > Annotation Processors

Make sure you add the following changes to to your project

在这里插入图片描述

For Maven

添加以下依赖

org.springframework.boot
spring-boot-configuration-processor
true

For Gradle

添加

dependencies {
optional "org.springframework.boot:spring-boot-configuration-processor"}

如果项目里面的application的配置文件后缀为properties改为yml,两种配置文件只是语法有区别,这里因为习惯yaml的写法所以,倾向于使用yml

打开src\main\resources\application.yml,尝试输入,可以看到提示成功

在这里插入图片描述

如果大家觉得还不错,点赞,收藏,分享,一键三连支持我一下~

转载地址:http://zfazi.baihongyu.com/

你可能感兴趣的文章
赏心悦目的宏代码
查看>>
理解套接字recv(),send()
查看>>
发一个C++写的跨平台的BlockingQueue
查看>>
Linux TCP/IP协议栈剖析【体系结构篇】
查看>>
游戏开发中预防内存泄露的一些措施
查看>>
以前的文章全部移除了。
查看>>
几首歌
查看>>
蝴蝶泉边
查看>>
编码转换
查看>>
freerice
查看>>
Does your mother know
查看>>
《写出质量好软件的75条体会》暨答案ZT [转自monkyy的blog]
查看>>
关于详细设计
查看>>
POJ2838,Sliding Window(单调队列)
查看>>
牛客练习赛50,B tokitsukaze and Hash Table(STL+输入输出挂)
查看>>
POJ3728,The merchant(倍增LCA+分治)
查看>>
2019 ICPC Malaysia National,E. Optimal Slots(01背包变形)
查看>>
洛谷P1638 逛画展(双向队列)
查看>>
牛客练习赛51,D(二分图匹配)
查看>>
POJ2892,Tunnel Warfare(线段树维护连续区间)
查看>>