简体   繁体   中英

Spring AOP @Pointcut not triggering

I want to trigger a method when a particular method from another class is invoked that is why I thought of using @Pointcut.

The code below is almost identical to the that I am coding and I don't what else I have to add.

public class OrgManagerImpl implements OrgManager {
    public IOrg getOrg(String orgShortName) {
    }
}

and this is the class that will should be triggered:

@Aspect
public class OrgManagerSynchronizer { 

    @Pointcut("execution(* com.alvin.OrgManager.getOrg(..))")
    public void classMethods() {}

    @Before("classMethods()")
    public void synchronize(JoinPoint jp) {
        //code should be executed. but does not execute.
    }
}

and in my .xml this was specified:

aop:aspectj-autoproxy

What more should I add? What to do next?

Check below things.

1) Check if OrgManagerImpl is either defind as bean in context xml or it is marked as @Component & in context xml you have or that classes's package.

2) If above thing is correct then try changing pointcut as below

@Pointcut("execution(* get*(..))")

This pointcut intercepts all get methods. See if with this point cut your synchronize method is working or not. If it works then at least your spring configurations are fine. you just need to refine pointcut expression. But if this also doesn't work then there is something wrong with your spring aop configuration itself, so we can concentrate on those.

Also if this doesn't work then try to give some more info like you context xml, bean java classes etc.

Two things you need to check.

  1. aop:aspectj-autoproxy is enabled in configuration
  2. The Point Cut / Aspect / Target are spring bean

The below is my xml config example.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy/>
    <context:component-scan base-package="com.techoffice"/>

    <bean class="com.techoffice.example.ExampleAspect"/>

</beans>

ExampleAspect.java

package com.techoffice.example;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class ExampleAspect {

    @Pointcut("execution (* com.techoffice..*(..))")        
    public void anyRun() {}

    @Before(value="anyRun()")
    public void beforeAnyRun(JoinPoint jointPoint){
        System.out.println("Before " + jointPoint.getClass().getName() + "." + jointPoint.getSignature().getName());
    }

    @After(value="anyRun()")
    public void afterAnyRun(JoinPoint jointPoint){
        System.out.println("After " + jointPoint.getClass().getName() + "." + jointPoint.getSignature().getName());
    }
}

HelloWorldExample

package com.techoffice.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class HelloWorldExample {

    public void run(){
        System.out.println("run");
    }

    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        HelloWorldExample helloWorldExample = context.getBean(HelloWorldExample.class);
        helloWorldExample.run();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM