spring aop的问题

ws_lm 2014-09-23 08:58:33
配置文件有配置一个切面:

<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”
...全文
146 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 11 楼 whos2002110 的回复:
引用 10 楼 ws_lm 的回复:
[quote=引用 9 楼 ws_lm 的回复:] [quote=引用 7 楼 whos2002110 的回复:] [quote=引用 5 楼 whos2002110 的回复:] [quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="mindReader" class="xxx.xxx.xxx.MindReader" />
	<bean id="audience" class="xxx.xxx.xxx.Audience" />
	<aop:config>
		<aop:aspect ref="mindReader">
			<aop:pointcut id="thinking"
				expression="execution(* xxx.xxx.xxx.Audience.thinkingSomething(String)) and args(thoughts)" />
			<aop:before pointcut-ref="thinking" method="readThoughts"
				arg-names="thoughts" />
		</aop:aspect>
	</aop:config>
</beans>
[/quote]我发现问题了,是我定义了两个切面,是只能有一个切面?[/quote]<aop:config proxy-target-class="true"> <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:aspect ref="audience"> <aop:pointcut expression="execution(* com.lm.springIdol.Performer.perform(..))" id="perform" /> <aop:before pointcut-ref="perform" method="takeSeat" /> <aop:before pointcut-ref="perform" method="turnOffPhone" /> <aop:after-returning pointcut-ref="perform" method="applaud" /> </aop:aspect> </aop:config> 这是两个切面的配置[/quote] audience 不是上个被mindReader 切入的bean么? <aop:aspect ref="audience"> 你这样是又要把它当切面切入到另外的bean? 感觉很怪, 不过你没报错应该是可以的吧, 我没这样弄过[/quote]可是两个放在一起前一个切面完全没有作用,得不到“I love you”,好纠结
whos2002110 2014-09-23
  • 打赏
  • 举报
回复
引用 10 楼 ws_lm 的回复:
引用 9 楼 ws_lm 的回复:
[quote=引用 7 楼 whos2002110 的回复:] [quote=引用 5 楼 whos2002110 的回复:] [quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="mindReader" class="xxx.xxx.xxx.MindReader" />
	<bean id="audience" class="xxx.xxx.xxx.Audience" />
	<aop:config>
		<aop:aspect ref="mindReader">
			<aop:pointcut id="thinking"
				expression="execution(* xxx.xxx.xxx.Audience.thinkingSomething(String)) and args(thoughts)" />
			<aop:before pointcut-ref="thinking" method="readThoughts"
				arg-names="thoughts" />
		</aop:aspect>
	</aop:config>
</beans>
[/quote]我发现问题了,是我定义了两个切面,是只能有一个切面?[/quote]<aop:config proxy-target-class="true"> <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:aspect ref="audience"> <aop:pointcut expression="execution(* com.lm.springIdol.Performer.perform(..))" id="perform" /> <aop:before pointcut-ref="perform" method="takeSeat" /> <aop:before pointcut-ref="perform" method="turnOffPhone" /> <aop:after-returning pointcut-ref="perform" method="applaud" /> </aop:aspect> </aop:config> 这是两个切面的配置[/quote] audience 不是上个被mindReader 切入的bean么? <aop:aspect ref="audience"> 你这样是又要把它当切面切入到另外的bean? 感觉很怪, 不过你没报错应该是可以的吧, 我没这样弄过
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 9 楼 ws_lm 的回复:
引用 7 楼 whos2002110 的回复:
[quote=引用 5 楼 whos2002110 的回复:] [quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="mindReader" class="xxx.xxx.xxx.MindReader" />
	<bean id="audience" class="xxx.xxx.xxx.Audience" />
	<aop:config>
		<aop:aspect ref="mindReader">
			<aop:pointcut id="thinking"
				expression="execution(* xxx.xxx.xxx.Audience.thinkingSomething(String)) and args(thoughts)" />
			<aop:before pointcut-ref="thinking" method="readThoughts"
				arg-names="thoughts" />
		</aop:aspect>
	</aop:config>
</beans>
[/quote]我发现问题了,是我定义了两个切面,是只能有一个切面?[/quote]<aop:config proxy-target-class="true"> <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:aspect ref="audience"> <aop:pointcut expression="execution(* com.lm.springIdol.Performer.perform(..))" id="perform" /> <aop:before pointcut-ref="perform" method="takeSeat" /> <aop:before pointcut-ref="perform" method="turnOffPhone" /> <aop:after-returning pointcut-ref="perform" method="applaud" /> </aop:aspect> </aop:config> 这是两个切面的配置
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 7 楼 whos2002110 的回复:
引用 5 楼 whos2002110 的回复:
[quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]

<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="mindReader" class="xxx.xxx.xxx.MindReader" />
	<bean id="audience" class="xxx.xxx.xxx.Audience" />
	<aop:config>
		<aop:aspect ref="mindReader">
			<aop:pointcut id="thinking"
				expression="execution(* xxx.xxx.xxx.Audience.thinkingSomething(String)) and args(thoughts)" />
			<aop:before pointcut-ref="thinking" method="readThoughts"
				arg-names="thoughts" />
		</aop:aspect>
	</aop:config>
</beans>
[/quote]我发现问题了,是我定义了两个切面,是只能有一个切面?
  • 打赏
  • 举报
回复
你输出不就有了么?
whos2002110 2014-09-23
  • 打赏
  • 举报
回复
引用 5 楼 whos2002110 的回复:
引用 3 楼 ws_lm 的回复:
[quote=引用 1 楼 whos2002110 的回复:]
看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢


那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]




<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean id="mindReader" class="xxx.xxx.xxx.MindReader" />
<bean id="audience" class="xxx.xxx.xxx.Audience" />
<aop:config>
<aop:aspect ref="mindReader">
<aop:pointcut id="thinking"
expression="execution(* xxx.xxx.xxx.Audience.thinkingSomething(String)) and args(thoughts)" />
<aop:before pointcut-ref="thinking" method="readThoughts"
arg-names="thoughts" />
</aop:aspect>
</aop:config>
</beans>
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 5 楼 whos2002110 的回复:
引用 3 楼 ws_lm 的回复:
[quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出[/quote]我发个源文件给你,帮我看看,能不能加个qq,我的是1551830060
whos2002110 2014-09-23
  • 打赏
  • 举报
回复
引用 3 楼 ws_lm 的回复:
引用 1 楼 whos2002110 的回复:
看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
那不应该呀, 我按你的配置自己试了一下,可以输出
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 2 楼 humanity 的回复:
那你期望的结果是什么? 代码终究要执行的,因此,你只需要添加行断点,然后在 debug 模式下运行你的程序就知道执行的流程了。 我了解一些 AspectJ AOP,但不是很明白这里面的 method="readThoughts" 是什么意思,如果是指在命中这个 pointcut 时执行 readThoughts 方法的话,那需要知道是哪个类和对象实例的 readThoughts 方法,如果说这个是指当前 pointcut 所对应的对象实例的话, 那么 Audience 类中并没有 readThoughts 方法,而如果是说这个 pointcut 被命中执行时是在 readThoughts 的控制层次之中,那么从你的源代码中看不出你调用过 readThoughts 方法。 另外这个 Performer 接口有什么特别之外么(也没发现你的 MindReader 有什么特别的 annotations)啊?
readThoughts是Audience里的一个方法,perform不用管,这里没有用到。我的目的是,Audience对象调用thinkingSomething(String thoughts)方法后,把thoughts传入MindReader对象的readThoughts的参数,然后输出thoughs.
ws_lm 2014-09-23
  • 打赏
  • 举报
回复
引用 1 楼 whos2002110 的回复:
看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢
humanity 2014-09-23
  • 打赏
  • 举报
回复
那你期望的结果是什么? 代码终究要执行的,因此,你只需要添加行断点,然后在 debug 模式下运行你的程序就知道执行的流程了。 我了解一些 AspectJ AOP,但不是很明白这里面的 method="readThoughts" 是什么意思,如果是指在命中这个 pointcut 时执行 readThoughts 方法的话,那需要知道是哪个类和对象实例的 readThoughts 方法,如果说这个是指当前 pointcut 所对应的对象实例的话, 那么 Audience 类中并没有 readThoughts 方法,而如果是说这个 pointcut 被命中执行时是在 readThoughts 的控制层次之中,那么从你的源代码中看不出你调用过 readThoughts 方法。 另外这个 Performer 接口有什么特别之外么(也没发现你的 MindReader 有什么特别的 annotations)啊?
whos2002110 2014-09-23
  • 打赏
  • 举报
回复
看上去没错, mindReader、audience 这两个bean都配置了吧

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧