or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advice-interceptors.mdaspectj-integration.mdauto-proxy.mdcore-abstractions.mdindex.mdpointcuts.mdproxy-creation.mdtarget-sources.md

index.mddocs/

0

# Spring AOP

1

2

Spring 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.

3

4

## Package Information

5

6

- **Package Name**: org.springframework:spring-aop

7

- **Package Type**: Maven

8

- **Language**: Java

9

- **Installation**:

10

```xml

11

<dependency>

12

<groupId>org.springframework</groupId>

13

<artifactId>spring-aop</artifactId>

14

<version>6.2.8</version>

15

</dependency>

16

```

17

18

## Core Imports

19

20

```java

21

import org.springframework.aop.framework.ProxyFactory;

22

import org.springframework.aop.support.AopUtils;

23

import org.springframework.aop.Pointcut;

24

import org.springframework.aop.PointcutAdvisor;

25

import org.aopalliance.intercept.MethodInterceptor;

26

import org.aopalliance.aop.Advice;

27

```

28

29

## Basic Usage

30

31

```java

32

import org.springframework.aop.framework.ProxyFactory;

33

import org.springframework.aop.support.DefaultPointcutAdvisor;

34

import org.springframework.aop.support.NameMatchMethodPointcut;

35

import org.aopalliance.intercept.MethodInterceptor;

36

37

// Create an interceptor for logging

38

MethodInterceptor loggingInterceptor = invocation -> {

39

System.out.println("Before method: " + invocation.getMethod().getName());

40

Object result = invocation.proceed();

41

System.out.println("After method: " + invocation.getMethod().getName());

42

return result;

43

};

44

45

// Create a pointcut that matches methods by name

46

NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();

47

pointcut.setMappedName("businessMethod");

48

49

// Create an advisor combining pointcut and advice

50

DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, loggingInterceptor);

51

52

// Create proxy factory and configure it

53

ProxyFactory factory = new ProxyFactory(targetObject);

54

factory.addAdvisor(advisor);

55

56

// Create and use the proxy

57

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

58

proxy.businessMethod(); // Logging will occur around the method call

59

```

60

61

## Architecture

62

63

Spring AOP is built on a layered architecture of key components:

64

65

- **Advice**: The action taken at a particular join point (before, after, around, etc.)

66

- **Pointcut**: Defines where advice should be applied (method patterns, class filters)

67

- **Advisor**: Combines advice with pointcut to determine when and where to apply the advice

68

- **Proxy**: Runtime object that intercepts method calls and applies advice

69

- **Target Source**: Manages access to target objects and their lifecycle

70

71

The framework integrates with both AOP Alliance standard interfaces and AspectJ for maximum flexibility and interoperability.

72

73

## Capabilities

74

75

### Core AOP Abstractions

76

77

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.

78

79

```java { .api }

80

public interface Pointcut {

81

ClassFilter getClassFilter();

82

MethodMatcher getMethodMatcher();

83

Pointcut TRUE = TruePointcut.INSTANCE;

84

}

85

86

public interface Advisor {

87

Advice getAdvice();

88

boolean isPerInstance();

89

Advice EMPTY_ADVICE = new Advice() {};

90

}

91

92

public interface PointcutAdvisor extends Advisor {

93

Pointcut getPointcut();

94

}

95

```

96

97

[Core AOP Abstractions](./core-abstractions.md)

98

99

### Proxy Creation and Management

100

101

ProxyFactory and related classes for creating AOP proxies programmatically, including configuration options for different proxy strategies and target source management.

102

103

```java { .api }

104

public class ProxyFactory extends ProxyCreatorSupport {

105

public ProxyFactory();

106

public ProxyFactory(Object target);

107

public ProxyFactory(Class<?>... proxyInterfaces);

108

public Object getProxy();

109

public Object getProxy(ClassLoader classLoader);

110

public static <T> T getProxy(Class<T> proxyInterface, Interceptor interceptor);

111

}

112

```

113

114

[Proxy Creation](./proxy-creation.md)

115

116

### AspectJ Integration

117

118

Support for AspectJ pointcut expressions, @AspectJ annotations, and aspect instance management, providing seamless integration with AspectJ weaving capabilities.

119

120

```java { .api }

121

public class AspectJExpressionPointcut implements ClassFilter, MethodMatcher, Pointcut {

122

public void setExpression(String expression);

123

public String getExpression();

124

public boolean matches(Class<?> clazz);

125

public boolean matches(Method method, Class<?> targetClass);

126

}

127

128

public class AspectJProxyFactory extends ProxyCreatorSupport {

129

public void addAspect(Object aspectInstance);

130

public void addAspect(Class<?> aspectClass);

131

public <T> T getProxy();

132

}

133

```

134

135

[AspectJ Integration](./aspectj-integration.md)

136

137

### Auto-Proxy Creation

138

139

Automatic proxy creation through BeanPostProcessor implementations that analyze beans for advisor applicability and create proxies transparently.

140

141

```java { .api }

142

public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport

143

implements BeanPostProcessor, BeanFactoryAware {

144

public void setProxyTargetClass(boolean proxyTargetClass);

145

public void setOptimize(boolean optimize);

146

public void setFrozen(boolean frozen);

147

}

148

149

public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator {

150

// Automatically creates proxies for @AspectJ annotated classes

151

}

152

```

153

154

[Auto-Proxy Creation](./auto-proxy.md)

155

156

### Pointcut Implementations

157

158

Various pointcut implementations for different matching strategies including name-based, regular expression, annotation-based, and composable pointcuts.

159

160

```java { .api }

161

public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut {

162

public void setMappedName(String mappedName);

163

public void setMappedNames(String... mappedNames);

164

public void addMethodName(String name);

165

}

166

167

public class ComposablePointcut implements Pointcut, Serializable {

168

public ComposablePointcut union(Pointcut other);

169

public ComposablePointcut intersection(Pointcut other);

170

}

171

```

172

173

[Pointcut Implementations](./pointcuts.md)

174

175

### Advice Types and Interceptors

176

177

Implementation of different advice types (before, after, around, throws) and method interceptors for cross-cutting concerns like performance monitoring and debugging.

178

179

```java { .api }

180

public interface MethodBeforeAdvice extends BeforeAdvice {

181

void before(Method method, Object[] args, Object target) throws Throwable;

182

}

183

184

public interface AfterReturningAdvice extends AfterAdvice {

185

void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;

186

}

187

188

public class PerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {

189

// Monitors method execution times

190

}

191

```

192

193

[Advice and Interceptors](./advice-interceptors.md)

194

195

### Target Source Management

196

197

Different target source implementations for managing target object lifecycle, including singleton, prototype, pooled, and thread-local target sources.

198

199

```java { .api }

200

public interface TargetSource extends TargetClassAware {

201

Class<?> getTargetClass();

202

boolean isStatic();

203

Object getTarget() throws Exception;

204

void releaseTarget(Object target) throws Exception;

205

}

206

207

public class HotSwappableTargetSource implements TargetSource, Serializable {

208

public Object swap(Object newTarget) throws IllegalArgumentException;

209

}

210

```

211

212

[Target Sources](./target-sources.md)

213

214

## Types

215

216

```java { .api }

217

// Core marker interfaces

218

public interface Advice {

219

// Tag interface for all advice types

220

}

221

222

public interface BeforeAdvice extends Advice {

223

// Marker interface for before advice

224

}

225

226

public interface AfterAdvice extends Advice {

227

// Marker interface for after advice

228

}

229

230

// Filtering interfaces

231

public interface ClassFilter {

232

boolean matches(Class<?> clazz);

233

ClassFilter TRUE = TrueClassFilter.INSTANCE;

234

}

235

236

public interface MethodMatcher {

237

boolean matches(Method method, Class<?> targetClass);

238

boolean isRuntime();

239

boolean matches(Method method, Class<?> targetClass, Object... args);

240

MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;

241

}

242

243

// Proxy configuration

244

public class ProxyConfig implements Serializable {

245

public void setProxyTargetClass(boolean proxyTargetClass);

246

public void setOptimize(boolean optimize);

247

public void setOpaque(boolean opaque);

248

public void setExposeProxy(boolean exposeProxy);

249

public void setFrozen(boolean frozen);

250

}

251

252

// Exception types

253

public class AopConfigException extends NestedRuntimeException {

254

// Thrown when there's a configuration problem with AOP proxy creation

255

public AopConfigException(String msg);

256

public AopConfigException(String msg, Throwable cause);

257

}

258

259

public class AopInvocationException extends NestedRuntimeException {

260

// Thrown when there's a problem with AOP method invocation

261

public AopInvocationException(String msg);

262

public AopInvocationException(String msg, Throwable cause);

263

}

264

```