Spring笔记(八)aop编程

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

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

3、Throws Advice
    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());
	}
}


4、拦截器,又称Around Advice
    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>


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

二、Pointcut;
  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>

你可能感兴趣的:(spring,AOP,编程,C++,c)