org.springframework.context.annotation
Class ConfigurationClassEnhancer.BeanMethodInterceptor

java.lang.Object
  extended by org.springframework.context.annotation.ConfigurationClassEnhancer.BeanMethodInterceptor
Enclosing class:
ConfigurationClassEnhancer

private static class ConfigurationClassEnhancer.BeanMethodInterceptor
extends java.lang.Object

Intercepts the invocation of any Bean-annotated methods in order to ensure proper handling of bean semantics such as scoping and AOP proxying.

See Also:
Bean, ConfigurationClassEnhancer

Field Summary
private  ConfigurableBeanFactory beanFactory
           
 
Constructor Summary
ConfigurationClassEnhancer.BeanMethodInterceptor(ConfigurableBeanFactory beanFactory)
           
 
Method Summary
private  java.lang.Object enhanceFactoryBean(java.lang.Class<?> fbClass, java.lang.String beanName)
          Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory instead of creating a new instance.
private  boolean factoryContainsBean(java.lang.String beanName)
          Check the beanFactory to see whether the bean named beanName already exists.
 java.lang.Object intercept(java.lang.Object enhancedConfigInstance, java.lang.reflect.Method beanMethod, java.lang.Object[] beanMethodArgs, MethodProxy cglibMethodProxy)
          Enhance a @Bean method to check the supplied BeanFactory for the existence of this bean object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

beanFactory

private final ConfigurableBeanFactory beanFactory
Constructor Detail

ConfigurationClassEnhancer.BeanMethodInterceptor

public ConfigurationClassEnhancer.BeanMethodInterceptor(ConfigurableBeanFactory beanFactory)
Method Detail

intercept

public java.lang.Object intercept(java.lang.Object enhancedConfigInstance,
                                  java.lang.reflect.Method beanMethod,
                                  java.lang.Object[] beanMethodArgs,
                                  MethodProxy cglibMethodProxy)
                           throws java.lang.Throwable
Enhance a @Bean method to check the supplied BeanFactory for the existence of this bean object.

Throws:
java.lang.Throwable - as a catch-all for any exception that may be thrown when invoking the super implementation of the proxied method i.e., the actual @Bean method.

factoryContainsBean

private boolean factoryContainsBean(java.lang.String beanName)
Check the beanFactory to see whether the bean named beanName already exists. Accounts for the fact that the requested bean may be "in creation", i.e.: we're in the middle of servicing the initial request for this bean. From an enhanced factory method's perspective, this means that the bean does not actually yet exist, and that it is now our job to create it for the first time by executing the logic in the corresponding factory method.

Said another way, this check repurposes ConfigurableBeanFactory.isCurrentlyInCreation(String) to determine whether the container is calling this method or the user is calling this method.

Parameters:
beanName - name of bean to check for
Returns:
whether beanName already exists in the factory

enhanceFactoryBean

private java.lang.Object enhanceFactoryBean(java.lang.Class<?> fbClass,
                                            java.lang.String beanName)
                                     throws java.lang.InstantiationException,
                                            java.lang.IllegalAccessException
Create a subclass proxy that intercepts calls to getObject(), delegating to the current BeanFactory instead of creating a new instance. These proxies are created only when calling a FactoryBean from within a Bean method, allowing for proper scoping semantics even when working against the FactoryBean instance directly. If a FactoryBean instance is fetched through the container via &-dereferencing, it will not be proxied. This too is aligned with the way XML configuration works.

Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException