Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Proxy Creation in Spring AOP | Spring AOP Internals: Proxies and Weaving
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Spring AOP Under the Hood

bookProxy Creation in Spring AOP

When Proxy Creation Happens in the Spring Lifecycle

Proxy creation in Spring AOP takes place during the bean initialization phase of the Spring application context lifecycle. You do not need to manually trigger proxy creation; it is handled automatically by the Spring container as part of its standard processing.

Timing of Proxy Instantiation

  • Proxy creation occurs after bean instantiation but before dependency injection is complete;
  • The process happens during the BeanPostProcessor phase, specifically through the BeanPostProcessor implementations such as AbstractAutoProxyCreator;
  • Proxies are created before the bean is returned for use in your application, ensuring that any method calls on the bean are intercepted as needed.

Triggering Proxy Creation

  • Proxy creation is triggered when the container detects that a bean matches an AOP pointcut or is eligible for advice;
  • The presence of AOP-related configuration (such as @Aspect, @EnableAspectJAutoProxy, or XML AOP tags) signals the container to apply proxying logic;
  • If a bean matches the criteria, the container wraps it in a proxy object instead of returning the original bean instance.

This automatic proxying ensures that all AOP advice is applied transparently, without requiring manual intervention. The timing guarantees that your beans are fully proxied and ready for AOP-based interception as soon as they are available for use in your application.

How Method Calls Are Intercepted at Runtime

When you use Spring AOP, method calls on your beans can be intercepted at runtime using proxies. A proxy is an object that wraps your target bean and controls access to it. Instead of calling the target bean's method directly, your application interacts with the proxy, which can add extra behavior before or after the method call.

The Interception Process

  • When you invoke a method on a proxied bean, the call is first received by the proxy object;
  • The proxy determines whether the method matches any configured advice (such as @Around, @Before, or @After);
  • If advice applies, the proxy delegates the call to an interceptor that handles the extra logic;
  • After executing the advice, the proxy calls the target method on the original bean;
  • The result is returned to you, possibly after additional advice is executed.

The Role of InvocationHandler and MethodInterceptor

  • If you are using JDK dynamic proxies (for interfaces), the proxy uses an InvocationHandler to intercept all method calls. The invoke method of InvocationHandler receives information about the called method and its arguments, allowing you to add custom behavior or delegate to the target bean.
  • If you are using CGLIB proxies (for concrete classes), Spring employs a MethodInterceptor. The intercept method of MethodInterceptor is called whenever a method is invoked on the proxy. You can run advice logic in this method, then call methodProxy.invokeSuper() to continue to the actual method on the target bean.

Example: MethodInterceptor in Action

public class LoggingInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before method: " + invocation.getMethod().getName());
        Object result = invocation.proceed();
        System.out.println("After method: " + invocation.getMethod().getName());
        return result;
    }
}

Whenever you call a method on a proxied bean, Spring ensures this interceptor runs before and after the actual method, enabling powerful cross-cutting functionality like logging, security, or transactions.

question mark

Which statement best describes how Spring AOP applies aspects to beans at runtime?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain the difference between JDK dynamic proxies and CGLIB proxies in Spring?

How does Spring decide which proxying mechanism to use for a bean?

Can you give more examples of common use cases for method interception in Spring AOP?

bookProxy Creation in Spring AOP

Swipe um das Menü anzuzeigen

When Proxy Creation Happens in the Spring Lifecycle

Proxy creation in Spring AOP takes place during the bean initialization phase of the Spring application context lifecycle. You do not need to manually trigger proxy creation; it is handled automatically by the Spring container as part of its standard processing.

Timing of Proxy Instantiation

  • Proxy creation occurs after bean instantiation but before dependency injection is complete;
  • The process happens during the BeanPostProcessor phase, specifically through the BeanPostProcessor implementations such as AbstractAutoProxyCreator;
  • Proxies are created before the bean is returned for use in your application, ensuring that any method calls on the bean are intercepted as needed.

Triggering Proxy Creation

  • Proxy creation is triggered when the container detects that a bean matches an AOP pointcut or is eligible for advice;
  • The presence of AOP-related configuration (such as @Aspect, @EnableAspectJAutoProxy, or XML AOP tags) signals the container to apply proxying logic;
  • If a bean matches the criteria, the container wraps it in a proxy object instead of returning the original bean instance.

This automatic proxying ensures that all AOP advice is applied transparently, without requiring manual intervention. The timing guarantees that your beans are fully proxied and ready for AOP-based interception as soon as they are available for use in your application.

How Method Calls Are Intercepted at Runtime

When you use Spring AOP, method calls on your beans can be intercepted at runtime using proxies. A proxy is an object that wraps your target bean and controls access to it. Instead of calling the target bean's method directly, your application interacts with the proxy, which can add extra behavior before or after the method call.

The Interception Process

  • When you invoke a method on a proxied bean, the call is first received by the proxy object;
  • The proxy determines whether the method matches any configured advice (such as @Around, @Before, or @After);
  • If advice applies, the proxy delegates the call to an interceptor that handles the extra logic;
  • After executing the advice, the proxy calls the target method on the original bean;
  • The result is returned to you, possibly after additional advice is executed.

The Role of InvocationHandler and MethodInterceptor

  • If you are using JDK dynamic proxies (for interfaces), the proxy uses an InvocationHandler to intercept all method calls. The invoke method of InvocationHandler receives information about the called method and its arguments, allowing you to add custom behavior or delegate to the target bean.
  • If you are using CGLIB proxies (for concrete classes), Spring employs a MethodInterceptor. The intercept method of MethodInterceptor is called whenever a method is invoked on the proxy. You can run advice logic in this method, then call methodProxy.invokeSuper() to continue to the actual method on the target bean.

Example: MethodInterceptor in Action

public class LoggingInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Before method: " + invocation.getMethod().getName());
        Object result = invocation.proceed();
        System.out.println("After method: " + invocation.getMethod().getName());
        return result;
    }
}

Whenever you call a method on a proxied bean, Spring ensures this interceptor runs before and after the actual method, enabling powerful cross-cutting functionality like logging, security, or transactions.

question mark

Which statement best describes how Spring AOP applies aspects to beans at runtime?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 1
some-alt