配置文件有配置一个切面:
<aop:config>
<aop:aspect ref="mindReader">
<aop:pointcut id="thinking"
expression="execution(* com.lm.springIdol.Audience.thinkingSomething(String)) and args(thoughts)" />
<aop:before pointcut-ref="thinking" method="readThoughts"
arg-names="thoughts" />
</aop:aspect>
</aop:config>
MindReader类:
public class MindReader implements Performer{
private String thoughts;
public void readThoughts(String thoughts){
this.thoughts=thoughts;
System.out.println(thoughts);
}
public void setThoughts(String thoughts){
this.thoughts=thoughts;
}
public String getThoughts(){
return this.thoughts;
}
@Override
public void perform() {
// TODO Auto-generated method stub
System.out.println(getThoughts());
}
}
Audience类:
public class Audience{
private String thoughts;
public void takeSeat(){
System.out.println("audience take seat");
}
public void turnOffPhone(){
System.out.println("please turn off phone");
}
public void applaud(){
System.out.println("audience are applauding");
}
public void thinkingSomething(String thoughts){
this.thoughts=thoughts;
}
public void setThought(String thoughts){
this.thoughts=thoughts;
}
public String getThoughts(){
return thoughts;
}
}
测试的main函数:
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("springIdol.xml");
Audience audience=(Audience)context.getBean("audience");
audience.thinkingSomething("I love you");
}
不会报错,但是不会显示“I love you”
------解决思路----------------------
那不应该呀, 我按你的配置自己试了一下,可以输出