Java 执行了Spring AOP表达式,但它不应';T
我是春天的新手,试图理解AOP。这是我得到的 我有一个简单的方面,我希望在调用任何非getter方法时运行它Java 执行了Spring AOP表达式,但它不应';T,java,spring,spring-aop,Java,Spring,Spring Aop,我是春天的新手,试图理解AOP。这是我得到的 我有一个简单的方面,我希望在调用任何非getter方法时运行它 @Aspect @Component public class LoggingAspect { @Pointcut("execution(* org.practice.entity.Person.get*())") private void getter() {} @Before("!getter()") public void noGetter()
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* org.practice.entity.Person.get*())")
private void getter() {}
@Before("!getter()")
public void noGetter() {
System.out.println("NO GETTER GETS CALLED");
}
}
Person类只是
@Component
public class Person {
public void getPerson() {
System.out.println("GETTING PERSON....");
}
}
我正在使用Java注释初始化配置
@Configuration
@EnableAspectJAutoProxy
@ComponentScan("org.practice")
public class DemoConfig {}
然后在我的主要方法中
public class MyApp {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
context.close();
}
}
正如您所看到的,我只是创建一个上下文并关闭它,而不是调用任何getter或non-getter方法。当我运行程序时,我得到以下控制台输出
NO GETTER GETS CALLED....
这是有道理的,因为我没有调用任何getter方法,但我希望只有当我显式调用任何非getter时(而不仅仅是打开上下文)才会执行此方面。请让我知道,如果我希望我的业务逻辑仅在调用任何非getter方法时执行,那么我将如何执行
谢谢试试这个:
@Pointcut("execution(* org.practice.entity.Person.*())")
private void methodCall() {}
@Before("!getter() && methodCall")
public void noGetter() {
System.out.println("NO GETTER GETS CALLED");
}
试试这个:
@Pointcut("execution(* org.practice.entity.Person.*())")
private void methodCall() {}
@Before("!getter() && methodCall")
public void noGetter() {
System.out.println("NO GETTER GETS CALLED");
}
我认为它发生在应用程序上下文加载时初始化PersonBean的过程中
由于您已将joinpoint指定为非getter,因此在默认构造执行时(由编译器提供),会触发通知我认为它发生在加载od应用程序上下文时初始化Person bean的过程中 由于您已将joinpoint指定为非getter,所以在执行默认构造时(由编译器提供),会触发通知