org.springframework.aop.framework.autoproxy

Class AbstractAutoProxyCreator

    • Constructor Detail

      • AbstractAutoProxyCreator

        public AbstractAutoProxyCreator()
    • Method Detail

      • setFrozen

        public void setFrozen(boolean frozen)
        Set whether or not the proxy should be frozen, preventing advice from being added to it once it is created.

        Overridden from the super class to prevent the proxy configuration from being frozen before the proxy is created.

        Overrides:
        setFrozen in class ProxyConfig
      • isFrozen

        public boolean isFrozen()
        Description copied from class: ProxyConfig
        Return whether the config is frozen, and no advice changes can be made.
        Overrides:
        isFrozen in class ProxyConfig
      • setCustomTargetSourceCreators

        public void setCustomTargetSourceCreators(TargetSourceCreator... targetSourceCreators)
        Set custom TargetSourceCreators to be applied in this order. If the list is empty, or they all return null, a SingletonTargetSource will be created for each bean.

        Note that TargetSourceCreators will kick in even for target beans where no advices or advisors have been found. If a TargetSourceCreator returns a TargetSource for a specific bean, that bean will be proxied in any case.

        TargetSourceCreators can only be invoked if this post processor is used in a BeanFactory and its BeanFactoryAware callback is triggered.

        Parameters:
        targetSourceCreators - the list of TargetSourceCreators. Ordering is significant: The TargetSource returned from the first matching TargetSourceCreator (that is, the first that returns non-null) will be used.
      • setInterceptorNames

        public void setInterceptorNames(java.lang.String... interceptorNames)
        Set the common interceptors. These must be bean names in the current factory. They can be of any advice or advisor type Spring supports.

        If this property isn't set, there will be zero common interceptors. This is perfectly valid, if "specific" interceptors such as matching Advisors are all we want.

      • setApplyCommonInterceptorsFirst

        public void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst)
        Set whether the common interceptors should be applied before bean-specific ones. Default is "true"; else, bean-specific interceptors will get applied first.
      • getBeanFactory

        @Nullable
        protected BeanFactory getBeanFactory()
        Return the owning BeanFactory. May be null, as this post-processor doesn't need to belong to a bean factory.
      • determineCandidateConstructors

        @Nullable
        public java.lang.reflect.Constructor<?>[] determineCandidateConstructors(java.lang.Class<?> beanClass,
                                                                                           java.lang.String beanName)
        Description copied from interface: SmartInstantiationAwareBeanPostProcessor
        Determine the candidate constructors to use for the given bean.

        The default implementation returns null.

        Specified by:
        determineCandidateConstructors in interface SmartInstantiationAwareBeanPostProcessor
        Parameters:
        beanClass - the raw class of the bean (never null)
        beanName - the name of the bean
        Returns:
        the candidate constructors, or null if none specified
      • getEarlyBeanReference

        public java.lang.Object getEarlyBeanReference(java.lang.Object bean,
                                                      java.lang.String beanName)
        Description copied from interface: SmartInstantiationAwareBeanPostProcessor
        Obtain a reference for early access to the specified bean, typically for the purpose of resolving a circular reference.

        This callback gives post-processors a chance to expose a wrapper early - that is, before the target bean instance is fully initialized. The exposed object should be equivalent to the what BeanPostProcessor.postProcessBeforeInitialization(java.lang.Object, java.lang.String) / BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String) would expose otherwise. Note that the object returned by this method will be used as bean reference unless the post-processor returns a different wrapper from said post-process callbacks. In other words: Those post-process callbacks may either eventually expose the same reference or alternatively return the raw bean instance from those subsequent callbacks (if the wrapper for the affected bean has been built for a call to this method already, it will be exposes as final bean reference by default).

        The default implementation returns the given bean as-is.

        Specified by:
        getEarlyBeanReference in interface SmartInstantiationAwareBeanPostProcessor
        Parameters:
        bean - the raw bean instance
        beanName - the name of the bean
        Returns:
        the object to expose as bean reference (typically with the passed-in bean instance as default)
      • postProcessAfterInstantiation

        public boolean postProcessAfterInstantiation(java.lang.Object bean,
                                                     java.lang.String beanName)
        Description copied from interface: InstantiationAwareBeanPostProcessor
        Perform operations after the bean has been instantiated, via a constructor or factory method, but before Spring property population (from explicit properties or autowiring) occurs.

        This is the ideal callback for performing custom field injection on the given bean instance, right before Spring's autowiring kicks in.

        The default implementation returns true.

        Specified by:
        postProcessAfterInstantiation in interface InstantiationAwareBeanPostProcessor
        Parameters:
        bean - the bean instance created, with properties not having been set yet
        beanName - the name of the bean
        Returns:
        true if properties should be set on the bean; false if property population should be skipped. Normal implementations should return true. Returning false will also prevent any subsequent InstantiationAwareBeanPostProcessor instances being invoked on this bean instance.
        See Also:
        InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)
      • postProcessBeforeInitialization

        public java.lang.Object postProcessBeforeInitialization(java.lang.Object bean,
                                                                java.lang.String beanName)
        Description copied from interface: BeanPostProcessor
        Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

        The default implementation returns the given bean as-is.

        Specified by:
        postProcessBeforeInitialization in interface BeanPostProcessor
        Parameters:
        bean - the new bean instance
        beanName - the name of the bean
        Returns:
        the bean instance to use, either the original or a wrapped one; if null, no subsequent BeanPostProcessors will be invoked
        See Also:
        InitializingBean.afterPropertiesSet()
      • getCacheKey

        protected java.lang.Object getCacheKey(java.lang.Class<?> beanClass,
                                               @Nullable
                                               java.lang.String beanName)
        Build a cache key for the given bean class and bean name.

        Note: As of 4.2.3, this implementation does not return a concatenated class/name String anymore but rather the most efficient cache key possible: a plain bean name, prepended with BeanFactory.FACTORY_BEAN_PREFIX in case of a FactoryBean; or if no bean name specified, then the given bean Class as-is.

        Parameters:
        beanClass - the bean class
        beanName - the bean name
        Returns:
        the cache key for the given class and name
      • wrapIfNecessary

        protected java.lang.Object wrapIfNecessary(java.lang.Object bean,
                                                   java.lang.String beanName,
                                                   java.lang.Object cacheKey)
        Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
        Parameters:
        bean - the raw bean instance
        beanName - the name of the bean
        cacheKey - the cache key for metadata access
        Returns:
        a proxy wrapping the bean, or the raw bean instance as-is
      • isInfrastructureClass

        protected boolean isInfrastructureClass(java.lang.Class<?> beanClass)
        Return whether the given bean class represents an infrastructure class that should never be proxied.

        The default implementation considers Advices, Advisors and AopInfrastructureBeans as infrastructure classes.

        Parameters:
        beanClass - the class of the bean
        Returns:
        whether the bean represents an infrastructure class
        See Also:
        Advice, Advisor, AopInfrastructureBean, shouldSkip(java.lang.Class<?>, java.lang.String)
      • shouldSkip

        protected boolean shouldSkip(java.lang.Class<?> beanClass,
                                     java.lang.String beanName)
        Subclasses should override this method to return true if the given bean should not be considered for auto-proxying by this post-processor.

        Sometimes we need to be able to avoid this happening, e.g. if it will lead to a circular reference or if the existing target instance needs to be preserved. This implementation returns false unless the bean name indicates an "original instance" according to AutowireCapableBeanFactory conventions.

        Parameters:
        beanClass - the class of the bean
        beanName - the name of the bean
        Returns:
        whether to skip the given bean
        See Also:
        AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX
      • getCustomTargetSource

        @Nullable
        protected TargetSource getCustomTargetSource(java.lang.Class<?> beanClass,
                                                               java.lang.String beanName)
        Create a target source for bean instances. Uses any TargetSourceCreators if set. Returns null if no custom TargetSource should be used.

        This implementation uses the "customTargetSourceCreators" property. Subclasses can override this method to use a different mechanism.

        Parameters:
        beanClass - the class of the bean to create a TargetSource for
        beanName - the name of the bean
        Returns:
        a TargetSource for this bean
        See Also:
        setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator...)
      • createProxy

        protected java.lang.Object createProxy(java.lang.Class<?> beanClass,
                                               @Nullable
                                               java.lang.String beanName,
                                               @Nullable
                                               java.lang.Object[] specificInterceptors,
                                               TargetSource targetSource)
        Create an AOP proxy for the given bean.
        Parameters:
        beanClass - the class of the bean
        beanName - the name of the bean
        specificInterceptors - the set of interceptors that is specific to this bean (may be empty, but not null)
        targetSource - the TargetSource for the proxy, already pre-configured to access the bean
        Returns:
        the AOP proxy for the bean
        See Also:
        buildAdvisors(java.lang.String, java.lang.Object[])
      • buildAdvisors

        protected Advisor[] buildAdvisors(@Nullable
                                          java.lang.String beanName,
                                          @Nullable
                                          java.lang.Object[] specificInterceptors)
        Determine the advisors for the given bean, including the specific interceptors as well as the common interceptor, all adapted to the Advisor interface.
        Parameters:
        beanName - the name of the bean
        specificInterceptors - the set of interceptors that is specific to this bean (may be empty, but not null)
        Returns:
        the list of Advisors for the given bean
      • customizeProxyFactory

        protected void customizeProxyFactory(ProxyFactory proxyFactory)
        Subclasses may choose to implement this: for example, to change the interfaces exposed.

        The default implementation is empty.

        Parameters:
        proxyFactory - a ProxyFactory that is already configured with TargetSource and interfaces and will be used to create the proxy immediately after this method returns
      • getAdvicesAndAdvisorsForBean

        @Nullable
        protected abstract java.lang.Object[] getAdvicesAndAdvisorsForBean(java.lang.Class<?> beanClass,
                                                                                     java.lang.String beanName,
                                                                                     @Nullable
                                                                                     TargetSource customTargetSource)
                                                                              throws BeansException
        Return whether the given bean is to be proxied, what additional advices (e.g. AOP Alliance interceptors) and advisors to apply.
        Parameters:
        beanClass - the class of the bean to advise
        beanName - the name of the bean
        customTargetSource - the TargetSource returned by the getCustomTargetSource(java.lang.Class<?>, java.lang.String) method: may be ignored. Will be null if no custom target source is in use.
        Returns:
        an array of additional interceptors for the particular bean; or an empty array if no additional interceptors but just the common ones; or null if no proxy at all, not even with the common interceptors. See constants DO_NOT_PROXY and PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS.
        Throws:
        BeansException - in case of errors
        See Also:
        DO_NOT_PROXY, PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS