Spring AOP module providing aspect-oriented programming capabilities for the Spring Framework
npx @tessl/cli install tessl/maven-org-springframework--spring-aop@6.2.0Spring AOP provides comprehensive aspect-oriented programming capabilities for the Spring Framework, enabling developers to implement cross-cutting concerns such as logging, security, transaction management, and monitoring in a modular and reusable way. It offers both programmatic and declarative approaches to AOP through proxy-based interception, support for AspectJ integration, method-level and class-level pointcut expressions, and a rich set of advice types.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.2.8</version>
</dependency>import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.aop.Advice;import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.aopalliance.intercept.MethodInterceptor;
// Create an interceptor for logging
MethodInterceptor loggingInterceptor = invocation -> {
System.out.println("Before method: " + invocation.getMethod().getName());
Object result = invocation.proceed();
System.out.println("After method: " + invocation.getMethod().getName());
return result;
};
// Create a pointcut that matches methods by name
NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
pointcut.setMappedName("businessMethod");
// Create an advisor combining pointcut and advice
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, loggingInterceptor);
// Create proxy factory and configure it
ProxyFactory factory = new ProxyFactory(targetObject);
factory.addAdvisor(advisor);
// Create and use the proxy
MyBusinessInterface proxy = (MyBusinessInterface) factory.getProxy();
proxy.businessMethod(); // Logging will occur around the method callSpring AOP is built on a layered architecture of key components:
The framework integrates with both AOP Alliance standard interfaces and AspectJ for maximum flexibility and interoperability.
Fundamental interfaces and classes for pointcuts, advisors, and advice types. These form the foundation of Spring's AOP system and provide the basic building blocks for aspect-oriented programming.
public interface Pointcut {
ClassFilter getClassFilter();
MethodMatcher getMethodMatcher();
Pointcut TRUE = TruePointcut.INSTANCE;
}
public interface Advisor {
Advice getAdvice();
boolean isPerInstance();
Advice EMPTY_ADVICE = new Advice() {};
}
public interface PointcutAdvisor extends Advisor {
Pointcut getPointcut();
}ProxyFactory and related classes for creating AOP proxies programmatically, including configuration options for different proxy strategies and target source management.
public class ProxyFactory extends ProxyCreatorSupport {
public ProxyFactory();
public ProxyFactory(Object target);
public ProxyFactory(Class<?>... proxyInterfaces);
public Object getProxy();
public Object getProxy(ClassLoader classLoader);
public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor);
}Support for AspectJ pointcut expressions, @AspectJ annotations, and aspect instance management, providing seamless integration with AspectJ weaving capabilities.
public class AspectJExpressionPointcut implements ClassFilter, MethodMatcher, Pointcut {
public void setExpression(String expression);
public String getExpression();
public boolean matches(Class<?> clazz);
public boolean matches(Method method, Class<?> targetClass);
}
public class AspectJProxyFactory extends ProxyCreatorSupport {
public void addAspect(Object aspectInstance);
public void addAspect(Class<?> aspectClass);
public <T> T getProxy();
}Automatic proxy creation through BeanPostProcessor implementations that analyze beans for advisor applicability and create proxies transparently.
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements BeanPostProcessor, BeanFactoryAware {
public void setProxyTargetClass(boolean proxyTargetClass);
public void setOptimize(boolean optimize);
public void setFrozen(boolean frozen);
}
public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator {
// Automatically creates proxies for @AspectJ annotated classes
}Various pointcut implementations for different matching strategies including name-based, regular expression, annotation-based, and composable pointcuts.
public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut {
public void setMappedName(String mappedName);
public void setMappedNames(String... mappedNames);
public void addMethodName(String name);
}
public class ComposablePointcut implements Pointcut, Serializable {
public ComposablePointcut union(Pointcut other);
public ComposablePointcut intersection(Pointcut other);
}Implementation of different advice types (before, after, around, throws) and method interceptors for cross-cutting concerns like performance monitoring and debugging.
public interface MethodBeforeAdvice extends BeforeAdvice {
void before(Method method, Object[] args, Object target) throws Throwable;
}
public interface AfterReturningAdvice extends AfterAdvice {
void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
}
public class PerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {
// Monitors method execution times
}Different target source implementations for managing target object lifecycle, including singleton, prototype, pooled, and thread-local target sources.
public interface TargetSource extends TargetClassAware {
Class<?> getTargetClass();
boolean isStatic();
Object getTarget() throws Exception;
void releaseTarget(Object target) throws Exception;
}
public class HotSwappableTargetSource implements TargetSource, Serializable {
public Object swap(Object newTarget) throws IllegalArgumentException;
}// Core marker interfaces
public interface Advice {
// Tag interface for all advice types
}
public interface BeforeAdvice extends Advice {
// Marker interface for before advice
}
public interface AfterAdvice extends Advice {
// Marker interface for after advice
}
// Filtering interfaces
public interface ClassFilter {
boolean matches(Class<?> clazz);
ClassFilter TRUE = TrueClassFilter.INSTANCE;
}
public interface MethodMatcher {
boolean matches(Method method, Class<?> targetClass);
boolean isRuntime();
boolean matches(Method method, Class<?> targetClass, Object... args);
MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;
}
// Proxy configuration
public class ProxyConfig implements Serializable {
public void setProxyTargetClass(boolean proxyTargetClass);
public void setOptimize(boolean optimize);
public void setOpaque(boolean opaque);
public void setExposeProxy(boolean exposeProxy);
public void setFrozen(boolean frozen);
}
// Exception types
public class AopConfigException extends NestedRuntimeException {
// Thrown when there's a configuration problem with AOP proxy creation
public AopConfigException(String msg);
public AopConfigException(String msg, Throwable cause);
}
public class AopInvocationException extends NestedRuntimeException {
// Thrown when there's a problem with AOP method invocation
public AopInvocationException(String msg);
public AopInvocationException(String msg, Throwable cause);
}