// Launcher.java
import com.rippleworks.spring.Macbeth;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Launcher {
public static void main(String... args){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ProjectConfig.class);
Macbeth drama = context.getBean(Macbeth.class);
drama.perform();
context.close();
}
}
// Macbeth.java
package com.rippleworks.spring;
import com.rippleworks.spring.interfaces.Performance;
import org.springframework.stereotype.Component;
@Component
public class Macbeth implements Performance {
@Override
public void perform() {
System.out.println("Macbeth playing..");
}
}
// Audience.java
package com.rippleworks.spring;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
@Pointcut("execution(* com.rippleworks.spring.Macbeth.perform(..))")
public void performance(){}
@Before("performance()")
public void silentCellphones() {
System.out.println("Cell phones are now silent.");
}
}
// ProjectConfig.java
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.rippleworks.spring")
public class ProjectConfig {
// If I comment out this method, I wont get a NotSuchBeanDefExp
// for class Macbeth. Weird :-(
@Bean
public Audience audience() {
return new Audience();
}
}
您好,Iam在弹簧中使用AspectJ Style AOP时遇到问题(IntelliJ IDEA Ultimate Version)。不使用方面时,输出是预期的。 但是当我将Aspect类Audience声明为Bean时,我得到了Macbeth类的NoSuchBeanDefinitionException。
2017年8月27日上午9:38:39 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO:刷新 org.springframework.context.annotation.AnnotationConfigApplicationContext@799f7e29: 启动日期[Sun Aug 27 09:38:39 IST 2017];上下文层次结构的根 线程“main”中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 “com.rippleworks.spring.Macbeth”类型的限定bean可用于 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) 在Launcher.main(Launcher.java:10)
感谢您的时间。
答案 0 :(得分:0)
尝试直接在建议类型上提供切入点。您可以直接在@before中使用切入点表达式进行检查。
@Before("execution(* com.rippleworks.spring.Macbeth.perform(..))")
public void silentCellphones() {
System.out.println("Cell phones are now silent.");
}