Spring boot automatic configuration decryption annotations @enableautoconfiguration

Source: Internet
Author: User
Spring Boot automatic configuration decryption annotations @enableautoconfigurationSpring boot for automatic configuration, added annotations @enableautoconfiguration,spring boot 1.2, our startup class needs to be annotated:
@Configuration
 @EnableAutoConfiguration
 @ComponentScan


Spring Boot 1.2 has a new annotation @springbootapplication, which can be replaced with an annotation:
@Target (Elementtype.type)
@Retention (retentionpolicy.runtime)
@Documented
@Inherited
@ Springbootconfiguration
@EnableAutoConfiguration
@ComponentScan (excludefilters = {
		@Filter (type = Filtertype.custom, classes = Typeexcludefilter.class),
		@Filter (type = filtertype.custom, classes = Autoconfigurationexcludefilter.class)}) public
@interface Springbootapplication {
The annotation @EnableAutoConfiguration is the key to spring boot autoconfiguration and the key to extending spring boot. Spring boot attempts to register the bean instance we need based on conditions: Enable auto-configuration of the Spring application Context, attempting to guess and co Nfigure beans that is likely to need. Auto-configuration classes is usually applied based on your classpath and what beans you have defined. For example, if you had Tomcat-embedded.jar on your classpath is likely to want a tomcatservletwebserverfactory (UN  Less you have defined your own servletwebserverfactory bean). When using Springbootapplication, the auto-configuration of the context was automatically enabled and adding this Annotati On have therefore no additional effect.
Auto-configuration tries to be as intelligent as possible and would back-away as you define more of  your own Configu Ration. You can all manually exclude () any configuration so you never want to apply  (use Excludename () if you don hav e access to them). You can also exclude them via the  Spring.autoconfigure.exclude property. Auto-configuration is all applied after user-defined beans  has been registered. The package of the class which is annotated with @EnableAutoConfiguration, usually via  @SpringBootApplication, have SP Ecific significance and is often used as a ' default '. For example,  It is used when scanning for @Entity classes. It is generally recommended so you place @EnableAutoConfiguration (if you ' re not using @SpringBootApplication) in a root Sub-packages and classes can be searched. Auto-configuration classes is regular Spring configuration beans. They is located using the springfactoriesloader mechanism (KEYed against this class). Generally auto-configuration beans are  @Conditional beans (most often using @ConditionalOnClass and @ConditionalOnM Issingbean annotations).

Note @enableautoconfiguration automatic configuration of beans automatically, mainly its annotations @Import (Autoconfigurationimportselector.class) The class org.springframework.boot.autoconfigure in.  Autoconfigurationimportselector plays a major role, and it mainly looks for some of the necessary configuration classes. The Autoconfigurationimportselector class has several methods, but the key way to automatically configure spring boot is:



/**
	 * Return the Auto-configuration class names that should be considered. By default
	 * This method would load candidates using {@link Springfactoriesloader} with
	 * {@link #getSpringFactorie Sloaderfactoryclass ()}.
	 * @param metadata the source metadata
	 * @param attributes the {@link #getAttributes (annotationmetadata) annotation
  * attributes}
	 * @return A list of candidate configurations
	 */
	protected list<string> Getcandidateconfigurations (Annotationmetadata metadata,
			annotationattributes attributes) {
		list< string> configurations = Springfactoriesloader.loadfactorynames (
				getspringfactoriesloaderfactoryclass (), Getbeanclassloader ());
		Assert.notempty (configurations,
				"No Auto Configuration classes found in Meta-inf/spring.factories. If You "
						+" were using a custom packaging, make sure the file is correct. ");
		return configurations;
	}
The method returns Spring Boot ConventionsClasses that require automatic configuration.
Code:
list<string> configurations = Springfactoriesloader.loadfactorynames (
				Getspringfactoriesloaderfactoryclass (), Getbeanclassloader ());
The main function is from the file
"Meta-inf/spring.factories";
Get a well-agreed automatic configuration class. Spring boot provides the automatic configuration classpath by default:
Spring-boot-autoconfigure-2.0.0.m7.jar!/meta-inf/spring.factories such as an automatic configuration class:
 @Configuration @ConditionalOnWebApplication (type = Type.servlet) @ConditionalOnClass ({ Servlet.class, Dispatcherservlet.class, Webmvcconfigurer.class}) @ConditionalOnMissingBean ( Webmvcconfigurationsupport.class) @AutoConfigureOrder (ordered.highest_precedence +) @AutoConfigureAfter ({ Dispatcherservletautoconfiguration.class, Validationautoconfiguration.class}) public class Webmvcautoconfiguration

	{public static final String default_prefix = "";

	public static final String default_suffix = "";

	private static final string[] Servlet_locations = {"/"}; @Bean @ConditionalOnMissingBean (hiddenhttpmethodfilter.class) public orderedhiddenhttpmethodfilter
	Hiddenhttpmethodfilter () {return new Orderedhiddenhttpmethodfilter (); }

	......

}
This automatic configuration class is not to instantiate and the internal configuration of the bean is not required to instantiate, and this back to the blog http://blog.csdn.net/doctor_who2004/article/details/79184230 said the note @          The role of the conditional. Interested can view the spring boot related source code.



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.