`
dingjun1
  • 浏览: 208360 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring AOP使用结总结

阅读更多
AOP使用结总结
最近需要在原来的系统上添加业务日志,原有系统使用了spring2.0框架,立马想到用spring的AOP,由于考虑到系统运行环境在JDK1.4上,直接使用spring的AOP,不采用aspectj。

1、实现自己的日志业务类class LogMethodBeforeAdvice implements MethodBeforeAdvice

2、由于要记录方法指定参数和request中指定的对象,所以实现了自己的切入点,没有采用配置文件的方式
Class CustomPointcut implements Pointcut
public class CustomPointcut implements Pointcut{
	private ClassFilter classFilter;
	private MethodMatcher methodMatcher;
	public void setClassFilter(ClassFilter classFilter){
		this.classFilter = classFilter;
	}
	public void setMethodMatcher(MethodMatcher methodMatcher){
		this.methodMatcher = methodMatcher;
	}
	
	public ClassFilter getClassFilter() {
		// TODO Auto-generated method stub
		return true;
	}

	public MethodMatcher getMethodMatcher() {
		// TODO Auto-generated method stub
		return methodMatcher;
	}

}




public class CustomMethodMatch implements MethodMatcher {
	
	private CustomAopConfig config;
	public void setConfig(CustomAopConfig config){
		this.config = config;
	}
	
	//过滤日志触发点
	public boolean matches(Method arg0, Class arg1) {
		// TODO Auto-generated method stub
		Map aopConfigMap = config.getAopConfigMap();
		//如果配置中有该类arg1的名称,同时在类信息下有该方法arg0的名称则返回true
		if(aopConfigMap.containsKey(arg1.getName())){
			AopClassTarget target = (AopClassTarget)aopConfigMap.get(arg1.getName());
			Map methodMap = target.getMethods();
			if(methodMap.containsKey(arg0.getName())){
				return true;
			}
		}
		
		return false;
	}
	
	public boolean isRuntime() {//如果是非静态的,需要设为true
		// TODO Auto-generated method stub
		return false;
	}
	
//isRuntime()返回false不会调用该方法
	public boolean matches(Method arg0, Class arg1, Object[] arg2) {
		// TODO Auto-generated method stub
		return false;
	}

}



实现了自己的自动装配类,主要是考虑代理的bean与方法、参数等放在一个文件中,就不在这里再单独配置了
package com.jcy.support.aop.util;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import java.util.List;
/**
 * 自动代理类,BEAN配置如下:
 * <bean class="com.jcy.support.aop.util.CustomAopAutoProxy" init-method="setBeanNames">
	  <property name="interceptorNames">
	    <list>
	      <value>customAdvisor</value>
	    </list>
	  </property>
	</bean>
 * @author dingjun
 *
 */
public class CustomAopAutoProxy extends BeanNameAutoProxyCreator {
	
	private CustomAopConfig config;
	public void setConfig(CustomAopConfig config){
		this.config = config;
	}
	
	
	/**
	 * 载需要自动代理的BEAN的名称
	 * 必须在初始化 CustomAopAutoProxy后,马上被调用
	 * 
	 */
	public void init(){
		
		if(config!=null && !config.getBeanList().isEmpty()){
			List list = config.getBeanList();
			String[] str_arr = new String[list.size()];
			config.getBeanList().toArray(str_arr);
			super.setBeanNames(str_arr);
		}
	}
}


下面就是配置了

<bean id="config" 
		class="com.jcy.support.aop.util.CustomAopConfig" 
		scope="singleton" 
		destroy-method="destroy">
		<constructor-arg><value>custom_aop_config.xml</value></constructor-arg>
	</bean>
	
	
	<bean id="customPointcut" class="com.jcy.support.aop.util.CustomPointcut">
		<property name="classFilter" ref="classFilter"></property>
		<property name="methodMatcher" ref="methodMatcher"></property>
	</bean>
	<bean id="classFilter" class="com.jcy.support.aop.util.CustomClassFilter"></bean>
	<bean id="methodMatcher" class="com.jcy.support.aop.util.CustomMethodMatch" >
		<property name="config" ref="config"></property>
	</bean>
	
	<bean id="customAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<constructor-arg  ref="logAdvice"></constructor-arg>
		<constructor-arg  ref="customPointcut"></constructor-arg>
	</bean>

<!—使用自动代理的好处就是不用再去修改原来业务系统bean的名字了。 -->
<bean class="com.jcy.support.aop.util.CustomAopAutoProxy" init-method="init">
	  <property name="config" ref="config"></property>
<!--  <property name="beanNames" >
			<value>bean1,bean2,bean3</value>
</property>
这个参数由config中注入了,就不在这里设置了。
 -->
<!--  由于很多类没有实现自己的接口,此处应设为true,否则会报错,找不到类或方法之类的错误,会启用cglib ,
		如果代理的类实现的自己的接口就不用配置该项。
-->
	  <property name="proxyTargetClass"><value>true</value></property>
	  <property name="interceptorNames">
	    <list>
	      <value>customAdvisor</value>
	    </list>
	  </property>
	</bean>
分享到:
评论

相关推荐

    Spring框架小结

    主要讲述aop、控制反转IOC的原理、以及spring的工作原理及流程

    Spring 2.0 开发参考手册

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.7. 编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1....

    spring.doc

    SpringAOP概念拓展: 73 之前实现了目标方法的动态调用,现在来实现切面的动态调用。 74 4.2.2 AOP实现的两种模式 78 4.2.2.1 xml形式 78 XML形式拓展: 81 异常通知处理例子: 91 不用spring异常通知,另一种处理...

    Spring.3.x企业应用开发实战(完整版).part2

    经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...

    Spring中文帮助文档

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    Spring API

    6.4.2. Spring AOP中使用@AspectJ还是XML? 6.5. 混合切面类型 6.6. 代理机制 6.6.1. 理解AOP代理 6.7. 以编程方式创建@AspectJ代理 6.8. 在Spring应用中使用AspectJ 6.8.1. 在Spring中使用AspectJ进行domain ...

    spring chm文档

    6. 使用Spring进行面向切面编程(AOP) 6.1. 简介 6.1.1. AOP概念 6.1.2. Spring AOP的功能和目标 6.1.3. Spring的AOP代理 6.2. @AspectJ支持 6.2.1. 启用@AspectJ支持 6.2.2. 声明一个切面 6.2.3. 声明一个...

    Spring3.x企业应用开发实战(完整版) part1

    经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练使用Spring的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...

    spring-boot项目实践总结

    Spring Boot是由Pivotal团队提供的全新框架,其设计目的是简化新Spring应用程序的初始构建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种

    工作5年J2EE经验文档总结

    工作5年J2EE经验文档总结, 文档包括69个J2EE相关文档 Extjs学习笔记 hibernate总结 J2EE经验总结 ...j2ee学习总结 ...JAVA经典总结 ...spring经验总结 ...Spring_AOP_学习小结 自己总结的spring 等几十个总结大全

    Spring Security 中文教程.pdf

    19.2. 在Spring Security里使用LDAP 19.3. 配置LDAP服务器 19.3.1. 使用嵌入测试服务器 19.3.2. 使用绑定认证 19.3.3. 读取授权 19.4. 实现类 19.4.1. LdapAuthenticator实现 19.4.1.1. 常用功能 ...

    Spring Security-3.0.1中文官方文档(翻译版)

    19.2. 在Spring Security 里使用LDAP 19.3. 配置LDAP 服务器 19.3.1. 使用嵌入测试服务器 19.3.2. 使用绑定认证 19.3.3. 读取授权 19.4. 实现类 19.4.1. LdapAuthenticator 实现 19.4.1.1. 常用功能 ...

    asp.net知识库

    C#静态成员和方法的学习小结 C#中结构与类的区别 C#中 const 和 readonly 的区别 利用自定义属性,定义枚举值的详细文本 Web标准和ASP.NET - 第一部分 XHTML介绍 在ASP.NET页面中推荐使用覆写(Override)而不是事件...

Global site tag (gtag.js) - Google Analytics