相关知识点
- Profile
- @Conditional
- Addressing ambiguity in autowiring 注入歧义性
- Bean Scoping
Spring Profile
类似于maven的profile,需要指定spring.profile.default/spring.profile.active来激活profile
条件注入
spring4 提供的注解,可以条件化的注入bean
注入的歧义
当注入有歧义的时候会抛出org.springframework.beans.factory.NoUniqueBeanDefinitionException
可以使用@Qualifier
限定 或者@Primary
来指定优先级
更高级的用法( 特殊的场景),由于Java不能多次使用同一个注解,可以通过自定义注解来限定。
Bean作用域
在写这篇笔记之前,业务就出现了一次一个static变量重复赋值导致的线上问题,有同事说是static的问题,但是这里如果不是static变量
还是会有这个问题(该变量是类属性),由于spring创建bean默认是singleton模式的,所以每次都是这个bean的变量被赋值到也会造成同样的问题。
不过spring还是提供了四种bean的作用域供我们选择:
- Singleton
One instance of the bean is created for the entire application. - Prototype
One instance of the bean is created every time the bean is injected into or retrieved from the Spring application context.
每次注入或者从context中获取到的都是新对象 - Session
In a web application, one instance of the bean is created for each session. - Request
In a web application, one instance of the bean is created for each request.
网友评论