IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示

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

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

1.1 如果你使用的是maven

增加依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

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.properties

myapp.prefix.prop1=1111
myapp.prefix.prop2=2222

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

原文地址:https://www.cnblogs.com/imxiangli/p/12151498.html

时间: 2024-08-29 15:27:37

IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示的相关文章

Spring Boot 项目构建 之 使用 Spring Boot 构建应用(Building an Application with Spring Boot)

Table of contents What you'll build What you'll need How to complete this guide Build with Gradle Build with Maven Build with Spring Tool Suite Learn what you can do with Spring Boot Create a simple web application Create an Application class Run the

Spring Boot 学习之路二 配置文件 application.yml

一.创建配置文件 如图所示,我们在resources文件夹中新建配置文件application.yml 结构图 二.一些基本配置 server: port: 8090 //配置端口 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: //数据库配置 url : jdbc:mysql://localhost:3306/newbirds username : root pas

使用idea maven开发spring boot 分布式开发入门

1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.

Spring boot 如何读取jar包外面的properties文件

先来张target跟目录的文件结构 conf文件夹中存放的是properties文件, lib文件夹中存放的是各种jar文件, start.bat是windows命令行的启动文件. 来张properties文件的内容 1. 创建配置文件相关的配置类,每个properties文件对应一个配置类 这里使用了@ConfigurationProperties, prefix是指properties文件中的前缀, 比如 asdf.name, 前缀就是asdf,  实体类中的name对应着asdf后面的na

application.yml和application.properties文件的区别

maven项目 .yml文件时树状结构,层级浅时比较方便,层级深的时候就比较麻烦了 .properties文件时属性访问结构,层级深浅对它来说是一样的,而且相较于.yml类型的文件比较好配置,但缺点也很明显--要重复写很多遍父级属性: 原文地址:https://www.cnblogs.com/lishidefengchen/p/11063294.html

Spring Boot? 配置文件详解:自定义属性、随机数、多环境配置等

自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: name: 程序猿DD title: Spring Boot教程 desc: ${com.didispace.blog.name}正在努力写<${com.didispace.blog.title}> # 随机字符串 value: ${random.value} # 随机int number: ${

Spring Boot属性配置文件详解

自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: com.example.blog.name=zzh com.example.blog.title=hello springboot @Component public class BlogProperties { @Value("${com.example.blog.name}") private String name; @Value("${com.exa

spring-boot实战【08】【转】:Spring Boot属性配置文件详解

相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用. 在之前的各篇文章中都有提及关于application.pro

spring boot 1.5.4 配置文件详解(八)

上一篇:spring boot 1.5.4 集成spring-Data-JPA(七) 1      Spring Boot配置文件详解 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在appli