我有一个简单的spring应用程序,包含一些bean和一个方面。我正在使用Java-config。我试图在IntelliJ中运行该项目。
代码编译没有错误,但未调用@ Before-和@ After-advice。我在这里阅读了一些问题和答案,但我无法找到错误。有什么建议..?
mvn依赖:树显示:
[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");
}
}
Performer界面:
...
public interface Performer {
void perform();
}
Juggler班:
...
@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");
}
}
主要班级:
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>
答案 0 :(得分:0)
似乎你没有在spring-aop.xml和PerformerConfig中将你的bean导入appconfig。
由于您已在appconfig中启用了@EnableAspectJAutoProxy。只需在appconfig中启用 @ComponentScan(&#34; de.steve72.spring.lektion07&#34;)即可。并且删除spring-aop.xml 。还要在appconfig中导入 @Import(PerformerConfig.class)。