原始逻辑类
public class Audience {
public Audience(){};
public void takeSeats(){
System.out.println("The audience is taking their seats");
}
public void turnOffCellPhones(){
System.out.println("The audience is turning off theri cellphones");
}
public void applaud(){
System.out.println("CLAP CLAP CLAP ClAp");
}
public void demandRefund(){
System.out.println("Boo! We want our money back");
}
//实现基本的观众提示
}
//实现 MethodBeforeAdvice,AfterReturningAdvice,ThrowsAdvice
//throwsAdvice 方法没出来
package aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class AudienceAdvice implements MethodBeforeAdvice,
AfterReturningAdvice, ThrowsAdvice {
private Audience audience;
public AudienceAdvice() {
}
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("ok");
audience.takeSeats();
audience.turnOffCellPhones();
}
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
System.out.println("run after");
audience.applaud();
}
public Audience getAudience() {
return audience;
}
public void setAudience(Audience audience) {
this.audience = audience;
}
}
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
以上是小弟写的简单的aop,
问我在perform未加载通知,及没打印代理的字
是我使用 错误还是我配置错误,求大仙指导一二,助小弟脱离苦海
[quote]
以上是小弟写的简单的aop,
问我在perform未加载通知,及没打印代理的字
是我使用 错误还是我配置错误,求大仙指导一二,助小弟脱离苦海
[/quote]
原因在于Spring的AOP是通过代理来实现的,无论是动态代理还是CGLIB代理。所以你除了需要定义目标对象(audiece)、切面逻辑(audieceAdvice),还需要通过代理工厂(ProxyFactoryBean或者BeanNameAutoProxyCreator)把这两者结合起来,通过使用代理对象达到把切面逻辑织入进去。
你可以在配置文件后面加上这个,比如:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="audiece" class="spring.Audience"></bean>
<bean id="audieceAdvice" class="spring.AudienceAdvice">
<property name="audience" ref="audiece"/>
</bean>
<bean id="audienceAdcisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="audieceAdvice" />
<property name="pattern" value="spring.Audience.*.*" />
</bean>
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指定目标对象,目标对象是PrinterImpl对象 -->
<property name="target" ref="audiece" />
<!-- 该目标中加入拦截器pointcutAdvisor -->
<property name="interceptorNames">
<list>
<value>audienceAdcisor</value>
</list>
</property>
</bean>
[/code]
注意,你原来的“pattern”属性设置也是有问题的,应该设置为你需要应用aop的类的具体方面,包括类的全限定名,可以使用通配符。
另外,这样配置成功后,运行依然有问题,问题在于你的AudienceAdvice 实现了ThrowsAdvice接口,虽然这个接口是个标记接口,但你必须实现形如:
[code="java"]
void afterThrowing([Method, args, target], ThrowableSubclass);
[/code]
的方法签名,具体说明请参考ThrowsAdvice的javadoc
parrten写错了吧,为什么不用aspectj呢