基本 spring 应用程序中的 @Aspect

@Aspect in a basic spring application

我有一个简单的 spring 应用程序,其中包含一些 bean 和一个方面。我正在使用 Java-config。我尝试 运行 IntelliJ 中的项目。

代码编译无误,但未调用@Before- 和@After-advices。我在这里阅读了一些问题和答案,但我找不到错误。有什么建议..?

mvn dependency:tree 显示:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ lektion07 ---
[INFO] de.steve72.spring:lektion07:jar:1.0.0
[INFO] +- org.springframework:spring-aop:jar:4.3.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.3.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:4.3.1.RELEASE:compile
[INFO] |     \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] +- org.aspectj:aspectjrt:jar:1.5.4:compile
[INFO] +- org.springframework:spring-context:jar:4.3.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-expression:jar:4.3.1.RELEASE:compile
[INFO] +- junit:junit:jar:4.12:compile
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] \- org.mockito:mockito-core:jar:1.10.19:compile
[INFO]    \- org.objenesis:objenesis:jar:2.1:runtime
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

@Aspect-注释class:

...

@Aspect
@Component
public class Demo {

    @Pointcut("execution(* de.steve72.spring.lektion07.springidol.Performer.perform(..))")
    public void sayHalloAspect() {}

    @Before("sayHalloAspect()")
    public void takeSeats(){

        System.out.println("the audience is taking their seats");
    }

    @After("sayHalloAspect()")
    public void jump() {

        System.out.println("the audience is going crazy");
    }
}

表演者界面:

...

public interface Performer {

    void perform();
}

杂耍人class:

...

@Component
public class Juggler implements Performer {

    private final int BEANBAGCOUNT = 3;

    private int beanBag = BEANBAGCOUNT;


    public Juggler() {
    }

    public Juggler(int beanBag) {

        this.beanBag = beanBag;
    }

    @Override
    public void perform() {

        System.out.println("juggling with " + beanBag + " beanbags");
    }
}

主要class:

public class MainAppLektion07 {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

        ctx.register(AppConfig.class);
        ctx.register(PerformerConfig.class);

        ctx.refresh();

        Performer juggler = (Performer)ctx.getBean("getJuggler");
        juggler.perform();
    }
}

应用程序配置:

@Configuration
@EnableAspectJAutoProxy
     public class AppConfig {
}

表演者配置:

@Configuration
public class PerformerConfig {

    @Bean
    public Performer getJuggler() {

        return new Juggler();
    }

    @Bean
    public Performer getInstrumentalist() {

        return new Instrumentalist();
    }
}

我忘了 spring-aop.xml:

<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">

    <context:component-scan base-package="de.steve72.spring.lektion07"/>

    <aop:aspectj-autoproxy/>

    <bean id="demoAspect" class="de.steve72.spring.lektion07.aspects.Demo"/>

</beans>

似乎您还没有将 spring-aop.xml 和 PerformerConfig 中的 beans 导入到 appconfig。

由于您已经在 appconfig 中启用了@EnableAspectJAutoProxy。只需在 appconfig 中启用 @ComponentScan("de.steve72.spring.lektion07")。并且 删除 spring-aop.xml。同时在 appconfig 中导入你的 @Import(PerformerConfig.class)