Spring AOP Dynamic Pointcut

The dynamic pointcuts are evaluated once while generating the proxy and further for each of the request for final evaluation. The dynamic pointcut is used for advice that has to be decided to apply at joinpoint after considering the runtime target method arguments. Dynamic pointcut evaluates for each method invocation and the result cannot be cached since this takes the target method arguments into consideration.

Type of Dynamic Pointcut

  • Control Flow Pointcut – For Reference, Click Here

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

    <!-- Proxy class need this library -->

1. Business Logic and Implementation

We will first write our business logic and then we will add Spring AOP to profile our business methods.

Create an interface BusinessService and write a following method. We will intercept this via Advice


package com.kruders.spring.aop;
public interface BusinessService {
    void doSomeThing();

Now create a class BusinessImpl.java that implements the above interface.


package com.kruders.spring.aop;
import org.springframework.stereotype.Service;
public class BusinessImpl implements BusinessService {
    public void doSomeThing() {
        System.out.println(&quot;Do Something Here&quot;);

2. Advice Class

Now write a Advice which will profile our business method.


package com.kruders.spring.advice;

import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class BusinessAdvice implements MethodInterceptor {
	public Object invoke(MethodInvocation method) throws Throwable {
		System.out.println(&quot;Method name : &quot; + method.getMethod().getName());
		System.out.println(&quot;Method arguments : &quot; + Arrays.toString(method.getArguments()));
		System.out.println(&quot;Around method is called&quot;);
	    System.out.println(&quot;Around before is running&quot;);
		System.out.println(&quot;Around after is running&quot;);
		return null;

3. Pointcut

Now write a Pointcut that extends DynamicMethodMatcherPointcut. Here we set the matching rules on the type of class and on the method.


package com.kruders.spring.pointcut;

import java.lang.reflect.Method;

import org.springframework.aop.support.DynamicMethodMatcherPointcut;

public class SimpleDyanmicPointcut extends DynamicMethodMatcherPointcut{
     public boolean matches(Method method, Class clazz, Object[] objects) {
         System.out.println(&quot;\nDynamic Checking for &quot; + method.getName());
         int testVal = ((Integer) objects[0]).intValue();
         return (testVal != 100);

Here we use a class that executes the static match(matches(Method method, Class clazz)) and dynamic match(matches(Method method, Class clazz, Object[] objects))

4. Helper Class

Create Main.java class that loads our Business bean from Spring Context and then calling our business method.


package com.kruders.spring.core;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

import com.kruders.spring.advice.BusinessAdvice;
import com.kruders.spring.aop.BusinessImpl;
import com.kruders.spring.pointcut.SimpleDyanmicPointcut;

public class Main {
	public static void main(String args[]) {
		BusinessImpl bImpl =  new BusinessImpl();
		BusinessImpl beanProxy;
		Pointcut pointcut = new SimpleDyanmicPointcut();
		Advice advice = new BusinessAdvice();
		Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
		ProxyFactory proxyFactory = new ProxyFactory();
        beanProxy = (BusinessImpl) proxyFactory.getProxy();

The folder structure of the example is shown below in Figure 18.1

Figure 18.1 Figure 18.1

You can download the source code of this example here.

No comments yet.

Leave a Reply