Search code examples
javaspring-mvcspring-bootannotationsxml-configuration

Spring Boot how to create Auto configuration class


I am beginner for Spring Boot. When I am using any dependencies in Spring Boot, they have the auto configuration default.

My questions are:

  1. What is actually auto configuration class?
  2. How does auto configuration work?
  3. How to make out own auto configuration?

Please suggest me any blog that describes easy manner or please provide me any code snippet for better understanding.


Solution

  • The Spring Boot core package spring-boot-starter contains the spring-boot-autoconfigure package.

    What does it do? (from the JavaDoc)

    Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined. For example, If you have tomcat-embedded.jar on your classpath you are likely to want a TomcatEmbeddedServletContainerFactory (unless you have defined your own EmbeddedServletContainerFactory bean).

    Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration. You can always manually exclude() any configuration that you never want to apply (use excludeName() if you don't have access to them). You can also exclude them via the spring.autoconfigure.exclude property. Auto-configuration is always applied after user-defined beans have been registered.

    So each jar in your classpath that Spring can autoconfigure, Spring will autoconfigure for you to use in your application. Think about Hibernate, ThymeLeaf, Jackson etc.

    How do you use it?

    Simply add the @EnableAutoConfiguration in your application to make Spring autoconfigure your application (you possibly also need @SpringBootConfiguration).

    @SpringBootConfiguration
    @EnableAutoConfiguration
    // Or just @SpringBootApplication instead of the 2 above
    @Import(AppConfig.class)
    public class App {
      public static void main(String[] args) {
        SpringApplication.run(App.class);
      }
    }
    

    And your good to go.

    What can it configure for you? All of these tools below (got this from looking at the org.springframework.boot.autoconfigure package)

    admin
    amqp
    aop
    batch
    cache
    cassandra
    cloud
    condition
    context
    couchbase
    dao
    data
    analyzer
    domain
    jest
    flyway
    freemarker
    template
    gson
    h2
    hateoas
    hazelcast
    info
    integration
    jackson
    jdbc
    jersey
    jms
    jmx
    jooq
    kafka
    ldap
    liquibase
    logging
    mail
    mobile
    mongo
    mustache
    jpa
    reactor
    security
    sendgrid
    session
    social
    solr
    template
    thymeleaf
    transaction
    validation
    web
    webservices
    websocket
    

    How to create your own configuration?

    Don't know, never needed to do this. But this blog is a good starting point.