美文网首页
自动扫描注解间的联系

自动扫描注解间的联系

作者: BenjaminCool | 来源:发表于2019-12-06 22:26 被阅读0次

@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 {}

Indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.

This is a convenience annotation that is equivalent to declaring @Configuration, @EnableAutoConfiguration and @ComponentScan.

指示一个配置类,该类声明一个或多个@Bean方法,并触发自动配置和组件扫描。

这是一个方便的注释,相当于声明@Configuration、@EnableAutoConfiguration和@ComponentScan。

1、Indicates a configuration class that declares one or more @Bean methods
2、 triggers auto-configuration
3、 triggers component scanning

@AutoConfiguration

## @EnableAutoConfiguration doc

Auto-configuration is always applied after user-defined beans have been registered.

Auto-configuration is always applied after user-defined beans have been registered.png

DeferredImportSelector
that runs after all {@code @Configuration} beans have been processed.

## 自动配置机制

org.springframework.boot.autoconfigure包,定义了自动配置项

## the SpringFactoriesLoader mechanism

/META-INF/spring.factories 干啥用的?用来自动配置的; 举例:


image.png

/META-INF/spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

结合@ConditionalOnBean、@ConditionalOnClass、@ConditionalOnMissingBean等注解一起做auto-configuration.

@org.springframework.context.annotation.Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration{}

## SpringBoot源码分析之工厂加载机制

/META-INF/spring.factories

有些jar包是脱离于spring框架的,所以不能配置@Component及其子类的注解,如果我们想要将jar包提前配置bean,需要用到/META-INF/spring.factories

对于使用@ComponentScan、@EntityScan或@springbootsapplication注释的Spring Boot应用程序来说,因为每个jar中的每个类都会被读取。

Configuration

## @Configuration

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // 被@Component注解,所以可以被@ComponentScan扫描
public @interface Configuration {}

1、 结合@Bean注解的方法,定义bean definition

2、 Working with externalized values

@Configuration classes引导启动: 被组件扫描引导启动

谁来引导启动@ComponentScan class呢?

AnnotationConfigApplicationContext 引导启动@ComponentScan class

@ComponentScan

默认扫描Application.java所在包下的所有类,不在Application.java所在包下的类扫描不到.

默认扫描Application.java所在包下的所有类,不在Application.java所在包下的类扫描不到.png

Configures component scanning directives for use with @Configuration classes.
@ComponentScan注解 和@Configuration一起使用, 用来配置component scanning 指令

区分auto-configuration与component scan

auto-configuration机制:

### the SpringFactoriesLoader mechanism

/META-INF/spring.factories 干啥用的?用来自动配置的;

SpringBoot源码分析之工厂加载机制

@EnableAutoConfiguration注解对应的ImportAutoConfigurationImportSelector中的selectImport方法会在spring.factories文件中找出key为EnableAutoConfiguration对应的值。

@ComponentScan 组件扫描机制:

扫描指定包或当前包及其子包路径下的所有@Component或其子类注解的类。
@Component的子类: @Controller、@Service、@Repository、@Configuration等

自动配置和组件扫描的关系

组件扫描在自动配置之前;先执行组件扫描, 再执行自动配置;
根据组件扫描的结果, 再根据@Conditional, @ConditionalOnBean,@ConditionalOnClass,@ConditionalOnMissingBean等决定是否自动配置该类。

Auto-configuration is always applied after user-defined beans have been registered.

Auto-configuration is always applied after user-defined beans have been registered.png

自动配置相关类和接口:
ImportSelector

自动配置常见的依赖条件有:
这些是springboot特有的,常见的条件依赖注解有:

@ConditionalOnBean,仅在当前上下文中存在某个bean时,才会实例化这个Bean。

@ConditionalOnClass,某个class位于类路径上,才会实例化这个Bean。

@ConditionalOnExpression,当表达式为true的时候,才会实例化这个Bean。

@ConditionalOnMissingBean,仅在当前上下文中不存在某个bean时,才会实例化这个Bean。

@ConditionalOnMissingClass,某个class在类路径上不存在的时候,才会实例化这个Bean。

@ConditionalOnNotWebApplication,不是web应用时才会实例化这个Bean。

@AutoConfigureAfter,在某个bean完成自动配置后实例化这个bean。

@AutoConfigureBefore,在某个bean完成自动配置前实例化这个bean。

@Import注解

Indicates one or more {@link Configuration @Configuration} classes to import.

我们可以将自动配置的关键几步以及相应的注解总结如下:

1、@Configuration&与@Bean->基于java代码的bean配置

2、@Conditional->设置自动配置条件依赖

3、@EnableConfigurationProperties与@ConfigurationProperties->读取配置文件转换为bean。

4、@EnableAutoConfiguration、@AutoConfigurationPackage 与@Import->实现bean发现与加载。

如果要让一个普通类交给Spring容器管理,通常有以下方法:

1、使用 @Configuration与@Bean 注解

2、使用@Controller @Service @Repository @Component 注解标注该类,然后启用@ComponentScan自动扫描

3、使用@Import 方法

相关文章

网友评论

      本文标题:自动扫描注解间的联系

      本文链接:https://www.haomeiwen.com/subject/bklygctx.html