2

我想在调用另一个类的特定方法时触发一个方法,这就是我想到使用@Pointcut 的原因。

下面的代码几乎与我正在编码的代码相同,我不需要添加其他内容。

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

这是应该被触发的类:

@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.
    }
}

在我的 .xml 中,这是指定的:

aop:aspectj-autoproxy

我还应该补充什么?接下来做什么?

4

2 回答 2

0

检查以下内容。

1)检查 OrgManagerImpl 是否在上下文 xml 中定义为 bean,或者在您拥有的上下文 xml 或该类的包中标记为 @Component &。

2)如果上面的事情是正确的,那么尝试改变切入点如下

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

此切入点拦截所有 get 方法。看看这点 cut 你的同步方法是否有效。如果它有效,那么至少你的弹簧配置是好的。你只需要细化切入点表达式。但是,如果这也不起作用,那么您的 spring aop 配置本身就有问题,所以我们可以专注于这些。

此外,如果这不起作用,请尝试提供更多信息,例如上下文 xml、bean java 类等。

于 2012-10-23T09:17:13.583 回答
0

你需要检查两件事。

  1. aop:aspectj-autoproxy 在配置中启用
  2. Point Cut / Aspect / Target 是 spring bean

下面是我的 xml 配置示例。

<?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());
    }
}

HelloWorld 示例

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();
    }
}
于 2017-11-19T09:48:16.410 回答