为什么不加注解也可以实例化?
各种注解注册的顺序?(ImportBeanDefinationRegistrar和ImportSelector)
一、spring注解
第一节课
@Configuration
@Bean的beanid就是方法名,也可以自定义。[优势就是可以导入不在当前工程下的Bean]
@ComponentScan(includeFilter.excludeFIlter)[只能导入当前工程的Bean]
includeFilter时必须useDefaultFilter=false(三种方式TypeFilter)
IOC容器开始的时候单例的bean就会被创建;多例的bean只有被使用到时才会被创建。
@Scope
request和session的含义
@Lazy(针对单实例的bean,容器启动时不会创建,第一次使用的时候才会创建)
第二节课:
@Conditional和Condition接口
BeanFactory和FactoryBean
@Import[快速给容器导入一个Bean,第三方的Bean(beanid是类名的全称)]
ImportSelector接口【全类名数组的Bean】(和@Import配合使用)
ImportBeanDefinationRegistrar(自定义BeanDefination,自己手动注册)(和Import配合使用)
实现FactoryBean接口,(这个的话可以和@Import,@Component,@Bean结合使用)
第三节课:
@Bean(initMethod="init",destroyMethod="destroy")
@Scope
InitializingBean,disposableBean
@PostConstruct,@PreDestroy
BeanPostProcessor(init方法执行前后分别执行)
第四节课:
各种各样的PostProcessor
声明式事务的实现
@Async
BeanValidationPostProcessor
InitDestroyAnnotationBeanPostProcessor(@PostConstruct,@PreDestroy)
ApplicationContextAwareProcessor
AutowiredAnnotatiobBeanPostProcessor
@Autowired@resource@inject
@Quialifar@Primary
@Value(1、基本字符2、spel#{20-2}3、${bird.color})环境变量
@PropertySource
@Autowired和@Quialifar结合使用
@Bean和@Component优先级与@Autowired注入顺序
那类型拿同时有几个bean怎么办呢?
@Primary优先级高一点
@Autowired和@Primary结合使用
@Quialifar比@Primary优先级高[@Quialifar会更加冗余没有@Primary好]
@Resource(name="")1、不支持@primary2、不支持@Autowired(required=false)
@Resource和@Autowired的对比
@Autowired可以在很多地方使用【字段,set方法,有参构造函数,函数参数】
各种Aware(BeanFactory,BeanName,Resource,Environment,ApplicationContext,EmbeddedValueResolver(@Value))+BeanPostProcessor
AOP四部曲:1、定义切面类(@Aspect@Component)(@PointCut)(五种通知)
2、定义业务类(@Component)
3、@EnableAspectJAutoProxy
------------------------------------------------------------------------------------------------------------------------
(多实例对象和Lazy修饰的对象在容器初始化时不创建)
@Configuration
1、@Bean(1、导入第三方的类或包的组件;2、包扫描+组件的标注注解,一般是针对自己实现的类)
2、@ComponentScan(@Controller@Service@Repository@Component)
@Scope
@Lazy
@ComponentScan(可以自定义TypeFilter接口)
@Conditional(可以自定义实现Condition接口)
3、@Import注册bean(快速给容器导入一个主键)
ImportSelector接口(返回需要注入到容器的bean数组)
ImportBeanDefinitionRegistrar接口(可以手动添加组件到IOC容器,所有bean的注册可以使用BeanDefinitionRegistrar)
4、FactoryBean接口(@Component或者@Bean注册)
----------------------------------------------------------------------------------------
1、InitializingBean接口,DisposableBean接口
2、init-method和destroy-method
3、@PreDestroy和@PostConstruct
----------------------------------------------------------------------------------------------
BeanPostProcessor
AutowiredAnnotationBeanPostProcessor
AsyncAnnotationBeanPostProcessor
ApplicationContextAwareProcessor(可以拿到applicationcontext)
BeanValidationPostProcessor
InitDestroyAnnotationBeanPostProcessor(@PreDestroy)
ApplicationContextAware
BeanNameAware
EnvironmentAware
EmbeddedValueResolver
1、创建并初始化容器相关的所有processor
2、bean
【1、构造函数
2、BeanPostProcessor的beforetProcessBeforeInitialization
3、执行init方法
4、BeanPostProcessor的postProcessAfterInitialization】
------------------------------------------------------------------------------------------------------
@Value(1)基本字符(2)spel表达式(3)读取配置文件(配置文件会配置为环境变量的)
@Autowired()【成员变量,支持构造器,set方法,函数参数】与@Qualifier(指定注入)配合使用
@resource(name="")【不支持Primary;不支持Autowired false功能】
@Primary首选项
@Qualifier优先级大于@Primary
第一方:@Bean修饰的bean的id;第二方:@Component修饰的bean的id; 第三方:@Autowired修饰的变量的名字
(1)@Bean注册的bean比@Componentscan优先级高一点【第一方名字==第二方名字】
(2)其次的优先级就是@Autowired变量的名字需要和两种方式注册的方式bean其中之一名字一样【第一方名字==第三方名字或者第二方名字==第三方名字】【如果三方的名字都不一样,那么会出现注册失败】
(3)@Qualifier和@Primary
------------------------------------------------------------------------------------------------------------------------
CGLIB
AOP:exposeProxy
proxyTargetClass(cglib和jdk)
SmartInstantiationAwareBeanPostProcessor(后置处理器)
BeanFactoryAware(可以通过beanfactory获取到bean)
ProxyProcessorSupport优先级(ordered)
作用:(1)拦截目标
一、spring中需要注意的地方
【
new
注入
init@postconstruct
aop
map nsingletonobjects
】
9个后置处理器
importSelector扩展
FactoryBean会向spring容器中注册两个Bean:获取对象lubanFactoryBean,获取工厂&lubanFactoryBean
1、在spring中加注解的类名字不能重复,否则无法进行注入,会有问题的,要不然就自己表明bean id
2、mergerbean是什么
3、什么是提前暴露一个工厂(循环依赖)
singletonObjects(一级缓冲)(终态)(单列池)
singletonFactories(二级缓冲)【】
earlySingletonObjects(三级缓冲)【提高效率】
@Configuration是为了确保@Bean是单例
注入三种方法:
byType,byName,构造方法
PostProcessorRegistrationDelegate.java
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);扫描
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);判断是否需要代理
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
ConfigurationClassPostProcessor.java
postProcessBeanFactory方法
全注解与半注解:full与lite
二、Spring的注解
1、@Configuration+@Bean
这个注解可以代替spring.xml的配置文件
2、@SpringBootApplication=@Configuration+@EnableAutoConfiguration+@ComponentScan
3、@ComponentScans({@ComponentScan("com.test")})
扫描包,加载用户自定义的bean
4、@EnableAutoConfiguration spring-boot-autoconfigure.jar(spring.factories的key=org.springframework.boot.autoconfigure.EnableAutoConfigure)
扫描META-INF/spring.factories这类文件的,扫描包加载springboot自带的相关的bean
5、@ConfigurationProperties和@EnableConfigurationProperties
EnableConfigurationProperties是使得ConfigurationProperties生效
6、@RestController是@Controller和@ResponseBody的合集
7、@ComponentScan和@EnableAutoConfiguration的不同点
研究springboot源码,在网上看相关博客的时候对@ComponentScan和@EnableAutoConfiguration两者之间的作用没有做过多的区分,导致我觉得他们两者都有扫描相关组建然后将符合要求的放入到ioc容器中。所以我就占牛角尖了,单独研究了一下他们的不同点。
@ComponentScan和@EnableAutoConfiguration都是包含在@SpringBootApplication中的,也是@SpringBootApplication中比较重要的注解。
@ComponentScan和@EnableAutoConfiguration的相同点
两者都可以将带有@Component,@Service等注解的对象加入到ioc容器中。
@ComponentScan和@EnableAutoConfiguration的不同点
1.两者虽然都能将带有注解的对象放入ioc容器中,但是它们扫描的范围是不一样的。@ComponentScan扫描的范围默认是它所在的包以及子包中所有带有注解的对象(这里包括依赖包),@EnableAutoConfiguration扫描的范围默认是它所在类。
2.它们作用的对象不一样,@EnableAutoConfiguration除了扫描本类带有的注解外,还会 借助@Import的支持,收集和注册依赖包中相关的bean定义,将这些bean注入到ioc容器中,在springboot中注入的bean有两部分组成,一部分是自己在代码中写的标注有@Controller,@service,@Respority等注解的业务bean,这一部分bean就由@ComponentScan将它们加入到ioc容器中,还有一部分是springboot自带的相关bean,可以将这部分bean看成是工具bean,这部分bean就是由@EnableAutoConfiguration负责加入到容器中。
3.@EnableAutoConfiguration可以单独启动springboot项目,而@ComponentScan是不能的。
8、@component和@bean
(1)@Bean注解在方法上 @component注解在类上
(2)@Bean注解只是把对象注入到spring容器中 @component先创建类然后再把创建好的类注入到spring容器中
(3)@Bean的默认beanId为方法名 @component的默认beanId为(类名把首字母小写)
(4)@Bean和@Configuration配合使用
9、缓冲注解
(1)@EnableCaching 开启基于注解的缓冲,这个注解注解在中心主类上
(2)@Cacheable
(3)@CachePut
(4)@CacheEvict
(5)@Caching
参考文献:
spring缓冲注解:https://blog.csdn.net/lizhiqiang1217/article/details/90577084
spring自动注入:https://blog.csdn.net/java_lyvee/article/details/102499560