在之前的文章spring容器启动流程一文中介绍了spring的容器启动流程以及bean的创建流程,但是我们熟知的spring-aop的动态代理是如何创建的还未在文中提到,那么接下来,就来探索一下spring的动态代理类是在什么时候创建的,spring容器又是如何保存生成的单例bean的。
直接从代码入手,在AbstractAutowireCapableBeanFactory中的doCreateBean方法中有这么一段:
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
throw (BeanCreationException) ex;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
}
}
populateBean这个方法就是给bean的属性填入对应的初始值,这个不难理解,在前文中我也提到过,这里我们就更多关注一下initializeBean(beanName, exposedObject, mbd)这个方法,进入方法:
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
invokeAwareMethods(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
该方法主要就是执行一些扩展方法,invokeAwareMethods的详细内容如下:
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader());
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
如果bean实现了aware接口,就判断是具体是哪一个实现,执行对应的扩展方法,比如我们需要定制bean的Name,就可以这样来做:
public class MyBeanNameAware implements BeanNameAware {
public void setBeanName(String s) {
System.out.println("这里可以对beanName自定义操作");
}
}
同样的针对BeanClassLoaderAware和BeanFactoryAware也可以扩展:
public class MyBeanClassLoaderAware implements BeanClassLoaderAware {
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("这里可以对bean的ClassLoader自定义操作");
}
}
public class MyBeanFactoryAware implements BeanFactoryAware {
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("这里是对beanFactory的自定义操作");
}
}
这样来理解invokeAwareMethods就稍微要好一些了。
接下来回到initializeBean方法中继续往下看,applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)这里主要是做bean初始化前的扩展,不多说。主要来看applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName),进入方法,一直到AbstractAutoProxyCreator的postProcessAfterInitialization:
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
然后看到wrapIfNecessary这个方法中有一句这样的代码:
Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
很明显,这里就是在创建代理类了,跟进去看看细节,会来到这个方法:
public Object getProxy(@Nullable ClassLoader classLoader) {
return createAopProxy().getProxy(classLoader);
}
这里的意思就是创建aop代理,跟进createAopProxy方法:
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
activate();
}
return getAopProxyFactory().createAopProxy(this);
}
继续跟进,到createAopProxy方法中:
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
有没有感觉到了西天取经看到佛祖的感觉,这里就是我们常说的spring动态代理的两种方式,基于jdk和cglib的两种方式的动态代理类的创建,之后就是对应的初始化动作了,返回实例,调用getProxy方法获得代理对象,这里我们又回到doCreateBean方法中来:
exposedObject = initializeBean(beanName, exposedObject, mbd);
代理对象就是我们返回创建成功的bean,之后再调用registerDisposableBeanIfNecessary(beanName, bean, mbd);来把bean注册成为随时可用的bean,其根本就是:
public void registerDisposableBean(String beanName, DisposableBean bean) {
synchronized (this.disposableBeans) {
this.disposableBeans.put(beanName, bean);
}
}
这个实现位于DefaultSingletonBeanRegistry中,就是把bean 按照beanName,bean实例的方式存到一个LinkedHashMap中。DefaultSingletonBeanRegistry这个类的顶部我们可以看到还定义了其他的对象:
/** Cache of singleton objects: bean name --> bean instance */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
/** Cache of early singleton objects: bean name --> bean instance */
private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);
/** Set of registered singletons, containing the bean names in registration order */
private final Set<String> registeredSingletons = new LinkedHashSet<String>(256);
/** Disposable bean instances: bean name --> disposable instance */
private final Map<String, Object> disposableBeans = new LinkedHashMap<String, Object>();
singletonObjects :所有单例对象缓存,类型为ConcurrentHashMap,以beanName为key,bean实例为value存储
earlySingletonObjects :是singletonFactory 制造出来的 singleton 的缓存
registeredSingletons :按顺序存放已经注册的SingletonBean的名称
disposableBeans :存放一次性bean的缓存对象
在 getSingleton的时候,spring的默认实现是,先从singletonObjects寻找,如果找不到,再从earlySingletonObjects寻找,仍然找不到,那就从singletonFactories寻找对应的制造singleton的工厂,然后调用工厂的getObject方法,造出对应的SingletonBean,并放入earlySingletonObjects中。
总结
以上就是个人对动态代理创建过程和spring的Bean缓存的分析,纯属个人见解,如有错误之处,请不吝指正。