Boaris
Boaris

Reputation: 5236

@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

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 Gradle project with Spring Boot Initializer (I haven't checked anything at all).
  2. Created a new class Properties.

When I annotated it with @ConfigurationProperties, the next notification has appeared: notification

The documentation said that I should add the following to my project:

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

compileJava.dependsOn(processResources)

After that, I tried to rebuild the project and enable annotation processors in settings but the notification hasn't gone. Completion doesn't work too (I created a string my).

Upvotes: 140

Views: 160918

Answers (16)

Ravi Pandey
Ravi Pandey

Reputation: 157

For this issue, we need to use annotations on the top of the main class of the project and mention this annotation to enable the

@EnableConfigurationProperties(AccountsContactInfoDto.class)

Upvotes: 0

Asad
Asad

Reputation: 61

This error can be resolved by adding this annotation in your mainApplicaitonClass

@EnableConfigurationProperties({class name}.class)

Upvotes: 0

anandbibek
anandbibek

Reputation: 2604

For those who are using maven, Intellij was still not happy with the addition of dependency. Seems like adding annotationProcessorPaths via maven-compiler-plugin finally tamed the beast.

Make sure the version matches your spring dependencies. I suspect it would be already present in your effective POM.

Reason: I was using a custom parent-pom which had a mapstruct annotation processor set in annotationProcessorPaths and that actually triggered IntelliJ to ask for all other annotation processors to be specified manually as well.

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <version>${project.parent.version}</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
</plugins>
</build>

Upvotes: 45

Peter
Peter

Reputation: 1602

Spring's website has a tutorial that contains a great explanation how to make it work. Just follow the steps under "Configuration properties" section below.

It has one of the steps which none of other answers mentioned here: run ./gradlew kaptKotlin manually.

As of Mar 2021, IDEA will still show the warning on top of the Kotlin properties class, but the properties will be recognized and work. You'll be able to navigate from .properties file to the Kotlin properties class, but not the other way around.

https://spring.io/guides/tutorials/spring-boot-kotlin/ scroll down to "Configuration properties" part.

Or the same page on GitHub:

https://github.com/spring-guides/tut-spring-boot-kotlin/blob/master/README.adoc#configuration-properties

Upvotes: 0

Mafei
Mafei

Reputation: 3819

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);
    }

}

Upvotes: 5

JeffersonSousa
JeffersonSousa

Reputation: 541

For those using Maven or Gradle, just add the dependency on spring-boot-configuration-processor.

Maven:

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

Gradle 4.5 and earlier

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

Gradle 4.6 and later

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

For more information: "Generating Your Own Metadata by Using the Annotation Processor" https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

Upvotes: 27

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

According to the Spring Boot docs, the correct configuration for Gradle 4.5 and earlier is

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

Upvotes: 0

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

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.

Upvotes: 46

naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37993

For Kotlin projects, the working configuration since Gradle 4.6 is using annotation processor

apply plugin: "kotlin-kapt"

dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
    compileOnly("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
}

Upvotes: 7

Oleg Pavlyukov
Oleg Pavlyukov

Reputation: 161

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

Maybe somebody helps.

Upvotes: 10

Victor Schurenko
Victor Schurenko

Reputation: 201

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.

Upvotes: 20

AR1
AR1

Reputation: 5023

In version 2018.3 of IntelliJ, I solved this problem (as per this documentation) in the following way:

With Gradle 4.5 and earlier, the dependency should be declared in the compileOnly configuration, as shown in the following example:

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

With Gradle 4.6 and later, the dependency should be declared in the annotationProcessor configuration, as shown in the following example:

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

Upvotes: 7

vargapeti
vargapeti

Reputation: 301

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') 

Upvotes: 2

Dirk Hoffmann
Dirk Hoffmann

Reputation: 1573

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)

Upvotes: 0

Icex
Icex

Reputation: 796

I had the same problem. I use idea 2017.2 and gradle 4.1, and some blog said you should add:

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

But I changed it to this:

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

And the warning is gone.

Upvotes: 78

Boaris
Boaris

Reputation: 5236

I forgot to add propdeps-plugin. However, I remember that it didn't work for me even with the plugin on 2016.3, So as @CrazyCoder mentioned, try to downgrade Gradle or download the new 2017.1 version (details).
Also you may receive Re-run Spring Boot Configuration Annotation Processor to update generated metadata when you will solve this issue. For this, click Refresh all Gradle projects (in Gradle side menu).

Upvotes: 11

Related Questions