JavaScript Required

We're sorry, but we doesn't work properly without JavaScript enabled.

Looking for an Expert Development Team? Take two weeks Trial! Try Now

Manual Bean Definitions in Spring Boot

Technology: SpringBoot is one of the most used framework for Rapid Development using Spring frameworks. It provides many features out of box like auto configuration. But because of all features startup time for the application may increase.

Some foe the features provided by Spring boot out of the box features we may not need for some applications, these configurations we can disable for better performance in term of memory and also execution time.

Spring Framework provides @Conditional annotation to load provide bean for various conditional specific beans like if the profile is dev load some beans, if profile is prod load other beans.

Spring Boot is implemented this conditional way in a broader way to configure the beans based on classpath scanning.

We will create a sample web application with single endpoint hello.

@RestController publicclassHelloController { @GetMapping("/hello") publicStringsayHelloWorld() { return"Hello world"; } } And @SpringBootApplication publicclassSpringBootApplicationExample { publicstaticvoidmain(String[] args) { SpringApplication.run(SpringBootApplicationExample.class, args); } }

And if we try to start using debug mode by adding property debug=true in application.properties file, then we can observe the below lines in console.

CONDITIONS EVALUATION REPORT

Positive matches:

CodecsAutoConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer' (OnClassCondition) CodecsAutoConfiguration.JacksonCodecConfiguration matched: - @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper' (OnClassCondition)

Negative matches:

ActiveMQAutoConfiguration:

Did not match: - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition) AopAutoConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.aspectj.lang.annotation.Aspect' (OnClassCondition) ArtemisAutoConfiguration: Did not match: - @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)

Exclusions:

None Unconditional classes: org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration and also 2019-01-26 12:57:05.417 DEBUG 5420 --- [ main] inMXBeanRegistrar$SpringApplicationAdmin : Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'

All these configuration is came from our classpath scan of the application, insteadofclasspath-scan if we can provide the autoconfiguration classes, spring boot won`t perform theclasspath-scan and startup can be reduced.

And some of the features like JMX, if we are not using in our application then we easily disable using spring.jmx.enabled=false, which can increase the performance of the application and also decrease startup time.

Suppose if we specify only configurations needed for the application like

@Import({ DispatcherServletAutoConfiguration.class, EmbeddedWebServerFactoryCustomizerAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, JacksonAutoConfiguration.class, HttpHandlerAutoConfiguration.class, ConfigurationPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })

Then startup will be decreased, the drawback of this order of the specifying beans is so important. We can overcome by creating new custom annotation and specifying the configuration files in factories file.

@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @ImportAutoConfiguration public @interface HelloApplicationConfiguration { } And create META-INF/spring.factories file org.sravan.HelloApplicationConfiguration = \ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\ org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration

The advantages of doing this are that the application code no longer has to manually enumerate the configurations, and also that the ordering is now taken care of by Spring Boot. The disadvantage is that it is only useful for applications which need precisely these features, and has to be replaced or augmented in any application that wants to do something a bit different. It is still fast though - Spring Boot does a little bit of extra work for the book keeping, but not really very much.

Conclusion

we can use springboot as a convenient collection of pre-test and prepared configurations, by default all majority of the features are enabled, but we can control all features using annotations.The smaller number of features you use, the fewer classes are loaded, which lead to faster startup. And also we learned how to improve the application performance using custom annotation. Contact Aegis Infoways for Java Development Outsourcing Services.