Springboot Series: @SpringBootApplication annotation

Source: Internet
Author: User
Tags cassandra documentation solr hazelcast

When using the Springboot framework for development, we usually add @SpringBootApplication annotations to the main function, and today we will explain @SpringBootApplication, if there are any mistakes, please comment. @SpringBootApplication

@SpringBootApplication source code is as follows:

@Target ({elementtype.type})
@Retention (retentionpolicy.runtime)
@Documented
@Inherited
@ Configuration
@EnableAutoConfiguration
@ComponentScan public
@interface springbootapplication {
    Class<?>[] Exclude () default {};

    String[] Excludename () default {};

    @AliasFor (
        annotation = componentscan.class,
        attribute = "Basepackages"
    )
    string[] Scanbasepackages () default {};

    @AliasFor (
        annotation = componentscan.class,
        attribute = "basepackageclasses"
    )
    class<?> [] scanbasepackageclasses () default {};
}

Learn from the source @SpringBootApplication be decorated with @Configuration, @EnableAutoConfiguration, @ComponentScan annotations, in other words springboot Provides a unified annotation to replace the above three annotations, simplifying the configuration of the program. The following explains the functions of each annotation. @Configuration

Spring Documentation Description:

@Configuration is a class-level annotation indicating the. Object is a source of bean definitions. @Configuration classes declare beans via public @Bean annotated methods.

@Configuration is a class-level comment that indicates that the object is the source of a bean definition. @Configuration class declares a bean through a public method that @bean annotations.

The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed By the Spring IoC container.

@Bean annotations are used to represent a method instantiation, and configuration and initialization is a new object managed by the Spring IoC container.

Popular speaking @Configuration commonly used in conjunction with @Bean annotations, using @Configuration annotation class equivalence and XML configuration beans, the @Bean annotation method is equivalent to the XML configuration Bean. An example is provided:

The XML configuration code is as follows:

<beans>
    <bean id = "UserService" class= "Com.user.UserService" >
        <property name= "Userdao" ref = " Userdao "></property>
    </bean>
    <bean id =" Userdao "class=" Com.user.UserDAO "></bean >
</beans>

Equivalent to

Package org.spring.com.user;

Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

@Configuration public
class Config {
    @Bean public
    userservice Getuserservice () {
        UserService UserService = new UserService ();
        Userservice.setuserdao (null);
        return userservice;
    }

    @Bean public
    Userdao Getuserdao () {return
        new Userdao ();
    }
}
Package org.spring.com.user;

public class UserService {
    private Userdao Userdao;

    Public Userdao Getuserdao () {return
        Userdao;
    }

    public void Setuserdao (Userdao userdao) {
        This.userdao = Userdao;
    }
}
Package org.spring.com.user;

public class Userdao {
}

Note: The method of using public modifiers @Bean annotations; userservice, Userdao classes need not be declared as @Component, @Service, @Repository, @Controller;

Look at the above Java code, perhaps someone will ask, why not @Autowired annotations directly injected into the Userdao. I think so. @Configuration, @Bean pay more attention to the configuration aspect, for example we can set the special information of the Userdao class object in the @Bean method, such as the designation TransactionManager and so on. @EnableAutoConfiguration

Spring Documentation Description:

Enable Auto-configuration of the spring application context, attempting to guess and configure beans-you are likely T o need. Auto-configuration classes are usually applied based on your classpath and what to beans you have.

Enables automatic configuration of the Spring application context, trying to guess and configure the beans that you might need. Automatic configuration classes are typically applied based on your classpath and defined beans objects.

The package of the class is annotated with @EnableAutoConfiguration has specific significance and is often used as a ' Default '. For example, it is used when scanning for@entity classes. It is generally recommended, place@enableautoconfiguration in a root package so, all sub-packages and classes can be searched.

The packages that are @EnableAutoConfiguration annotated classes have a specific meaning and are used as the default configuration. For example, when scanning a @Entity class, it uses this. It is usually recommended that the @EnableAutoConfiguration be configured under the root package so that all of the child packages and classes can be found.

Auto-configuration classes are regular Spring configuration beans. They are located using the Springfactoriesloader mechanism (keyed against this class). Generally auto-configuration beans are @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnMissing Bean annotations).

The Auto-configuration class is a regular Spring configuration Bean. They use the Springfactoriesloader mechanism (key in the Enableautoconfiguration classpath). Usually auto-configuration beans is @Conditional beans (in most cases with @ConditionalOnClass and @ConditionalOnMissingBean annotations).

Springfactoriesloader mechanism:
Springfactoriesloader will query for jars containing meta-inf/spring.factories files. When the spring.factories file is found, Springfactoriesloader will query for the properties named by the configuration file. The key value for Enableautoconfiguration is org.springframework.boot.autoconfigure.EnableAutoConfiguration. The spring configuration is based on the value corresponding to this key. In the Spring-boot-autoconfigure.jar file, contains a spring.factories file that reads as follows:

# initializers Org.springframework.context.applicationcontextinitializer=\
Org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer # application Listeners
Org.springframework.context.applicationlistener=\
Org.springframework.boot.autoconfigure.BackgroundPreinitializer # Auto Configure
Org.springframework.boot.autoconfigure.enableautoconfiguration=\
Org.springframework.boot.autoconfigure.admin.springapplicationadminjmxautoconfiguration,\
Org.springframework.boot.autoconfigure.aop.aopautoconfiguration,\
Org.springframework.boot.autoconfigure.amqp.rabbitautoconfiguration,\
Org.springframework.boot.autoconfigure.messagesourceautoconfiguration,\
Org.springframework.boot.autoconfigure.propertyplaceholderautoconfiguration,\
Org.springframework.boot.autoconfigure.batch.batchautoconfiguration,\
Org.springframework.boot.autoconfigure.cache.cacheautoconfiguration,\ Org.springframework.boot.autoconfigure.cassandra.cassandraautoconfiguration,\ Org.springframework.Boot.autoconfigure.cloud.cloudautoconfiguration,\
Org.springframework.boot.autoconfigure.context.configurationpropertiesautoconfiguration,\
Org.springframework.boot.autoconfigure.dao.persistenceexceptiontranslationautoconfiguration,\
Org.springframework.boot.autoconfigure.data.cassandra.cassandradataautoconfiguration,\
Org.springframework.boot.autoconfigure.data.cassandra.cassandrarepositoriesautoconfiguration,\
Org.springframework.boot.autoconfigure.data.elasticsearch.elasticsearchautoconfiguration,\
Org.springframework.boot.autoconfigure.data.elasticsearch.elasticsearchdataautoconfiguration,\
Org.springframework.boot.autoconfigure.data.elasticsearch.elasticsearchrepositoriesautoconfiguration,\
Org.springframework.boot.autoconfigure.data.jpa.jparepositoriesautoconfiguration,\
Org.springframework.boot.autoconfigure.data.mongo.mongodataautoconfiguration,\
Org.springframework.boot.autoconfigure.data.mongo.mongorepositoriesautoconfiguration,\ Org.springframework.boot.autoconfigure.data.soLr.
Solrrepositoriesautoconfiguration,\ org.springframework.boot.autoconfigure.data.redis.redisautoconfiguration,\
Org.springframework.boot.autoconfigure.data.rest.repositoryrestmvcautoconfiguration,\
Org.springframework.boot.autoconfigure.data.web.springdatawebautoconfiguration,\
Org.springframework.boot.autoconfigure.freemarker.freemarkerautoconfiguration,\
Org.springframework.boot.autoconfigure.gson.gsonautoconfiguration,\
Org.springframework.boot.autoconfigure.h2.h2consoleautoconfiguration,\
Org.springframework.boot.autoconfigure.hateoas.hypermediaautoconfiguration,\
Org.springframework.boot.autoconfigure.hazelcast.hazelcastautoconfiguration,\
Org.springframework.boot.autoconfigure.hazelcast.hazelcastjpadependencyautoconfiguration,\
Org.springframework.boot.autoconfigure.integration.integrationautoconfiguration,\
Org.springframework.boot.autoconfigure.jackson.jacksonautoconfiguration,\ Org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration,\ Org.springframework.boot. autoconfigure.jdbc.jndidatasourceautoconfiguration,\
Org.springframework.boot.autoconfigure.jdbc.xadatasourceautoconfiguration,\
Org.springframework.boot.autoconfigure.jdbc.datasourcetransactionmanagerautoconfiguration,\
Org.springframework.boot.autoconfigure.jms.jmsautoconfiguration,\
Org.springframework.boot.autoconfigure.jmx.jmxautoconfiguration,\
Org.springframework.boot.autoconfigure.jms.jndiconnectionfactoryautoconfiguration,\
Org.springframework.boot.autoconfigure.jms.activemq.activemqautoconfiguration,\
Org.springframework.boot.autoconfigure.jms.artemis.artemisautoconfiguration,\
Org.springframework.boot.autoconfigure.jms.hornetq.hornetqautoconfiguration,\
Org.springframework.boot.autoconfigure.flyway.flywayautoconfiguration,\
Org.springframework.boot.autoconfigure.groovy.template.groovytemplateautoconfiguration,\
Org.springframework.boot.autoconfigure.jersey.jerseyautoconfiguration,\ Org.springframework.boot.autoconfigure.jooq.jooqautoconfiguration,\ Org.springframework.boot.Autoconfigure.liquibase.liquibaseautoconfiguration,\
Org.springframework.boot.autoconfigure.mail.mailsenderautoconfiguration,\
Org.springframework.boot.autoconfigure.mail.mailsendervalidatorautoconfiguration,\
Org.springframework.boot.autoconfigure.mobile.deviceresolverautoconfiguration,\
Org.springframework.boot.autoconfigure.mobile.devicedelegatingviewresolverautoconfiguration,\
Org.springframework.boot.autoconfigure.mobile.sitepreferenceautoconfiguration,\
Org.springframework.boot.autoconfigure.mongo.embedded.embeddedmongoautoconfiguration,\
Org.springframework.boot.autoconfigure.mongo.mongoautoconfiguration,\
Org.springframework.boot.autoconfigure.mustache.mustacheautoconfiguration,\
Org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration,\
Org.springframework.boot.autoconfigure.reactor.reactorautoconfiguration,\
Org.springframework.boot.autoconfigure.security.securityautoconfiguration,\ Org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfigurAtion,\ org.springframework.boot.autoconfigure.security.fallbackwebsecurityautoconfiguration,\
Org.springframework.boot.autoconfigure.security.oauth2.oauth2autoconfiguration,\
Org.springframework.boot.autoconfigure.sendgrid.sendgridautoconfiguration,\
Org.springframework.boot.autoconfigure.session.sessionautoconfiguration,\
Org.springframework.boot.autoconfigure.social.socialwebautoconfiguration,\
Org.springframework.boot.autoconfigure.social.facebookautoconfiguration,\
Org.springframework.boot.autoconfigure.social.linkedinautoconfiguration,\
Org.springframework.boot.autoconfigure.social.twitterautoconfiguration,\
Org.springframework.boot.autoconfigure.solr.solrautoconfiguration,\
Org.springframework.boot.autoconfigure.velocity.velocityautoconfiguration,\
Org.springframework.boot.autoconfigure.thymeleaf.thymeleafautoconfiguration,\
Org.springframework.boot.autoconfigure.transaction.transactionautoconfiguration,\ Org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguratioN,\ org.springframework.boot.autoconfigure.web.dispatcherservletautoconfiguration,\
Org.springframework.boot.autoconfigure.web.embeddedservletcontainerautoconfiguration,\
Org.springframework.boot.autoconfigure.web.errormvcautoconfiguration,\
Org.springframework.boot.autoconfigure.web.httpencodingautoconfiguration,\
Org.springframework.boot.autoconfigure.web.httpmessageconvertersautoconfiguration,\
Org.springframework.boot.autoconfigure.web.multipartautoconfiguration,\
Org.springframework.boot.autoconfigure.web.serverpropertiesautoconfiguration,\
Org.springframework.boot.autoconfigure.web.webmvcautoconfiguration,\
Org.springframework.boot.autoconfigure.websocket.websocketautoconfiguration,\ Org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration # Template Availability
Providers org.springframework.boot.autoconfigure.template.templateavailabilityprovider=\ Org.springframework.boot.autoconfigure.freemarker.freemarkertemplateavailabilityprovider,\ ORG.SPRINGFRamework.boot.autoconfigure.mustache 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.