基于API 的spring aop(XML Schema)

1、接口类
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.*(..)),应该是么有相应的类和方法被代理,但是为什么会报错。测试了下 方法名可以不存在 但是如果相应的类不存在的话会报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值