版权声明:本文为FromTheWind原创文章,转载请注明出处。https://blog.youkuaiyun.com/FromTheWind/article/details/84502604
SpringBootApplication是一个组合注解,进入到application类中,按住Ctrl进入到@SpringBootApplication内部:
@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 {......}
从以上源码可以看到它组合了@SpringBootConfiguration、@EnableAutoConfiguration以及@ComponentScan。@SpringBootConfiguration内部使用了@Configuration注解,说明这是一个配置类。
@EnableAutoConfiguration则表示让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。@Configuration将java类声明为一个配置类,等同于spring的xml文件。
@ComponentScan
扫描注解,如:@Controller,@Service等注解
详解:
1、@EnableAutoConfiguration注解:
spring通常建议我们将main方法所在的类放到一个root包下,@EnableAutoConfiguration(开启自动配置)注解通常都放到main所在类的上面,这样@EnableAutoConfiguration可以从逐层的往下搜索各个加注解的类,例如,你正在编写一个JPA程序(如果你的pom里进行了配置的话),spring会自动去搜索加了@Entity注解的类,并进行调用。
@EnableAutoConfiguration借助@Import的帮助,通过selectImports从META-INF/spring.factories中取出想要的配置,将所有符合自动配置条件(这里的条件是spring制定的,带有@Conditional注解就是加了条件,文章结尾又常用几个条件的解释)的bean定义加载到spring(IoC)容器。
从代码中分析(这里是Spring Boot 2.1.0.RELEASE版本):进入到@EnableAutoConfiguration中,看到@Import加载了AutoConfigurationImportSelector.class(这是重点),而在AutoConfigurationImportSelector中通过 List<String> configurations=this.getCandidateConfigurations(annotationMetadata,attributes)获取了springboot自动配置相关类。从getCandidateConfigurations方法层层进入能看到spring-boot-autoconfigure.jar中的META-INF/spring.factories文件(以key=value形式存在)中以org.springframework.boot.autoconfigure.EnableAutoConfiguration为key的数据被EnableAutoConfiguration.class以反射的方式根据具体条件加载到spring容器。
代码调用流程:AutoConfigurationImportSelector->selectImports(...)->this.getAutoConfigurationEntry(...)->this.getCandidateConfigurations(...)->SpringFactoriesLoader.loadFactoryNames(...)->factoryClass.getName()。
详情请查看参考文章:https://blog.youkuaiyun.com/u010399009/article/details/79290699
2、SpringBoot 配置类
2.1、@Configuration注解:
使用了@Configuration注解的java类为配置类。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
查看源代码发现@Configuration本质还是@Conponent。因此<context:component-scan/>
或者@ComponentScan
都能处理@Configuration注解的类。@Configuration就相当于Spring配置文件中的<beans />标签,里面可以配置bean。
2.2、@Bean
@Bean注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与方法名相同;@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为多例
后期补充:使用 @Configuration 时,会为该类生成CGLIB代理对象的子类Class,并对方法拦截。多次调用方法时,方法返回的对象会是同一个对象。而@Component 则不会生成 CGLIB 代理Class,所以多次调用同一个方法就会生成多个不同的对象返回。@Configuration包含有@Component,但不仅仅具有@Component的功能。@Bean在一个class具有自己独特的构造器无法被spring管理时,在配置类(@Configuration)中调用此class的构造函数且返回时,在方法上加@Bean,会将class对象交由spring管理。
3、@ComponentScan
Spring是一个依赖注入(dependency injection)框架。所有的内容都是关于bean的定义及其依赖关系。定义Spring Beans的第一步是使用正确的注解-@Component或@Service或@Repository。但是,Spring不知道你定义了某个bean。@ComponentScan做的事情就是告诉Spring从哪里找到bean。默认情况下会扫描使用了@ComponentScan
的包及其子包。
4、拓展:
@component :(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>
)。 泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用 @Component来标注这个类。
@Component 和 @ComponentScan 的区别:@Component 和 @ComponentScan的使用目的不一样。
- 在某个类上使用@Component注解,表示将该类配置为Bean,交给spring来管理(IoC、DI)。
- @ComponentScan 用于扫描指定包下的类。确定被spring管理的类都有哪些。
@Conditional常用条件注解说明:
@ConditionalOnClass:当类路径下有指定类时实例
@ConditionalOnMissingClass:某个class类路径上不存在时实例化
@ConditionalOnProperty:指定属性存在指定值时实例化
@ConditionalOnExpression:当表达式为true时实例化
@ConditionalOnBean:仅仅在当前上下文中存在某个对象时实例化
@ConditionalOnMissingBean:仅仅在当前上下文中不存在某个对象时实例化
@ConditionalOnNotWebApplication:不是web应用时实例化