一、简介
Configuration class会在ConfigurationClassPostProcessor中将Configuration class进行强化处理。Spring通过CGLIB进行动态代理。核心类ConfigurationClassEnhancer。
二、源码
private Enhancer newEnhancer(Class<?> superclass, ClassLoader classLoader) {
Enhancer enhancer = new Enhancer();
// 设置父类Configuration
enhancer.setSuperclass(superclass);
// 用于设置beanFactory
enhancer.setInterfaces(new Class<?>[] {EnhancedConfiguration.class});
enhancer.setUseFactory(false);
// 用于命名BySpringCGLIB
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
// 定义beanFactory属性名,暴露ClassLoader
enhancer.setStrategy(new BeanFactoryAwareGeneratorStrategy(classLoader));
// 设置代理回调:
// BeanMethodInterceptor,**核心@Bean方法调用**。
// BeanFactoryAwareMethodInterceptor,提供setBeanFactory(BeanFactory)调用
enhancer.setCallbackFilter(CALLBACK_FILTER);
enhancer.setCallbackTypes(CALLBACK_FILTER.getCallbackTypes());
return enhancer;
}
ConfigurationClassEnhancer主要对Scope和AOP进行处理。