rockydgeekgod rockydgeekgod - 1 year ago 91
Java Question

Spring AOP Pointcut does not trigger

I am new to Spring and AOP. I am trying this simple thing where I have created a custom annotation which when placed before any method should execute some code.
This is the annotation I created

// Declares a custom annotation that validates json
public @interface JsonSchemaAnnotation {

Next I created the Spring Aspect class which holds the logic

public class UpdateUIMetadataInterceptor {

@Pointcut("execution(public **.*(..))")
public void anyPublicMethod() {

@Before("anyPublicMethod() && @annotation(jsonSchemaAnnotation)")
public void validateJson(ProceedingJoinPoint pjp) throws Throwable {


And this is my simple test class

public class ValidationTest {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/configuration.xml");
String jsondata = "{\"id\": \"EXPENSE_REPORT\",\"properties\": {\"transactionType\": \"EXPENSE_REPORT\"},\"sections\": []} ]}";
ValidationTest test = new ValidationTest();
test.jsonValidationTest("dummy", jsondata);
((AbstractApplicationContext) context).close();


public void jsonValidationTest(String dummy, String jsondata) {


The problem is my spring aop never gets triggered. I have included a bean in my

<aop:include name="UpdateUIMetadataInterceptor" />
<bean id="updateUI" class="" />

Can anyone point out what I am missing?

Answer Source

You have several problems with your code:

  1. You should create your ValidationTest object as a bean managed by Spring and not using new
  2. <aop:include name="UpdateUIMetadataInterceptor" /> should be <aop:include name="updateUI"/>; you can actually just stick with <aop:aspectj-autoproxy/> for simplicity here
  3. ProceedingJoinPoint is not supported for before aspects, so remove it; you can use JoinPoint instead if you need access to arguments
  4. JsonSchemaAnnotation jsonSchemaAnnotation parameter should be present for validateJson method of your aspect, as pointed out by frant.hartm