为什么Advices方法没有接到电话?

时间:2016-07-23 06:48:51

标签: spring spring-aop

Spring 4 + AspectJ

嗨伙计,

我是春天和AOP的新手,我只是通过亲自动手学习。在这里,我想要将观众类中的跨领域问题外部化。但是在执行测试类时,我只是在没有交叉关注方法的情况下获得输出。

我无法弄清楚为什么这些方法没有执行,请任何人都可以查看代码。感谢。

输出

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
CircusPerformar is performing

这是一个执行一个方法的接口。

1)Performer.java

package concert;

public interface Performer {
    void perform();
}

2)CircusPerformar.java

package concert;

import org.springframework.stereotype.Component;

    @Component
    public class CircusPerformar implements Performer{

        @Override
        public void perform() {
            System.out.println("CircusPerformar is performing");
        }
    }

3)PerformanceConfig.java

package concert;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan
    public class PerformanceConfig {

    }

5)Audience.java

package concert;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class Audience {

    @Before("execution(** concert.Performance.perform(..))")
    public void silenceCellPhones(){
        System.out.println("Silencing the phone");
    }

    @Before("execution(** concert.Performance.perform(..)")
    public void takeSeats(){
        System.out.println("Taking seat");
    }

    @AfterReturning("execution(** concert.Performance.perform(..))")
    public void applause(){
        System.out.println("CLAP CLAP CLAP CLAP!!!!");
    }

    @AfterThrowing("execution(** concert.Performance.perform(..)")
    public void demandMoney(){
        System.out.println("Demanding a refund");
    }

}

6)PerformanceTest.java

package concert;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class PerformanceTest {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext  ctx = new AnnotationConfigApplicationContext();
        ctx.register(PerformanceConfig.class);
        ctx.refresh();
        Performer cp = (CircusPerformar) ctx.getBean("circusPerformar");
        cp.perform();
    }
}

0 个答案:

没有答案