Spring Core
在生命周期长的Bean中指定一个短的
- xml配置方式
proxy-target-class
default=true
使用CGlib进行代理; default=false
时使用JDK动态代理;
xml配置
<!-- DefaultUserPreferences implements the UserPreferences interface -->
<bean id="userPreferences" class="com.stuff.DefaultUserPreferences" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
</bean>
<bean id="userManager" class="com.stuff.UserManager">
<property name="userPreferences" ref="userPreferences"/>
</bean>
- 注解方式
// CGLIB 方式
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS)
// JDK动态代理 方式
@Scope(value="request", proxyMode = ScopedProxyMode.INTERFACES)
单例Bean中的有原型bean
方法一:lookup
,通过lookup会自动去查找类型一样的bean进行注入;注意使用lookup的方法必须为抽象方法;
@Component("AAA")
@Scope(value = "prototype")
public class AAA {
@Bean
public AAA createAAA(A a,B b) {
return this;
}
}
@Component
public abstract class ControllerManager {
private AAA aaa;
@Lookup
public abstract AAA createAAA() ;
public void test() {
this.aaa = createAAA();
}
}
方法二:实现ApplicationContextAware
拿到beanFactory
对象,每次方法调用的时候都会获取到一个新的单例bean
@Autowired
field底层实现
@Autowired
先会按类型注入,如果有多个类型,则按照名字注入;在源码中设置值是通过反射实现org.springframework.beans.DirectFieldAccessor.FieldPropertyHandler#setValue
@Override
public void setValue(@Nullable Object value) throws Exception {
try {
ReflectionUtils.makeAccessible(this.field);
this.field.set(getWrappedInstance(), value);
} catch (IllegalAccessException ex) {
throw new InvalidPropertyException(getWrappedClass(), this.field.getName(),
"Field is not accessible", ex);
}
}
@Autowired
或者@Resource
指定实现类
- 在某一个子实现类上使用
@Primary
指定要注入的Bean为当前的bean, 以下注入方式二选一
@Primary
@Component
public class ProtoBeanImpl implements ProtoBean{
}
@Primary
@Bean
public ProtoBeanImpl protoBean(){
return new ProtoBeanImpl();
}
- 使用
@Qualifier("xxx")
注解 指定要注入的bean的类型
@Qualifier("protoBeanImpl2")
@Autowired
private ProtoBean bean;
@Autowired
public SingleBean(@Qualifier("protoBeanImpl2") ProtoBean proto) {
this.protoBeanImpl2 = (ProtoBeanImpl2) proto;
}
- 使用
@Resource
@Resource(name="protoBeanImpl2")
private ProtoBean protoBean;
// 和下面写法等同,不指定name,默认为变量名
@Resource
private ProtoBean protoBeanImpl2;
构造器注入
-
using constructor inject,do not need other annotation if all the properties is base type ,using
@ConstructorProperties({"xxx","xxx",...})
to inject the value -
if just one constructor here, need' not
@Autowired
-
only one multi-argument constructor can be set
@Autowired(required = true)
-
if one more constructor are annotationed with
@Autowired(required = false)
The constructor with the greatest number of dependencies that can be satisfied by matching beans in the Spring container will be chosen -
The
@Autowired
,@Inject
,@Value
, and@Resource
annotations are handled by Spring BeanPostProcessor implementations
使用AspectJ注解进行AOP配置好还是xml
使用xml配置时,将AOP配置分散了,一部分在xml中,一部分在后台的class类中。不符合DRY原则。
使用@AspectJ
,则将整个AOP的配置放在一个配置类中,@AspectJ
支持额外的实例模型更丰富的组合,是每个切面成为一个模型单元。
同时,@AspectJ
能被 Spring AOP 和AspectJ 解析,你可以使用AspectJ的语法去实现更加复杂的切面逻辑