`
cyz001
  • 浏览: 42549 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

spring recipes笔记 - 使用经典的spring通知来模块化横切关注点

阅读更多
虽然动态代理在模块化横切关注点方面很有帮助,但编写如此低层次代码对应用开发者来说太过苛刻。

Aop为应用程序开发者定义了一组高层次的概念,用于表达横切关注点。
经典的spring aop支持4种类型的通知:

1前置通知
2返回通知
3异常通知
4环绕通知


前置通知在方法执行之前执行,可以通过实现MethodBeforeAdvice接口创建它
public class LoggingBeforeAdvice implements MethodBeforeAdvice{
	private Log log = LogFactory.getLog(this.getClass());
	
	public void before(Method method, Object[] arg1, Object target)
			throws Throwable {
		log.info("the method "+method.getName()+"() start");
	}
}

接下来,为每个计算器Bean创建一个代理以应用该通知,在spring aop里,代理的创建是通过一个叫ProxyFactryBean的工厂bean完成

<bean id="loggingBeforeAdvice" class="com.netease.z3.LoggingBeforeAdvice"></bean>

<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
		</list>
		</property>
	</bean>

ProxyFactoryBean只为实现了任意接口的目标bean创建jdk代理。如果目标bean没有实现任何接口,那么ProxyFactoryBean将创建CGLIB代理。
在Main类里,应用从IOC容器获取应用了日志代理通知的代理Bean
public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("z3.xml");
		Compute compute = (Compute)context.getBean("computeProxy");
		compute.add(10,10);
	}


返回通知在方法执行后记录方法的结束和返回的结果,可以通过实现AfterReturningAdvice接口创建它

public class LoggingAfterAdvice implements AfterReturningAdvice{
	private Log log = LogFactory.getLog(this.getClass());

	public void afterReturning(Object returnValue, Method method, Object[] arg,
			Object target) throws Throwable {
		// TODO Auto-generated method stub
		log.info("the method "+method.getName()+" end "+returnValue);
	}
}


要使这个通知生效,需要在IOC容器里声明一个它的实例,然后在interceptorNames属性里面增加一项对它的引用

<bean id="loggingAfterAdvice" class="com.netease.z3.LoggingAfterAdvice"></bean>
	<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
				<value>loggingAfterAdvice</value>
			</list>
		</property>
	</bean>


第三种通知是异常通知,要能够产生异常,要为算术计算器增加一个检查,有异常时,将抛出Exception
public class ComputeImpl implements Compute {
	private Log log = LogFactory.getLog(this.getClass());
	/* (non-Javadoc)
	 * @see com.netease.dao.Compute#add(double, double)
	 */
	public void div(double a,double b){
		if(b==0){
			throw new IllegalArgumentException("by zero");
		}
	}
}


对于异常通知类型,必须实现ThrowsAdvice接口,每个处理方法的程序必须是afterThrowing.异常的类型由方法的参数类型指定。

public class LoggingThrowsAdvice implements ThrowsAdvice{
	private Log log = LogFactory.getLog(this.getClass());
	public void afterThrowing(IllegalArgumentException e)throws Throwable{
		log.info("Illegal argument");
	}
}


在ioc容器声明一个该通知的实例,然后在interceptorNames属性增加一项对它的引用

<bean id="loggingThrowsAdvice" class="com.netease.z3.LoggingThrowsAdvice"></bean>
	<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
				<value>loggingAfterAdvice</value>
				<value>loggingThrowsAdvice</value>
			</list>
		</property>
	</bean>



环绕通知,在所有通知类型中,它是最强大的,因为它能完全控制方法的执行过程,所以可以把前面所有通知动作合并到一个单独的通知里面。

环绕通知必须实现MethodIntercepor接口,如果要继续执行原始方法那么必须调用methodInvocation.proceed(),如果忘记这步,原始的方法是不会被调用,下面环绕通知合并了前面的前置,后置,异常通知.

public class LoggingAroundAdvice implements MethodInterceptor {
	private Log log = LogFactory.getLog(this.getClass());
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		log.info("the method "+methodInvocation.getMethod().getName()+"() start"+
				" with "+Arrays.toString(methodInvocation.getArguments()));
		try{
			Object result = methodInvocation.proceed();
			log.info("the method "+methodInvocation.getMethod().getName()+"() end "+result);
			return result;
		}catch(Exception e){
			log.error("error");
			throw e;
		}
	}
}

<bean id="loggingAroundAdvice" class="com.netease.z3.LoggingAroundAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingAroundAdvice</value>
			</list>
		</property>
	</bean>


下一篇:spring recipes笔记 - 使用经典的spring切入点匹配方法
分享到:
评论
1 楼 zhhx1115 2010-04-28  
cyz001 写道
虽然动态代理在模块化横切关注点方面很有帮助,但编写如此低层次代码对应用开发者来说太过苛刻。

Aop为应用程序开发者定义了一组高层次的概念,用于表达横切关注点。
经典的spring aop支持4种类型的通知:

1前置通知
2返回通知
3异常通知
4环绕通知


前置通知在方法执行之前执行,可以通过实现MethodBeforeAdvice接口创建它
public class LoggingBeforeAdvice implements MethodBeforeAdvice{
	private Log log = LogFactory.getLog(this.getClass());
	
	public void before(Method method, Object[] arg1, Object target)
			throws Throwable {
		log.info("the method "+method.getName()+"() start");
	}
}

接下来,为每个计算器Bean创建一个代理以应用该通知,在spring aop里,代理的创建是通过一个叫ProxyFactryBean的工厂bean完成

<bean id="loggingBeforeAdvice" class="com.netease.z3.LoggingBeforeAdvice"></bean>

<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
		</list>
		</property>
	</bean>

ProxyFactoryBean只为实现了任意接口的目标bean创建jdk代理。如果目标bean没有实现任何接口,那么ProxyFactoryBean将创建CGLIB代理。
在Main类里,应用从IOC容器获取应用了日志代理通知的代理Bean
public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("z3.xml");
		Compute compute = (Compute)context.getBean("computeProxy");
		compute.add(10,10);
	}


返回通知在方法执行后记录方法的结束和返回的结果,可以通过实现AfterReturningAdvice接口创建它

public class LoggingAfterAdvice implements AfterReturningAdvice{
	private Log log = LogFactory.getLog(this.getClass());

	public void afterReturning(Object returnValue, Method method, Object[] arg,
			Object target) throws Throwable {
		// TODO Auto-generated method stub
		log.info("the method "+method.getName()+" end "+returnValue);
	}
}


要使这个通知生效,需要在IOC容器里声明一个它的实例,然后在interceptorNames属性里面增加一项对它的引用

<bean id="loggingAfterAdvice" class="com.netease.z3.LoggingAfterAdvice"></bean>
	<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
				<value>loggingAfterAdvice</value>
			</list>
		</property>
	</bean>


第三种通知是异常通知,要能够产生异常,要为算术计算器增加一个检查,有异常时,将抛出Exception
public class ComputeImpl implements Compute {
	private Log log = LogFactory.getLog(this.getClass());
	/* (non-Javadoc)
	 * @see com.netease.dao.Compute#add(double, double)
	 */
	public void div(double a,double b){
		if(b==0){
			throw new IllegalArgumentException("by zero");
		}
	}
}


对于异常通知类型,必须实现ThrowsAdvice接口,每个处理方法的程序必须是afterThrowing.异常的类型由方法的参数类型指定。

public class LoggingThrowsAdvice implements ThrowsAdvice{
	private Log log = LogFactory.getLog(this.getClass());
	public void afterThrowing(IllegalArgumentException e)throws Throwable{
		log.info("Illegal argument");
	}
}


在ioc容器声明一个该通知的实例,然后在interceptorNames属性增加一项对它的引用

<bean id="loggingThrowsAdvice" class="com.netease.z3.LoggingThrowsAdvice"></bean>
	<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingBeforeAdvice</value>
				<value>loggingAfterAdvice</value>
				<value>loggingThrowsAdvice</value>
			</list>
		</property>
	</bean>



环绕通知,在所有通知类型中,它是最强大的,因为它能完全控制方法的执行过程,所以可以把前面所有通知动作合并到一个单独的通知里面。

环绕通知必须实现MethodIntercepor接口,如果要继续执行原始方法那么必须调用methodInvocation.proceed(),如果忘记这步,原始的方法是不会被调用,下面环绕通知合并了前面的前置,后置,异常通知.

public class LoggingAroundAdvice implements MethodInterceptor {
	private Log log = LogFactory.getLog(this.getClass());
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		log.info("the method "+methodInvocation.getMethod().getName()+"() start"+
				" with "+Arrays.toString(methodInvocation.getArguments()));
		try{
			Object result = methodInvocation.proceed();
			log.info("the method "+methodInvocation.getMethod().getName()+"() end "+result);
			return result;
		}catch(Exception e){
			log.error("error");
			throw e;
		}
	}
}

<bean id="loggingAroundAdvice" class="com.netease.z3.LoggingAroundAdvice"></bean>
<bean id="computeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="target" ref="compute"></property>
		<property name="interceptorNames">
			<list>
				<value>loggingAroundAdvice</value>
			</list>
		</property>
	</bean>


下一篇:spring recipes笔记 - 使用经典的spring切入点匹配方法

ssdfsd

相关推荐

Global site tag (gtag.js) - Google Analytics