Spring AOP 表达式已执行但不应该执行
Spring AOP expression executed but it shouldn't
我是 Spring 的新手,正在尝试了解 AOP。这是我得到的
我有一个简单的方面,我想在任何非getter方法被调用时运行
@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");
}
}
人class简直就是
@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 或非 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");
}
我认为它发生在加载应用程序上下文时初始化 Person bean 期间
因为你给的连接点不是 getter 所以在默认构造执行时(由编译器提供)建议被触发
我是 Spring 的新手,正在尝试了解 AOP。这是我得到的
我有一个简单的方面,我想在任何非getter方法被调用时运行
@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");
}
}
人class简直就是
@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 或非 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");
}
我认为它发生在加载应用程序上下文时初始化 Person bean 期间 因为你给的连接点不是 getter 所以在默认构造执行时(由编译器提供)建议被触发