Wednesday, October 30, 2019

Spring Boot: Importance of @AutoConfiguration


Everyone must have seen magic of spring boot, where it auto adds all the necessary dependencies based on presence of jar, property etc...

Behind the scene the magic is done by @AutoConfiguration annotation.

Instead of saying the magic is done by @Autoconfiguration, I would say actual work is done by @Conditional annotations.

@Conditional annotation allows us to configure beans based on anything like classType, jar, value of property param etc...

I would suggest you guys to read about @Conditional very well, the all magic is done by this annotation only. @AutoConfiguration is just a wrapper.



If you need to find out what auto-configuration is currently being applied, and why, start your application with the --debug switch. Doing so enables debug logs for a selection of core loggers and logs a conditions report to the console.

Spring Boot: Parent POM importance

Everyone must have written few Spring boot code and exposed some applications.

if you have noticed every Spring boot application has below as parent 

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

What does the above line signifies, if you remove this, the project might not build.

Actually this one parent does the following behind the scene
1. Add all necessary config, like java versions. basedir etc.
2. include all resource files to path
3. Add necessary maven plugins
4. Add all necessary spring resources with versions, every parent version has supported versions of all necessary versions of spring resources like activemq version, hibernate version, cassandra-driver version etc...


essense is instead of you managing versions for all dependencies, spring will take care of versions which are stable.

so it is of great use.

Spring Boot: Starter Annotations

Almost all of us developers have faced this issue, to add all dependencies for a particular project.

What strategy generally I use to follow, go to maven repo, copy required maven/gradle dependency string and  past into POM/build

isn't it good if we can centralize everything at a single place and add that as a parent in our POM ?

Many must have already done this, but in their own different way.

If Spring itself provides some parent POM which accumulate everything at a single place.
Just by adding that dependency in project POM, everything gets added.

All these POM's are called STARTERS.

Based on the requirement we can add appropriate Starter POM and all related dependencies will be added to our project

List of all starter POM's


spring-boot-starter-actuator
spring-boot-starter-amqp
spring-boot-starter-aop
spring-boot-starter-artemis
spring-boot-starter-batch
spring-boot-starter-cache
spring-boot-starter-cloud-connectors
spring-boot-starter-data-cassandra-reactive
spring-boot-starter-data-cassandra
spring-boot-starter-data-couchbase-reactive
spring-boot-starter-data-couchbase
spring-boot-starter-data-elasticsearch
spring-boot-starter-data-jdbc
spring-boot-starter-data-jpa
spring-boot-starter-data-ldap
spring-boot-starter-data-mongodb-reactive
spring-boot-starter-data-mongodb
spring-boot-starter-data-neo4j
spring-boot-starter-data-redis-reactive
spring-boot-starter-data-redis
spring-boot-starter-data-rest
spring-boot-starter-data-solr
spring-boot-starter-freemarker
spring-boot-starter-groovy-templates
spring-boot-starter-hateoas
spring-boot-starter-integration
spring-boot-starter-jdbc
spring-boot-starter-jersey
spring-boot-starter-jetty
spring-boot-starter-jooq
spring-boot-starter-json
spring-boot-starter-jta-atomikos
spring-boot-starter-jta-bitronix
spring-boot-starter-log4j2
spring-boot-starter-logging
spring-boot-starter-mail
spring-boot-starter-mustache
spring-boot-starter-oauth2-client
spring-boot-starter-oauth2-resource-server
spring-boot-starter-parent
spring-boot-starter-quartz
spring-boot-starter-reactor-netty
spring-boot-starter-rsocket
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-thymeleaf
spring-boot-starter-tomcat
spring-boot-starter-undertow
spring-boot-starter-validation
spring-boot-starter-web-services
spring-boot-starter-web
spring-boot-starter-webflux
spring-boot-starter-websocket
spring-boot-starter
spring-boot-starter-activemq

Spring Boot: Stereotype annotations

                                                  Spring has 4 Stereotype Annotation

@Component: Parent of all the Stereotype annotations, all stereotype annotations are internally annotated with @Component

@Controller: In MVC controller means which controls and rout the requests.

@Service: All Business logic holding classes should be annotated with @Service.

@Repository: All persistence layer interaction logic are written in DAO classes and all DAO classes should be annotated with @Repository. It also ensure all unchecked exception mapped to Spring DataAccessException.