让我先foremention,我面临的问题是与
interceptThoughts(String thoughts)
方法,从第一个代码块,不打印为什么这个Spring Aspect不能像方法参数那样打印?
我从春季行动中运行教程。有一个Magician
类implements MindReader
接口方法interceptThoughts(String thoughts)
和getThoughts()
@Aspect
public class Magician implements MindReader {
private String thoughts;
@Pointcut("execution(* com.underdogdevs.myspringaspectj."
+ "Thinker.thinkOfSomething(String)) && args(thoughts)")
public void thinking(String thoughts) {
}
@Override
@Before("thinking(thoughts)")
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts : " + thoughts);
this.thoughts = thoughts;
}
@Override
public String getThoughts() {
return thoughts;
}
}
的方面是应该读Volunteer
是implements Thinker
接口的头脑,用方法thinkOfSomething(String thoughts)
public class Volunteer implements Thinker {
private String thoughts;
@Override
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts;
System.out.println("Something");
}
public String getThoughts() {
return thoughts;
}
}
我有我的Java BeanConfig
与Magician
和Volunteer
@Configuration
public class BeanConfig {
@Bean
public MindReader magician() {
return new Magician();
}
@Bean
public Thinker volunteer() {
return new Volunteer();
}
}
而我试图运行它来获得Magician
方法打印线的interceptThoughts
方法
public class App {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("spring-idol.xml");
System.out.println();
Thinker volunteer = (Thinker)context.getBean("volunteer");
volunteer.thinkOfSomething("This is what I'm thinking");
}
}
- NO Eorrrs
- 没有例外
- 包在
@Pointcut(execution(
的Magician
方面是正确的 我有这两个项目在我的Spring配置XML
<context:component-scan base-package="com.underdogdevs.myspringaspectj" /> <aop:aspectj-autoproxy />
的问题是从
Magician
方面@Before
不打印,因为它应该。 我在这里错过了什么吗?为什么不打印?我有其他方面的方法,没有任何参数,运行得很好。我没有正确传递参数值吗?
这可能是疯狂的猜测,因为你的方面对我来说似乎是正确的;你是否尝试将志愿者和魔术师注释为@ @ Component?或者将切入点放在@Before注释中? –
!guido我刚刚尝试过,但它不起作用。我想这就是_this_' '用于在上下文中扫描bean的情况,而不是真正指定'@ Component' –
,实际上'context:component-scan'是for扫描类*以搜索* @Component注释(以及@Service,@Controller,...还有@Configuration,它是用@Component进行元注释的) –