0

I've a simple spring application with some beans and one aspect. I'm using Java-config. I've tried to run the project within IntelliJ.

The code compiles without error but the @Before- and @After-advices are not called. I read some questions and answers here, but I was not able to find the error. Any suggestions ..?

mvn dependency:tree shows:

[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-annotated 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");
    }
}

The Performer interface:

...

public interface Performer {

    void perform();
}

The Juggler 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");
    }
}

The main 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();
    }
}

The application config:

@Configuration
@EnableAspectJAutoProxy
     public class AppConfig {
}

The performer config:

@Configuration
public class PerformerConfig {

    @Bean
    public Performer getJuggler() {

        return new Juggler();
    }

    @Bean
    public Performer getInstrumentalist() {

        return new Instrumentalist();
    }
}

I forgot the 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>
Steve
  • 7
  • 3

1 Answers1

0

Seems you have not imported your beans in spring-aop.xml and PerformerConfig to appconfig.

Since you have already enabled @EnableAspectJAutoProxy in appconfig. Just enable the @ComponentScan("de.steve72.spring.lektion07") in appconfig. And remove the spring-aop.xml. Also import your @Import(PerformerConfig.class) in appconfig.

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71