Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cristianprofile/9bd520c4f085b84058992b6084bc4980 to your computer and use it in GitHub Desktop.
Save cristianprofile/9bd520c4f085b84058992b6084bc4980 to your computer and use it in GitHub Desktop.
Spring boot and Intellij custom properties autocomplete
Code completion for custom properties in Spring Boot
May 16, 2015
All three major IDEs (IntelliJ IDEA, Eclipse and Netbeans) provide either plugins or built-in features for code completion in application.properties files in a Spring Boot project:
Besides completion, you also see, that all properties are typed (and validated), and if present a Javadoc is shown.
Fortunately this feature can also be used for custom defined properties. There are 2 prerequisites for that:
You have to use classes annotated with @ConfigurationProperties which holds fields that are mapped to your properties
You have to add the spring-boot-configuration-processor plugin as dependency to your project
Part 1: @ConfigurationProperties
Spring Boot provides the annotation @ConfigurationProperties which can be used on classes, that hold the property values at runtime.
@ConfigurationProperties(prefix = "hornetq_health")
@Getter
@Setter
public class HornetQHealthConfigurationProperties {
/**
* Threshold per queue name.
*/
private Map<String, Integer> thresholds;
private Integer count;
private List<String> list;
}
The property name is generated from the prefix attribute plus the field names, in this case there are three properties available:
hornetq_health.thresholds
hornetq_health.count
hornetq_health.list
Javadoc must be on the field, not on the getter or setter to take effect.
Part 2: Configure the spring-boot-configuration-processor plugin
To activate the plugin, just add it as optional dependency in your POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
If you recompile (or make) the project, the plugin will create a JSON file in the path target/classes/META-INF/spring-configuration-metadata-json. The file contains a list of all properties with type and Javadoc information and will be evaluated by the IDE plugins.
http://www.mdoninger.de/2015/05/16/completion-for-custom-properties-in-spring-boot.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment