CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework--spring-aop

Spring AOP module providing aspect-oriented programming capabilities for the Spring Framework

Pending
Overview
Eval results
Files

proxy-creation.mddocs/

Proxy Creation and Management

ProxyFactory and related classes provide programmatic creation of AOP proxies with comprehensive configuration options for different proxy strategies, target source management, and advisor chains. This system enables flexible runtime proxy creation for both interface-based and class-based proxying scenarios.

Capabilities

ProxyFactory - Main Entry Point

The primary class for creating AOP proxies programmatically, providing fluent configuration and multiple creation strategies.

public class ProxyFactory extends ProxyCreatorSupport {
    /**
     * Create a new ProxyFactory.
     */
    public ProxyFactory();
    
    /**
     * Create a new ProxyFactory.
     * Will proxy all interfaces that the given target implements.
     * @param target the target object to be proxied
     */
    public ProxyFactory(Object target);
    
    /**
     * Create a new ProxyFactory.
     * No target, only interfaces. Must add interceptors.
     * @param proxyInterfaces the interfaces that the proxy should implement
     */
    public ProxyFactory(Class<?>... proxyInterfaces);
    
    /**
     * Create a new ProxyFactory for the given interface and interceptor.
     * Convenience method for creating a proxy for a single interceptor,
     * assuming that the interceptor handles all calls itself rather than
     * delegating to a target, like in the case of remoting proxies.
     * @param proxyInterface the interface that the proxy should implement
     * @param interceptor the interceptor that the proxy should invoke
     */
    public ProxyFactory(Class<?> proxyInterface, Interceptor interceptor);
    
    /**
     * Create a ProxyFactory for the specified {@code TargetSource}.
     * @param proxyInterface the interface that the proxy should implement
     * @param targetSource the source for the target object to be proxied
     */
    public ProxyFactory(Class<?> proxyInterface, TargetSource targetSource);
    
    /**
     * Create a proxy according to this factory's settings.
     * <p>Can be called repeatedly. Effect will vary if we've added
     * or removed interfaces. Can add and remove interceptors.
     * <p>Uses the default class loader: usually, the thread context class loader
     * (if necessary for proxy creation).
     * @return the proxy object
     */
    public Object getProxy();
    
    /**
     * Create a proxy according to this factory's settings.
     * <p>Uses the given class loader (if necessary for proxy creation).
     * @param classLoader the class loader to create the proxy with
     * (or {@code null} for the low-level proxy facility's default)
     * @return the proxy object
     */
    public Object getProxy(ClassLoader classLoader);
    
    /**
     * Determine the proxy class according to this factory's settings.
     * <p>Uses the default class loader: usually, the thread context class loader
     * (if necessary for proxy creation).
     * @return the proxy class
     */
    public Class<?> getProxyClass();
    
    /**
     * Determine the proxy class according to this factory's settings.
     * <p>Uses the given class loader (if necessary for proxy creation).
     * @param classLoader the class loader to create the proxy with
     * (or {@code null} for the low-level proxy facility's default)
     * @return the proxy class
     */
    public Class<?> getProxyClass(ClassLoader classLoader);
    
    // Static convenience methods
    
    /**
     * Create a proxy for the specified {@code TargetSource}.
     * @param targetSource the source for the target object to be proxied
     * @return the proxy object
     */
    public static Object getProxy(TargetSource targetSource);
    
    /**
     * Create a proxy for the specified {@code TargetSource},
     * using the given class loader.
     * @param targetSource the source for the target object to be proxied
     * @param classLoader the class loader to use for the proxy
     * @return the proxy object
     */
    public static Object getProxy(TargetSource targetSource, ClassLoader classLoader);
    
    /**
     * Create a proxy for the given interface and interceptor.
     * @param proxyInterface the interface that the proxy should implement
     * @param interceptor the interceptor that the proxy should invoke
     * @return the proxy object
     */
    public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor);
    
    /**
     * Create a proxy for the specified {@code TargetSource} that extends
     * the target class of the {@code TargetSource}.
     * @param proxyInterface the interface that the proxy should implement
     * @param targetSource the {@code TargetSource} the proxy should delegate to
     * @return the proxy object
     */
    public static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource);
}

ProxyCreatorSupport - Base Class

Base class for proxy factories, providing common functionality for AOP proxy creation and configuration.

public class ProxyCreatorSupport extends AdvisedSupport {
    /**
     * Set the AopProxyFactory to use.
     * <p>Default is {@link DefaultAopProxyFactory}, creating either a CGLIB proxy
     * or a JDK dynamic proxy.
     * @param aopProxyFactory the AOP proxy factory to use
     */
    public void setAopProxyFactory(AopProxyFactory aopProxyFactory);
    
    /**
     * Return the AopProxyFactory that this ProxyConfig uses.
     */
    public AopProxyFactory getAopProxyFactory();
    
    /**
     * Call this method on a new instance created by the no-arg constructor
     * to create an independent copy of the configuration from the given object.
     * @param other the AdvisedSupport object to copy configuration from
     */
    public void copyConfigurationFrom(AdvisedSupport other);
    
    /**
     * Create an AOP proxy for this configuration.
     * @return the AOP proxy for this configuration
     * @throws AopConfigException if the configuration is invalid
     */
    protected final synchronized AopProxy createAopProxy();
    
    /**
     * Copy configuration from the other config object.
     * @param other configuration to copy from
     */
    protected void copyConfigurationFrom(AdvisedSupport other, TargetSource targetSource, List<Advisor> advisors);
}

AOP Proxy Factory System

Factory interfaces and implementations for creating different types of AOP proxies.

public interface AopProxyFactory {
    /**
     * Create an {@link AopProxy} for the given AOP configuration.
     * @param config the AOP configuration in the form of an
     * AdvisedSupport object
     * @return the corresponding AOP proxy
     * @throws AopConfigException if the configuration is invalid
     */
    AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException;
}

public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
    @Override
    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException;
}

public interface AopProxy {
    /**
     * Create a new proxy object.
     * <p>Uses AopProxy's default class loader (if necessary for proxy creation):
     * usually, the thread context class loader.
     * @return the new proxy object (never {@code null})
     * @see Thread#getContextClassLoader()
     */
    Object getProxy();
    
    /**
     * Create a new proxy object.
     * <p>Uses the given class loader (if necessary for proxy creation).
     * {@code null} will simply be passed down and thus lead to the low-level
     * proxy facility's default, which is usually different from the default
     * chosen by the AopProxy implementation's {@link #getProxy()} method.
     * @param classLoader the class loader to create the proxy with
     * (or {@code null} for the low-level proxy facility's default)
     * @return the new proxy object (never {@code null})
     */
    Object getProxy(ClassLoader classLoader);
    
    /**
     * Return the proxy class, as opposed to the proxy instance.
     * <p>This is essentially equivalent to {@code getProxy().getClass()}
     * but more efficient (no proxy instantiation required).
     * @param classLoader the class loader to determine the proxy class for
     * (may be {@code null} to prioritize the AopProxy implementation's
     * chosen default class loader)
     * @return the proxy class (never {@code null})
     */
    Class<?> getProxyClass(ClassLoader classLoader);
}

Proxy Configuration Support

Configuration classes and interfaces for managing AOP proxy settings.

public class ProxyConfig implements Serializable {
    /**
     * Set whether to proxy the target class directly, instead of just proxying
     * specific interfaces. Default is "false".
     * <p>Set this to "true" to force proxying for the TargetSource's exposed
     * target class. If that target class is an interface, a JDK proxy will be
     * created for the given interface. If that target class is any other class,
     * a CGLIB proxy will be created for the given class.
     * @param proxyTargetClass whether to proxy the target class directly as well
     */
    public void setProxyTargetClass(boolean proxyTargetClass);
    
    /**
     * Return whether the proxy should be cast to advised.
     */
    public boolean isProxyTargetClass();
    
    /**
     * Set whether proxies should perform aggressive optimizations.
     * The exact meaning of "aggressive optimizations" will differ
     * between proxies, but there is usually some tradeoff.
     * Default is "false".
     * @param optimize whether to apply aggressive optimizations
     */
    public void setOptimize(boolean optimize);
    
    /**
     * Return whether proxies should perform aggressive optimizations.
     */
    public boolean isOptimize();
    
    /**
     * Set whether this config should be frozen.
     * <p>When a config is frozen, no advice changes can be made. This is
     * useful for optimization, and useful when we don't want callers to
     * be able to manipulate configuration after casting to Advised.
     * @param frozen whether configuration should be frozen
     */
    public void setFrozen(boolean frozen);
    
    /**
     * Return whether the config is frozen, and no advice changes can be made.
     */
    public boolean isFrozen();
    
    /**
     * Set whether the proxy created by this configuration should be exposed
     * via the {@link AopContext} so that it can be accessed by the target.
     * <p>Default is "false", meaning that no guarantees are made that the target
     * will be able to access the proxy. If this is set to "true", the proxy
     * will be accessible via {@link AopContext#currentProxy()}, provided that
     * the AopContext is configured to hold it.
     * @param exposeProxy whether to expose the proxy in the AopContext
     */
    public void setExposeProxy(boolean exposeProxy);
    
    /**
     * Return whether the factory should expose the proxy.
     */
    public boolean isExposeProxy();
    
    /**
     * Set whether this proxy configuration is pre-filtered so that it only
     * contains applicable advisors (matching this proxy's target class).
     * <p>Default is "false". Set this to "true" if the advisors have been
     * pre-filtered already, meaning that the ClassFilter check can be skipped
     * when building the actual advisor chain for proxy invocations.
     * @param preFiltered whether the proxy is pre-filtered
     * @see ClassFilter
     */
    public void setPreFiltered(boolean preFiltered);
    
    /**
     * Return whether this proxy configuration is pre-filtered so that it only
     * contains applicable advisors (matching this proxy's target class).
     */
    public boolean isPreFiltered();
}

public interface Advised extends TargetClassAware {
    /**
     * Return whether the Advised configuration is frozen,
     * in which case no advice changes can be made.
     */
    boolean isFrozen();
    
    /**
     * Are we proxying the full target class instead of specified interfaces?
     */
    boolean isProxyTargetClass();
    
    /**
     * Return the interfaces proxied by the AOP proxy.
     * <p>Will not include the target class, which may also be proxied.
     */
    Class<?>[] getProxiedInterfaces();
    
    /**
     * Determine whether the given interface is proxied.
     * @param intf the interface to check
     */
    boolean isInterfaceProxied(Class<?> intf);
    
    /**
     * Change the {@code TargetSource} used by this {@code Advised} object.
     * Only works if the configuration isn't {@linkplain #isFrozen frozen}.
     * @param targetSource new TargetSource to use
     */
    void setTargetSource(TargetSource targetSource);
    
    /**
     * Return the {@code TargetSource} used by this {@code Advised} object.
     */
    TargetSource getTargetSource();
    
    /**
     * Set whether the proxy should be exposed by the AOP framework as a
     * {@link ThreadLocal} for retrieval via the {@link AopContext} class.
     * <p>It can be necessary to expose proxy if an advised object needs
     * to call another advised method on itself. (If it uses {@code this},
     * the call will not be advised).
     * <p>Default is "false", for optimal performance.
     */
    void setExposeProxy(boolean exposeProxy);
    
    /**
     * Return whether the factory should expose the proxy as a ThreadLocal
     * to be retrievable via the AopContext class. This is particularly
     * useful if an advised object needs to call another advised method on itself.
     */
    boolean isExposeProxy();
    
    /**
     * Set whether this proxy configuration is pre-filtered so that it only
     * contains applicable advisors (matching this proxy's target class).
     * <p>Default is "false". Set this to "true" if the advisors have been
     * pre-filtered already, meaning that the ClassFilter check can be skipped
     * when building the actual advisor chain for proxy invocations.
     * @param preFiltered whether the proxy is pre-filtered
     */
    void setPreFiltered(boolean preFiltered);
    
    /**
     * Return whether this proxy configuration is pre-filtered so that it only
     * contains applicable advisors (matching this proxy's target class).
     */
    boolean isPreFiltered();
    
    /**
     * Return the advisors applying to this proxy.
     * @return a list of Advisors applying to this proxy (never {@code null})
     */
    Advisor[] getAdvisors();
    
    /**
     * Add an advisor at the end of the advisor chain.
     * <p>The Advisor may be an {@link org.springframework.aop.IntroductionAdvisor},
     * in which case the advised interfaces will be those supported by the advisor.
     * @param advisor the advisor to add to the end of the chain
     * @throws AopConfigException in case of invalid advice
     */
    void addAdvisor(Advisor advisor) throws AopConfigException;
    
    /**
     * Add an Advisor at the specified position in the chain.
     * @param pos position in chain (0 is head). Must be valid.
     * @param advisor advisor to add at the specified position in the chain
     * @throws AopConfigException in case of invalid advice
     */
    void addAdvisor(int pos, Advisor advisor) throws AopConfigException;
    
    /**
     * Remove the given advisor.
     * @param advisor the advisor to remove
     * @return {@code true} if the advisor was removed; {@code false}
     * if the advisor was not found in the list
     */
    boolean removeAdvisor(Advisor advisor);
    
    /**
     * Remove the advisor at the given index.
     * @param index the index of advisor to remove
     * @throws AopConfigException if the index is invalid
     */
    void removeAdvisor(int index) throws AopConfigException;
    
    /**
     * Return the index (from 0) of the given advisor,
     * or -1 if no such advisor applies to this proxy.
     * <p>The return value of this method can be used to index into the advisors array.
     * @param advisor the advisor to search for
     * @return index from 0 of this advisor, or -1 if there's no such advisor
     */
    int indexOf(Advisor advisor);
    
    /**
     * Replace the given advisor.
     * <p><b>Note:</b> If the advisor is an {@link org.springframework.aop.IntroductionAdvisor}
     * and the replacement is not or vice versa, the relevant interfaces will be added
     * or removed from the proxied interfaces list.
     * @param a the advisor to replace
     * @param b the advisor to replace it with
     * @return whether it was replaced. If the advisor wasn't found in the
     * list of advisors, this method returns {@code false} and does nothing.
     * @throws AopConfigException in case of invalid advice
     */
    boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;
    
    /**
     * Add the given AOP Alliance advice to the tail of the advice (advisor) chain.
     * <p>This will be wrapped in a DefaultPointcutAdvisor with a pointcut that always
     * applies, and returned from the {@code getAdvisors()} method in this wrapped form.
     * <p>Note that the given advice will apply to all invocations on the proxy,
     * even to the {@code toString()} method! Use appropriate advice implementations
     * or specify appropriate pointcuts to apply to a narrower set of methods.
     * @param advice the advice to add to the tail of the chain
     * @throws AopConfigException in case of invalid advice
     * @see #addAdvice(int, Advice)
     * @see org.springframework.aop.support.DefaultPointcutAdvisor
     */
    void addAdvice(Advice advice) throws AopConfigException;
    
    /**
     * Add the given AOP Alliance Advice at the specified position in the advice chain.
     * <p>This will be wrapped in a {@link org.springframework.aop.support.DefaultPointcutAdvisor}
     * with a pointcut that always applies, and returned from the {@link #getAdvisors()}
     * method in this wrapped form.
     * <p>Note: The given advice will apply to all invocations on the proxy,
     * even to the {@code toString()} method! Use appropriate advice implementations
     * or specify appropriate pointcuts to apply to a narrower set of methods.
     * @param pos index from 0 (head)
     * @param advice advice to add at the specified position in the advice chain
     * @throws AopConfigException in case of invalid advice
     */
    void addAdvice(int pos, Advice advice) throws AopConfigException;
    
    /**
     * Remove the Advisor containing the given advice.
     * @param advice the advice to remove
     * @return {@code true} of the advice was found and removed;
     * {@code false} if there was no such advice
     */
    boolean removeAdvice(Advice advice);
    
    /**
     * Return the index (from 0) of the given AOP Alliance Advice,
     * or -1 if no such advice is an advice for this proxy.
     * <p>The return value of this method can be used to index into
     * the advisors array.
     * @param advice AOP Alliance advice to search for
     * @return index from 0 of this advice, or -1 if there's no such advice
     */
    int indexOf(Advice advice);
    
    /**
     * As {@code toString()} will normally be delegated to the target,
     * this returns the equivalent for the AOP proxy.
     * @return a string description of the proxy configuration
     */
    String toProxyConfigString();
}

Support Classes

Utility classes for AOP proxy creation and management.

public final class AopContext {
    /**
     * Try to return the current AOP proxy. This method is usable only if the
     * calling method has been invoked via AOP, and the AOP framework has been set
     * to expose proxies. Otherwise, this method will throw an IllegalStateException.
     * @return the current AOP proxy (never returns {@code null})
     * @throws IllegalStateException if the proxy cannot be found, because the
     * method was invoked outside an AOP invocation context, or because the
     * AOP framework has not been configured to expose the proxy
     */
    public static Object currentProxy() throws IllegalStateException;
}

public class ProxyProcessorSupport extends ProxyConfig implements Ordered, BeanClassLoaderAware, AopInfrastructureBean {
    /**
     * This should run after all other processors, so that it can just add
     * an advisor to existing proxies rather than double-proxy.
     */
    private int order = Ordered.LOWEST_PRECEDENCE;
    
    @Override
    public void setBeanClassLoader(ClassLoader classLoader);
    
    /**
     * Return the currently configured {@link ClassLoader}, or {@code null}
     * if none configured yet.
     * <p>The default implementation simply returns {@code null}, as this
     * base class does not hold a ClassLoader reference by default.
     */
    protected ClassLoader getProxyClassLoader();
    
    /**
     * Check whether the given bean class is an infrastructure class that should
     * never be proxied.
     * <p>The default implementation considers Advices, Advisors and AopInfrastructureBeans
     * as infrastructure classes.
     * @param beanClass the class of the bean
     * @return whether the bean represents an infrastructure class
     * @see org.aopalliance.aop.Advice
     * @see org.springframework.aop.Advisor
     * @see org.springframework.aop.framework.AopInfrastructureBean
     * @see #shouldSkip
     */
    protected boolean isInfrastructureClass(Class<?> beanClass);
}

Usage Examples

Basic Proxy Creation

// Create a simple proxy with method interceptor
Object target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);

MethodInterceptor interceptor = invocation -> {
    System.out.println("Before: " + invocation.getMethod().getName());
    Object result = invocation.proceed();
    System.out.println("After: " + invocation.getMethod().getName());
    return result;
};

factory.addAdvice(interceptor);
MyService proxy = (MyService) factory.getProxy();

Interface-Based Proxy

// Proxy specific interfaces only
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new MyServiceImpl());
factory.setInterfaces(MyService.class, AnotherInterface.class);
factory.addAdvice(new LoggingInterceptor());

MyService proxy = (MyService) factory.getProxy();

Class-Based Proxy (CGLIB)

// Force CGLIB proxying for target class
ProxyFactory factory = new ProxyFactory();
factory.setTarget(new ConcreteService());
factory.setProxyTargetClass(true); // Use CGLIB
factory.addAdvisor(new TransactionAdvisor());

ConcreteService proxy = (ConcreteService) factory.getProxy();

Advanced Configuration

ProxyFactory factory = new ProxyFactory();
factory.setTarget(target);

// Configure proxy behavior
factory.setOptimize(true);
factory.setFrozen(false);
factory.setExposeProxy(true);
factory.setPreFiltered(true);

// Add multiple advisors
factory.addAdvisor(new SecurityAdvisor());
factory.addAdvisor(new CachingAdvisor());
factory.addAdvisor(new LoggingAdvisor());

// Create proxy with custom class loader
Object proxy = factory.getProxy(Thread.currentThread().getContextClassLoader());

Static Factory Methods

// Convenience methods for common scenarios
MyService proxy1 = ProxyFactory.getProxy(MyService.class, loggingInterceptor);

TargetSource targetSource = new SingletonTargetSource(new MyServiceImpl());
MyService proxy2 = ProxyFactory.getProxy(MyService.class, targetSource);

Install with Tessl CLI

npx tessl i tessl/maven-org-springframework--spring-aop

docs

advice-interceptors.md

aspectj-integration.md

auto-proxy.md

core-abstractions.md

index.md

pointcuts.md

proxy-creation.md

target-sources.md

tile.json