Skip to content

Spring boot yml config and Intellij navigation

Notifications You must be signed in to change notification settings

brobert83/nav-yml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repo illustrates how to setup a Spring boot app using yml based properties

By using this setup navigation is possible from the application.yml file to where the properties are bound in code, and back (Crtl+B, Crtl+click, etc)

Notice this dependency in pom.xml

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

How to enable annotation processing in Intellij

alt text

Properties not bound are highlighted

alt text

The magic

This is what is basically needed, then just inject that bean wherever

@Configuration
@ConfigurationProperties("my.living.rent")
class Address {

    private String city;
    private String country;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Also, @EnableConfigurationProperties

@SpringBootApplication
@EnableConfigurationProperties
public class NavYmlApplication {

	public static void main(String[] args) {
		SpringApplication.run(NavYmlApplication.class, args);
    }

}

This is how it should look in the class, by clicking those green leafs the ide will take you to the location in the yml where that prop is declared

alt text

Notes:

  • it does not work in combination with Lombok (@Getter @Setter)
  • the annotation processing must be enabled in Intellij
  • I've noticed that sometimes the IDE needs some time to make the binding happen (refresh the maven config)

About

Spring boot yml config and Intellij navigation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages