spring aop的问题

J2EE 码拜 9年前 (2015-04-11) 882次浏览 0个评论

配置文件有配置一个切面:

<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”

spring aop的问题
看上去没错,  mindReader、audience 这两个bean都配置了吧
spring aop的问题
那你期望的结果是什么?
代码终究要执行的,因此,你只需要添加行断点,然后在 debug  模式下运行你的程序就知道执行的流程了。

我了解一些 AspectJ AOP,但不是很明白这里面的 method=”readThoughts” 是什么意思,如果是指在命中这个 pointcut 时执行 readThoughts 方法的话,那需要知道是哪个类和对象实例的 readThoughts 方法,如果说这个是指当前 pointcut 所对应的对象实例的话, 那么 Audience 类中并没有 readThoughts 方法,而如果是说这个 pointcut 被命中执行时是在 readThoughts 的控制层次之中,那么从你的源代码中看不出你调用过 readThoughts 方法。

另外这个 Performer 接口有什么特别之外么(也没发现你的 MindReader 有什么特别的 annotations)啊?

spring aop的问题
引用 1 楼 whos2002110 的回复:

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

都有配置了呢

spring aop的问题
引用 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.

spring aop的问题
60分
引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题
引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

我发个源文件给你,帮我看看,能不能加个qq,我的是1551830060

spring aop的问题
引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题

<?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>
spring aop的问题
你输出不就有了么?
spring aop的问题
引用 7 楼 whos2002110 的回复:
Quote: 引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题

<?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>

我发现问题了,是我定义了两个切面,是只能有一个切面?

spring aop的问题
引用 9 楼 ws_lm 的回复:
Quote: 引用 7 楼 whos2002110 的回复:
Quote: 引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题

<?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>

我发现问题了,是我定义了两个切面,是只能有一个切面?

<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>
这是两个切面的配置

spring aop的问题
引用 10 楼 ws_lm 的回复:
Quote: 引用 9 楼 ws_lm 的回复:
Quote: 引用 7 楼 whos2002110 的回复:
Quote: 引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题

<?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>

我发现问题了,是我定义了两个切面,是只能有一个切面?

<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>
这是两个切面的配置

audience 不是上个被mindReader 切入的bean么? 
<aop:aspect ref=”audience”>   你这样是又要把它当切面切入到另外的bean?
感觉很怪, 不过你没报错应该是可以的吧, 我没这样弄过

spring aop的问题
引用 11 楼 whos2002110 的回复:
Quote: 引用 10 楼 ws_lm 的回复:
Quote: 引用 9 楼 ws_lm 的回复:
Quote: 引用 7 楼 whos2002110 的回复:
Quote: 引用 5 楼 whos2002110 的回复:
Quote: 引用 3 楼 ws_lm 的回复:
Quote: 引用 1 楼 whos2002110 的回复:

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

都有配置了呢

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

spring aop的问题

<?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>

我发现问题了,是我定义了两个切面,是只能有一个切面?

<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>
这是两个切面的配置

audience 不是上个被mindReader 切入的bean么? 
<aop:aspect ref=”audience”>   你这样是又要把它当切面切入到另外的bean?
感觉很怪, 不过你没报错应该是可以的吧, 我没这样弄过

可是两个放在一起前一个切面完全没有作用,得不到“I love you”,好纠结


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明spring aop的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!