Developing Plugins for Spring Boot in Java

 James Warner
  Dec 28, 2016

Technology: Spring Boot is the popular framework on Java platform, using this framework we can easily develop enterprise applications, and also it provides various starter projects different modules like web, security, amqp, batch applications etc. And also it provides a maven provides to package all the depended classes to flat jar which is runnable application. Spring boot framework provides easy to way to develop plugin on top spring boot framework.

Based on our company libraries we can write our own starter projects which will auto configure the required beans for spring boot applications. These auto configuration jars won`t need to write any code, we need to add them to class-path, spring boot will take care of creating beans behalf of us.

Understanding auto configured beans:

Auto-configuration classes are implemented using @Configuration classes, but it may also contain @Conditional annotations which are used specify when auto-configuration need to apply. We generally use @ConditionalOnClass, @ConditionalOnMissingBean annotations which will ensure that the specified beans only if the classes are present, and if the required beans are not present in context. There more conditional annotations like @ConditionalOnJava,@ConditionalOnExpression,@ConditionalOnWebApplication etc.

Locating Auto Configuration Classes:

By default spring boot check for existence of META-INF/spring.factories file, if present it will present for org.springframework.boot.autoconfigure.EnableAutoConfiguration key.

Example: org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\

org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\

org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\

org.springframework.boot.autoconfigure.MessageSourceAutoConfiguration

Ordering Auto Configuration classes:

Spring boot provides @AutoConfigureAfter, @AutoConfigureBefore annotations when we need to configure this auto-configuration class. For example if we want to provide configuration for web based applications we need to configure after @ConditionalOnWebApplication.

If we want to order auto-configuration classes then we can use @AutoconfigureOrder annotation and we can specify the order of that annotation, it will work similar way of @Order annotation but it will specific to auto-configuration classes.

Conditional Annotations:

  • Class conditions: The @ConditionalOnClass and @ConditionalOnMissingClass annotations are allows to include based on class present in class-path, the annotation meta-data is parsed by ASM, the value attribute points to real class even though class is not present on class-path. These annotations depend on OnClassCondition conditional class for matches.
  • Bean Annotations: The @ConditionalOnBean and @ConditionalOnMissingBean annotations allow a bean to be included based on the presence or absence of specific beans. You can use the value attribute to specify beans by type, or name to specify beans by name. The search attribute allows you to limit the ApplicationContext hierarchy that should be considered when searching for beans. These annotations depends on OnBeanCondition conditional class for matches.
  • Property Conditions: The @ConditionalOnProperty annotation allows configuration to be included based on a Spring Environment property. Use the prefix and name attributes to specify the property that should be checked. By default any property that exists and is not equal to false will be matched. We can also create more advanced checks using the havingValue and matchIfMissing attributes. These annotations will depend on OnPropertyCondition class for condition match.
  • Resource Conditions: The @ConditionalOnResource annotation allows configuration to be included only when a specific resource is present. Resources can be specified using the usual spring conventions.
  • Web Application Conditions: The @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations allow configuration to be included depending on whether the application is a 'web application'. A web application is any application that is using a Spring WebApplicationContext, defines a session scope or has a StandardServletEnvironment. These annotations will depend on OnWebApplicationCondition class for condition match.
  • SpEL expression conditions: The @ConditionalOnExpression annotation allows configuration to be included based on the result of a SpEL expression. These annotations is depend on OnExpressionCondition class for match.

Creating our own starters:

Spring boot starter will contains 2 modules.

autoconfigure module contains auto configuration code.

Starter module contains dependencies to auto-configure module and other dependencies to it. These starters are useful in starting auto-configure module.

Generating your own meta-data using the annotation processor: if our plugin depends on properties then we can use @ ConfigurationProperties annotation, we can also generate our own configuration meta-data using spring-boot-configuration-processor jar file. The jar includes a Java annotation processor which is invoked as our project is compiled and generate spring-configuration-metadata.json in META-INF folder. We can specific the default values for each property, and if it is a group property then we can specify the source type, if it contains any nested properties then we can specify the sourceMethod property.

Example: for group properties:

{

"name": "security",

"type": "org.springframework.boot.autoconfigure.security.SecurityProperties",

"sourceType": "org.springframework.boot.autoconfigure.security.SecurityProperties"

},

{

"name": "security.basic",

"type": "org.springframework.boot.autoconfigure.security.SecurityProperties$Basic",

"sourceType": "org.springframework.boot.autoconfigure.security.SecurityProperties",

"sourceMethod": "getBasic()"

}

Example: for individual properties:

{

"name": "security.basic.enabled",

"type": "java.lang.Boolean",

"description": "Enable basic authentication.”

"sourceType": "org.springframework.boot.autoconfigure.security.SecurityProperties$Basic",

"defaultValue": true

},

{

"name": "security.basic.path",

"type": "java.lang.String[]",

"description": "Comma-separated list of paths to secure.”

"sourceType": "org.springframework.boot.autoconfigure.security.SecurityProperties$Basic",

"defaultValue": ["/**"]

}

To use spring-boot-configuration-processor we need to simply add this dependency in build file (like pom.xml, build.gradle files) as optional dependency.

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-configuration-processor</artifactId>

<optional>true</optional>

</dependency>

dependencies {

optional "org.springframework.boot:spring-boot-configuration-processor"

}

compileJava.dependsOn(processResources)

}

We need to add compileJava.dependsOn(processResources) to build to ensure that resources are processed before code is compiled. Without this directive any additionalspring-configuration-metadata.json files will not be processed.

All the properties are detected via presence of getters, setters for specific collection types, these annotation processes also support @Data,@Getter,@Setter project Lombok annotations.

<dependency>

<groupId>com.mysema.maven</groupId>

<artifactId>apt-maven-plugin</artifactId>

<version>1.1.3</version>

</dependency>

The above dependency contains one file called javax.annotation.processing.Processor in META-INF/services folder has content org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor which is responsible for generating metadata file. The file will detected by JVM for annotation processing.

The above configuration will create metadata file each time of compilation, if we want to restrict every time, we need to disable annotation processing at time of compilation.

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<proc>none</proc>

</configuration>

</plugin>

Additional Configuration Meta-data: if the configuration properties not bound to @ConfigurationProperties then also we can add configuration properties to spring boot. We can create additional-springconfiguration-metadata.json file under META-INF folder.

It provides custom properties and also some hints, annotation processor will automatically merge the both configuration metadata files.

Enabling Plugin on need basis: if we specify the auto configuration files in spring.factories file, configuration files will loaded, suppose if we want to load the files on need basis, then we can write one annotation and import all auto-configuration classes instead of specifying in spring.factories file.

Example:

@Import(AsyncConfigurationSelector.class)

public @interface EnableAsync {

}

If multiple configuration classes are there we can separate with comma in import annotation.

If any of the classes annotated with @Configuration there we can add our annotation if we want to bootstrap our configuration, if the annotation we are not used the configuration files will not loaded.

Conclusion: Spring Boot is most recently used framework for enterprise applications, and its architecture provides easy way to create plugin on top it, if our company needs same bean configuration on multiple projects then we can create one starter module and we can share jar, so that it will be automatically without writing any code, it provides way to pass default properties to context using spring-configuration-metadata.json file using spring-boot-configuration-processor dependency, and also we can provide additional configuration by writing additional-springconfiguration-metadata.json file META-INF folder, and also we can restrict to load configuration only on need basis by writing custom annotation.

You can find some examples in below GitHub repository:

https://github.com/snicoll-demos/spring-boot-master-auto-configuration.git

The guide shared by java software development team is to let you know how to develop plugins for Spring Boot. If you have any question, ask in comments.

Developing Plugins for Spring Boot in Java

James Warner

James Warner is working as senior developer at NexSoftSys, a web application & java software development company in USA & India. Writes about Emergency technology, Innovative quotes, Social Media News and online marketing.

Popular posts

Content Writing as the Most Important Tool for Digital Marketing
May 24, 2019
Non- Renewable Energy: The Things You Need to Know
May 24, 2019
Scavenger Hunt: It is Easy If You Do It Smart
May 23, 2019
The Important Reasons Which Make It Necessary To Hire Auto Transportation Services
May 23, 2019
Other posts by James Warner
3 ways how AI helps in stepping up the marketing initiatives!
Sep 26, 2018
Chemical Testing On Animals, All Thanks To Big Data & AI
Aug 22, 2018
AI In Hollywood Is Already Gaining The Ground!
Aug 20, 2018
Azure and .NET : Making Beautiful Style Together
Aug 07, 2018
AI tools will sense your emotions by analyzing your facial expressions
Jul 27, 2018
  • Add Comment
    SEMICONDUCTOR ANALYTICS