Spring笔记(八)aop编程

[b]一、Advice:最顶层接口为aop联盟定制[/b]
[i] 1、Methods Before Advice[/i]
a、实现MethodBeforeAdvice接口。
b、在目标方法执行之前被执行。
c、不能阻止目标方法被调用。
d、不能改变目标方法的返回值。
e、例如在进行权限控制时,一般不使用Methods Before Advice,因为即使验证了权限不够,也不能阻止目标方法的执行。

[i] 2、After returning Advice[/i]
a、实现AfterReturningAdvice接口。
b、在目标方法执行完成后被执行。
c、不能阻止目标方法被调用。
e、不能改变目标方法的返回值。

[i]3、Throws Advice[/i]
a、ThrowsAdvice接口。
b、项目中框架师会利用模板模式
c、处理目标抛出的异常
d、根据异常类型的匹配,根据异常树的就近匹配。
e、并不屏蔽异常,如:目标对象抛出了异常,经过Throws Advice处理后,继续抛给代理对象。
/**
* 前置通知,抛出后通知,异常处理通知
*
* @author dsm
*
*/
public class LogAdvice implements MethodBeforeAdvice, AfterReturningAdvice,
ThrowsAdvice {

/**
* @param method 目标对象的目标方法
* @param args 目标对象的目标方法的参数
* @param target 目标对象
*/
public void before(Method method, Object[] args, Object target)
throws Throwable {

System.out.println("execute time: " + new Date());
}

/**
* @param returnValue 目标对象的目标方法的返回值
* @param method 目标对象的目标方法
* @param args 目标对象的目标方法的参数
* @param target 目标对象
*/
public void afterReturning(Object returnValue, Method method, Object[] args,
Object target) throws Throwable {

System.out.println("finish time: " + new Date());
}

/**
* @param ine 抛出的异常对象
*/
public void afterThrowing(IllegalNameException ine) {

System.out.println("disponse: " + ine.getMessage());
}
public void afterThrowing(IllegalAgeException iae) {

System.out.println("disponse: " + iae.getMessage());
}
public void afterThrowing(CustomerException ce) {

System.out.println("disponse: " + ce.getMessage());
}
}


[i]4、拦截器,又称Around Advice[/i]
a、实现MethodInterceptor接口。
b、不同与前面的通知,其在目标方法执行的前后任何时候都可以实时拦截操作。
c、可以阻止目标方法被调用。
d、可以改变目标方法的返回值。
/**
* 环绕通知(拦截器)
*
* @author dsm
*
*/
public class LogIntercepter implements MethodInterceptor {

/**
* @param invocation
*/
public Object invoke(MethodInvocation invocation) throws Throwable {

System.out.println("Execute time: " + new Date());
//调用目标对象的业务逻辑方法
Object rstObj = invocation.proceed();
System.out.println("Finish time: " + new Date());

return rstObj;
}
}

	<!-- 目标对象 -->
<bean id="customerServiceTarget" class="aop.spring.CustomerServiceImpl" />

<!-- 通知 -->
<bean id="logAdvice" class="aop.spring.LogAdvice" />
<bean id="logIntercepter" class="aop.spring.LogIntercepter" />

<!-- 代理 (将我们的切面织入到目标对象)-->
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 若目标对象实现了代理接口,则可以提供代理接口的配置 -->
<property name="proxyInterfaces" value="aop.spring.ICustomerServiceProxy" />
<!-- 配置目标对象 -->
<property name="target" ref="customerServiceTarget" />
<!-- 配置切面 -->
<property name="interceptorNames">
<list>
<value>logIntercepter</value>
</list>
</property>
</bean>


[i] 5、Introduction[/i]
a、在不修改目标对象的源代码的情况下,为目标对象增加方法和属性。

[b]二、Pointcut;[/b]
1、实现Pointcut接口,实现getMethodMatcher和getClassFilter方法
a、ClassFilter接口用于进行类匹配,matches方法返回true,则运用切面,flase不运用。
b、MethodMatcher接口用于进行方法匹配,isRuntime方法,返回true表明为动态切入点,执行下面二个方法。返回false,只执行下面第一个方法。
c、MethodMatcher接口的matches(Method method, Class targetClass)方法同ClassFilter接口的matches的方法。
d、MethodMatcher接口的matches(Method method, Class targetClass, Object[] args)方法为动态切入点实现,在运行时通过判断传入的参数来决定是否运用切面。
e、不提倡使用动态切入点,十分影响性能。

public class MyPointcut implements Pointcut {

public ClassFilter getClassFilter() {

return new ClassFilter() {
public boolean matches(Class clazz) {

return clazz.getSimpleName().equals("SomeServiceImpl");
}
};
}

public MethodMatcher getMethodMatcher() {

return new MethodMatcher() {

public boolean isRuntime() {
return false;
}

public boolean matches(Method method, Class clazz) {

return method.getName().equals("doSome");
}

public boolean matches(Method method, Class clazz, Object[] args) {
//不使用动态切入点
return false;
}
};
}
}


2、将Advice和pointcut绑定成Advisor
a、实现Advisor接口里的getPointcut和getAdvice方法。
b、建议用ioc的方法。
	<!-- 切入点 -->
<bean id="myPoincut" class="aop.spring.pointcut.MyPointcut" />

<!-- 将Advice和pointcut绑定成Advisor -->
<bean id="myAdvisor" class="aop.spring.pointcut.MyAdvisor">
<property name="advice" ref="someServiceAdvice" />
<property name="pointcut" ref="myPoincut" />
</bean>

<bean id="someServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="someServiceTarget" />
<property name="proxyInterfaces" value="aop.spring.pointcut.ISomeService" />
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
</list>
</property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值