Spring AOP Control Flow Pointcut

The Control Flow pointcut allows us to define a method pointcut that applies advice only when a method is invoked from a specific class or a specific method in a class.

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.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class BusinessAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] boObjects, Object object)
                    throws Throwable {
        System.out.println("Calling before " + method);

3. 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.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

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

public class Main {
	public static void main(String args[]) {
		BusinessImpl bImpl =  new BusinessImpl();
		Pointcut pointcut = new ControlFlowPointcut(Main.class,"controlFlowTest");
		Advice advice = new BusinessAdvice();
		Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);
		ProxyFactory proxyFactory = new ProxyFactory();
        BusinessImpl normalProxy =  (BusinessImpl) proxyFactory.getProxy();
        System.out.println("Calling Normally");
        System.out.println("\nCalling in ControlFlow");
	public static void controlFlowTest(BusinessImpl bImpl) {

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

Figure 19.1 Figure 18.1

You can download the source code of this example here.

No comments yet.

Leave a Reply