Spring AOP:&&和'and'之间的差异



我有这个接口:

public interface Performance {
    public void perform();
}

该类实施:

@Component
public class PerformanceImplementation implements Performance {
    public void perform() {
        System.out.println("PerformanceImplementation.perform()");
    }
}

我想将以下方面应用于上面的接口方法,但只有在另一个软件包中执行该方法,因此我使用该软件包名称

使用within指定器
@Aspect
public class Audience {
    @Pointcut("execution(** concert.Performance.perform(..)) " +
                "and within(asdasdasd.*)")
    public void performance() {}

    @Around("performance()")
    public void watchPerformance(ProceedingJoinPoint jp) {
        try {
            System.out.println("Silencing cell phones");
            System.out.println("Taking seats");
            jp.proceed();
            System.out.println("CLAP CLAP CLAP!!!");
        } catch (Throwable e) {
            System.out.println("Demanding a refund");
        } }
}

如果我使用and而不是&&,则该方面无论如何都会触发,并且似乎没有考虑指定器的限制。

有什么原因发生的?

取决于您是在注释中还是在XML中使用它们。

可以使用'&&','||'组合点曲表达式。和'!'

@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}

组合点尺度子表达式'&''在XML文档中很尴尬,因此可以使用关键字'和',',''''''',可以用代替"&&','||'和 '!'分别。例如,上一个点曲线可以更好地写为:

<aop:pointcut id="businessService"
        expression="execution(* com.xyz.myapp.service.*.*(..)) **and** this(service)"/>

最新更新