@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

前端 未结 13 1047
太阳男子
太阳男子 2021-02-02 05:06

I try to make completion for custom properties in Spring Boot.

I tried to create a simple project via IntelliJ IDEA 2016.3:

  1. Created a new G
相关标签:
13条回答
  • 2021-02-02 05:37

    You have to mention in the main class the class you want to use @ConfigurationProperties annotation like below.

    @EnableConfigurationProperties(AppProperties.class)
    

    The Property Configuration class with @ConfigurationProperties will be like this

    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    
    @ConfigurationProperties(prefix = "app")
    public class AppProperties {
        String name;
        String id;
    }
    
    
    

    The Main class will be like this

    import com.auth2.demo.config.AppProperties;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(AppProperties.class)
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    
    0 讨论(0)
  • 2021-02-02 05:39

    According to the Spring Boot docs, the correct configuration since Gradle 4.6 is

    dependencies {
        annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
        // ...
    }
    

    IntelliJ IDEA supports annotationProcessor scope since build 193.3382 (2019.3). Don't forget to enable annotation processing in IntelliJ IDEA settings.

    0 讨论(0)
  • 2021-02-02 05:39

    In maven project helps adding dependency spring-boot-configuration-processor and marking main class with @EnableConfigurationProperties(AppProperties.class).

    Maybe somebody helps.

    0 讨论(0)
  • 2021-02-02 05:44

    It happens to me for two reasons in IDEA:

    1. Double check if your setting is picked (enabled) in IDEA: Preferences->Annotation Processors->Enable annotation processing.
    2. After update your Idea, check your plugins and update them. It happens that plugins become incompatible with your new IDEA version, so just click to update them.
    0 讨论(0)
  • 2021-02-02 05:44

    I had the same problem with IntelliJ version 2018.1.2. I also had to define the actual version of spring-boot-configuration-processor in order to get it worked:

    compile('org.springframework.boot:spring-boot-configuration-processor:2.0.1.RELEASE') 
    
    0 讨论(0)
  • 2021-02-02 05:44

    following works for me:

    buildscript {
        repositories {
            jcenter()
            maven { url 'https://repo.jenkins-ci.org/public/' }
            maven { url 'http://repo.spring.io/plugins-release' }
        }
        dependencies {
            classpath "io.spring.gradle:propdeps-plugin:0.0.9.RELEASE"
        }
    }
    
    ...
    
    apply plugin: 'propdeps'
    apply plugin: 'propdeps-eclipse'
    apply plugin: 'propdeps-idea'
    
    ...
    
    dependencyManagement {
        imports {
            mavenBom 'org.springframework.boot:spring-boot-starter-parent:2.0.0.RELEASE'
        }
    }
    
    ...
    
    dependencies {
        compile "org.springframework.boot:spring-boot-starter"
        compile "org.springframework.boot:spring-boot-starter-actuator"
        annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" // for @ConfigurationProperties, make sure compileJava.dependsOn(processResources)
        ...
    }
    
    compileJava.dependsOn(processResources)
    
    0 讨论(0)
提交回复
热议问题