当前位置: 代码迷 >> J2EE >> spring aop的有关问题
  详细解决方案

spring aop的有关问题

热度:234   发布时间:2016-04-17 23:36:17.0
spring aop的问题
配置文件有配置一个切面:

<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”
------解决思路----------------------
引用:
Quote: 引用:

看上去没错,  mindReader、audience 这两个bean都配置了吧
都有配置了呢


那不应该呀, 我按你的配置自己试了一下,可以输出
  相关解决方案