67,515
社区成员
发帖
与我相关
我的任务
分享
<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>
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());
}
}
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;
}
}
<?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”,好纠结都有配置了呢 [quote=引用 7 楼 whos2002110 的回复:] [quote=引用 5 楼 whos2002110 的回复:] [quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
<?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=引用 5 楼 whos2002110 的回复:] [quote=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
<?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=引用 3 楼 ws_lm 的回复:] [quote=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
<?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=引用 1 楼 whos2002110 的回复:]
看上去没错, mindReader、audience 这两个bean都配置了吧
<?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=引用 1 楼 whos2002110 的回复:] 看上去没错, mindReader、audience 这两个bean都配置了吧
都有配置了呢 看上去没错, mindReader、audience 这两个bean都配置了吧
那你期望的结果是什么? 代码终究要执行的,因此,你只需要添加行断点,然后在 debug 模式下运行你的程序就知道执行的流程了。 我了解一些 AspectJ AOP,但不是很明白这里面的 method="readThoughts" 是什么意思,如果是指在命中这个 pointcut 时执行 readThoughts 方法的话,那需要知道是哪个类和对象实例的 readThoughts 方法,如果说这个是指当前 pointcut 所对应的对象实例的话, 那么 Audience 类中并没有 readThoughts 方法,而如果是说这个 pointcut 被命中执行时是在 readThoughts 的控制层次之中,那么从你的源代码中看不出你调用过 readThoughts 方法。 另外这个 Performer 接口有什么特别之外么(也没发现你的 MindReader 有什么特别的 annotations)啊?
看上去没错, mindReader、audience 这两个bean都配置了吧