Springboot Series: @SpringBootApplication annotations

Source: Internet
Author: User
Tags cassandra documentation hazelcast

When using the Springboot framework for development, usually we will add @SpringBootApplication annotations on the main function, today for everyone to parse the @SpringBootApplication, if there is an incorrect, welcome criticism. @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 {};
}

It is learned from source code that @SpringBootApplication is modified by @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 functions of each annotation are explained below. @Configuration

Spring Documentation Description:

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

@Configuration is a class-level comment indicating that the object is a bean-defined source. The @Configuration class declares a bean by public methods that @bean annotations.

The @Bean annotation is used to indicate. 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, configuration, and initialization is a new object managed by the Spring IoC container.

Generally speaking @Configuration used in conjunction with @Bean annotations, with @Configuration annotation class equivalence and XML configuration beans, with the @Bean annotation method equivalent to the configuration Bean in XML. To illustrate:

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>

is 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: Use public to modify the method of @Bean annotations; UserService, Userdao classes need not be declared as @Component, @Service, @Repository , @Controller;

looking at the above Java code, perhaps someone will ask, why not @Autowired annotation directly injected Userdao. I think so. @Configuration, @Bean more attention to configuration aspects, such as we can set the Userdao class object in the @Bean method of special information, such as specifying TransactionManager and so on. @EnableAutoConfiguration

Spring Documentation Description:

Enable Auto-configuration of the spring application Context, attempting to guess and configure beans that is likely t o need. Auto-configuration classes is usually applied based on your classpath and what beans you have defined.

Enable automatic configuration of the Spring application context to try to guess and configure the beans you might need. Automatic configuration classes are typically applied based on your classpath and beans objects that have already been defined.

The package of the class, annotated with @EnableAutoConfiguration have specific significance and is often used as a ' Default '. For example, it'll be 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 package that is the @EnableAutoConfiguration annotated class contains a specific meaning and is used as the default configuration. For example, it will be used when scanning @Entity class. It is usually recommended to configure the @EnableAutoConfiguration under the root package so that all the child packages and classes can be found.

Auto-configuration classes is regular Spring configuration beans. They is located using the Springfactoriesloader mechanism (keyed against this class). Generally auto-configuration beans is @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 (with the Enableautoconfiguration classpath as key). Typically auto-configuration beans are @Conditional beans (in most cases with @ConditionalOnClass and @ConditionalOnMissingBean annotations).

Springfactoriesloader mechanism:
Springfactoriesloader will query the jar that contains the meta-inf/spring.factories file. When the spring.factories file is found, Springfactoriesloader queries the configuration file for the named property. The Enableautoconfiguration key value 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, include 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

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.