1、接口类
2、实现类
3、advice ,方法名不限,JoinPoint可有可无。ProceedingJoinPoint 必须有 而且必须有返回值
3、配置文件
或者pointcut重复利用。pointcut包括表达式和签名两部分。其中execution(表达式)格式为execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern? )。其中参数中(*)表示一个参数,类型为任务,(..)表示零到多个参数。[b]..[/b]表示子包
如果pointcunt元素定义为cn.com.gan.spring.aop.Hello.*(..)),应该是么有相应的类和方法被代理,但是为什么会报错。测试了下 方法名可以不存在 但是如果相应的类不存在的话会报错。
package cn.com.gan.spring.aop;
public interface IHello {
public void SayHello();
public void sayHelloAgain();
}
2、实现类
package cn.com.gan.spring.aop;
public class ImpHello implements IHello {
@Override
public void SayHello() {
System.out.println("hello everybody!");
}
@Override
public void sayHelloAgain(){
System.out.println("hello everybody again");
}
}
3、advice ,方法名不限,JoinPoint可有可无。ProceedingJoinPoint 必须有 而且必须有返回值
package cn.com.gan.spring.aop;
package cn.com.gan.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class XmlLoggerBefore {
public void before(JoinPoint jointPoint) {
System.out.println("开始记录:"
+ jointPoint.getSignature().getDeclaringTypeName() + "."
+ jointPoint.getSignature().getName());
}
public void after(JoinPoint jointPoint) {
System.out.println("记录结束:"
+ jointPoint.getSignature().getDeclaringTypeName() + "."
+ jointPoint.getSignature().getName());
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around开始记录:"
+ pjp.getSignature().getDeclaringTypeName() + "."
+ pjp.getSignature().getName());
Object rtv=pjp.proceed();
System.out.println("around开始记录记录结束:"
+ pjp.getSignature().getDeclaringTypeName() + "."
+ pjp.getSignature().getName());
return rtv;
}
}
3、配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id="hello" class="cn.com.gan.spring.aop.ImpHello"></bean>
<bean id="before" class="cn.com.gan.spring.aop.XmlLoggerBefore"></bean>
<aop:config>
<aop:aspect id="loggerbefore" ref="before">
<aop:before pointcut="execution(* cn.com.gan.spring.aop.IHello.*(..))"
method="before"/>
</aop:aspect>
</aop:config>
</beans>
或者pointcut重复利用。pointcut包括表达式和签名两部分。其中execution(表达式)格式为execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern? )。其中参数中(*)表示一个参数,类型为任务,(..)表示零到多个参数。[b]..[/b]表示子包
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<bean id="hello" class="cn.com.gan.spring.aop.ImpHello"></bean>
<bean id="before" class="cn.com.gan.spring.aop.XmlLoggerBefore"></bean>
<aop:config>
<aop:aspect id="loggerbefore" ref="before">
<aop:pointcut id="beforHello"
expression="execution( * cn.com.gan.spring.aop.IHello.*(..))" />
<aop:before pointcut-ref="beforHello" method="before" />
<aop:after-returning pointcut-ref="beforHello" method="after" />
<aop:around pointcut-ref="beforHello" method="around"/>
</aop:aspect>
</aop:config>
</beans>
如果pointcunt元素定义为cn.com.gan.spring.aop.Hello.*(..)),应该是么有相应的类和方法被代理,但是为什么会报错。测试了下 方法名可以不存在 但是如果相应的类不存在的话会报错。