在Spring的开发过程中,通过@Configuration和@Bean实现将配置类添加到spring的bean容器中,但是实际的操作过程发现添加@Configuration注解和不添加该注解,配置类中的Bean都会添加到bean容器中,那么添加@Configuration注解和不添加该注解有什么区别呢?
Spring默认bean容器中的示例是单例的,下面的代码中添加@Configuration和不添加得到的结果是不一样的。
@ComponentScan({"org.std.framework"})
@Configuration
public class IndexConfig {
@Bean
public UserServiceImpl userServiceImpl(){
return new UserServiceImpl();
}
@Bean
public UserServiceImpl2 userServiceImpl2(){
UserServiceImpl userServiceImpl = userServiceImpl();
System.out.println(userServiceImpl);
return new UserServiceImpl2();
}
}
public class ConfigTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IndexConfig.class);
UserServiceImpl userServiceImpl = context.getBean(UserServiceImpl.class);
System.out.println(userServiceImpl);
context.getBean(UserServiceImpl2.class);
}
}
添加@Configuration打印的结果是两次得到的UserServiceImpl对象是同一个
org.std.framework.service.impl.UserServiceImpl@15aab8c6
org.std.framework.service.impl.UserServiceImpl@15aab8c6
没有添加@Configuration打印的结果是两次得到的UserServiceImpl对象不是同一个
org.std.framework.service.impl.UserServiceImpl@79d8407f
org.std.framework.service.impl.UserServiceImpl@68267da0
查看源码发现,添加@Configuration注解,spring会配置类进行cglib代理增强,对原有的方法进行拦截额外处理一部分逻辑
bean工厂创建的时有个内置的后置处理器ConfigurationClassPostProcessor,其中的postProcessBeanFactory会添加了@Configuration注解的配置类进行cglib代理增强
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
int factoryId = System.identityHashCode(beanFactory);
if (this.factoriesPostProcessed.contains(factoryId)) {
throw new IllegalStateException(
"postProcessBeanFactory already called on this post-processor against " + beanFactory);
}
this.factoriesPostProcessed.add(factoryId);
if (!this.registriesPostProcessed.contains(factoryId)) {
// BeanDefinitionRegistryPostProcessor hook apparently not supported...
// Simply call processConfigurationClasses lazily at this point then.
processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);
}
enhanceConfigurationClasses(beanFactory);
beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
}
//通过cglib对@Configuration进行代理增强
public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFactory) {
Map<String, AbstractBeanDefinition> configBeanDefs = new LinkedHashMap<>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
//此处判断BeanDefinition是否添加了@Configuration注解
if (ConfigurationClassUtils.isFullConfigurationClass(beanDef)) {
if (!(beanDef instanceof AbstractBeanDefinition)) {
throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
}
else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
"' since its singleton instance has been created too early. The typical cause " +
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
"return type: Consider declaring such methods as 'static'.");
}
configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef);
}
}
if (configBeanDefs.isEmpty()) {
// nothing to enhance -> return immediately
return;
}
ConfigurationClassEnhancer enhancer = new ConfigurationClassEnhancer();
for (Map.Entry<String, AbstractBeanDefinition> entry : configBeanDefs.entrySet()) {
AbstractBeanDefinition beanDef = entry.getValue();
// If a @Configuration class gets proxied, always proxy the target class
beanDef.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
try {
// Set enhanced subclass of the user-specified bean class
Class<?> configClass = beanDef.resolveBeanClass(this.beanClassLoader);
if (configClass != null) {
Class<?> enhancedClass = enhancer.enhance(configClass, this.beanClassLoader);
if (configClass != enhancedClass) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Replacing bean definition '%s' existing class '%s' with " +
"enhanced class '%s'", entry.getKey(), configClass.getName(), enhancedClass.getName()));
}
beanDef.setBeanClass(enhancedClass);
}
}
}
catch (Throwable ex) {
throw new IllegalStateException("Cannot load configuration class: " + beanDef.getBeanClassName(), ex);
}
}
}
拦截器方法中执行增强逻辑,判断当前线程调用的方法和动态代理调用的方法名称是否一致,一致的说明是初次实例化调用,invoke调用父类方法进行实例化,不一致说明不是初次示例化调用,从BeanFactory中获取。如上述实例中@Bean直接调用userServiceImpl,线程调用的方法名是userServiceImpl和动态代理调用的方法名称userServiceImpl一致,直接返回invokeSuper;在userServiceImpl2中调用userServiceImpl,线程调用的方法名称是userServiceImpl2,动态代理调用的方法名称是userServiceImpl,两者不一致,UserServiceImpl对象从BeanFactory中获取。没有添加@Configuration注解的不存在cglib动态代理,多次调用返回的都是new UserServiceImpl();
public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object[] beanMethodArgs,
MethodProxy cglibMethodProxy) throws Throwable {
ConfigurableBeanFactory beanFactory = getBeanFactory(enhancedConfigInstance);
String beanName = BeanAnnotationHelper.determineBeanNameFor(beanMethod);
// Determine whether this bean is a scoped-proxy
Scope scope = AnnotatedElementUtils.findMergedAnnotation(beanMethod, Scope.class);
if (scope != null && scope.proxyMode() != ScopedProxyMode.NO) {
String scopedBeanName = ScopedProxyCreator.getTargetBeanName(beanName);
if (beanFactory.isCurrentlyInCreation(scopedBeanName)) {
beanName = scopedBeanName;
}
}
// To handle the case of an inter-bean method reference, we must explicitly check the
// container for already cached instances.
// First, check to see if the requested bean is a FactoryBean. If so, create a subclass
// proxy that intercepts calls to getObject() and returns any cached bean instance.
// This ensures that the semantics of calling a FactoryBean from within @Bean methods
// is the same as that of referring to a FactoryBean within XML. See SPR-6602.
if (factoryContainsBean(beanFactory, BeanFactory.FACTORY_BEAN_PREFIX + beanName) &&
factoryContainsBean(beanFactory, beanName)) {
Object factoryBean = beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
if (factoryBean instanceof ScopedProxyFactoryBean) {
// Scoped proxy factory beans are a special case and should not be further proxied
}
else {
// It is a candidate FactoryBean - go ahead with enhancement
return enhanceFactoryBean(factoryBean, beanMethod.getReturnType(), beanFactory, beanName);
}
}
//判断当前线程调用的方法名称和动态代理调用的方法名称是否一致,一致的说明是初次实例化调用,invoke父类方法
//不一致说明不是初次实例化调用(在其他方法中调用),此时不在进行实例化,而是直接从BeanFactory中获取
if (isCurrentlyInvokedFactoryMethod(beanMethod)) {
// The factory is calling the bean method in order to instantiate and register the bean
// (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
// create the bean instance.
if (logger.isWarnEnabled() &&
BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
logger.warn(String.format("@Bean method %s.%s is non-static and returns an object " +
"assignable to Spring's BeanFactoryPostProcessor interface. This will " +
"result in a failure to process annotations such as @Autowired, " +
"@Resource and @PostConstruct within the method's declaring " +
"@Configuration class. Add the 'static' modifier to this method to avoid " +
"these container lifecycle issues; see @Bean javadoc for complete details.",
beanMethod.getDeclaringClass().getSimpleName(), beanMethod.getName()));
}
//实例化
return cglibMethodProxy.invokeSuper(enhancedConfigInstance, beanMethodArgs);
}
//从BeanFactory中获取
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);
}
private boolean isCurrentlyInvokedFactoryMethod(Method method) {
Method currentlyInvoked = SimpleInstantiationStrategy.getCurrentlyInvokedFactoryMethod();
return (currentlyInvoked != null && method.getName().equals(currentlyInvoked.getName()) &&
Arrays.equals(method.getParameterTypes(), currentlyInvoked.getParameterTypes()));
}