`
dingjun1
  • 浏览: 211918 次
  • 性别: 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_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结

    总结,Spring AOP提供了一种优雅的方式,让我们能够分离关注点,实现代码的模块化,提高可维护性和复用性。理解并熟练运用这些概念和机制,可以极大地提升我们的开发效率和代码质量。通过学习和实践,你可以更好地在...

    Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现.doc

    Spring AOP 实现原理详解之 AOP 切面的实现 Spring AOP 是基于 IOC 的 Bean 加载来实现的,本文主要介绍 Spring AOP 原理解析的切面实现过程。AOP 切面的实现是将切面类的所有切面方法根据使用的注解生成对应 ...

    Spring框架小结

    Spring框架作为Java开发领域中最受欢迎的企业级应用程序框架之一,其核心特性包括控制反转(Inversion of Control, IoC)、依赖注入(Dependency Injection, DI)、面向切面编程(Aspect Oriented Programming, AOP...

    AOP小结

    **AOP小结** 面向切面编程(Aspect-Oriented Programming,简称AOP)是一种编程范式,旨在解决软件开发中的横切关注点问题。在传统的面向对象编程中,业务逻辑与系统服务(如日志、事务管理、权限检查等)往往交织...

    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 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.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. 声明一个...

    spring-security 官方文档 中文版

    - **小结**:这些组件共同构成了 Spring Security 的核心架构,提供了强大的身份验证和授权能力。 **5.3 验证** - **什么是 Spring Security 的验证呢?** - Spring Security 的验证机制允许开发者自定义认证过程...

    Java中Spring获取bean方法小结

    在Java开发中,Spring框架是不可或缺的一部分,它提供了一个强大的依赖注入(DI)和面向切面编程(AOP)的容器。Spring的核心在于其BeanFactory和ApplicationContext,它们使得对象的管理和装配变得简单。这里我们将...

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

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

    Spring配置总结

    Spring的核心特性包括依赖注入(Dependency Injection,DI)、面向切面编程(Aspect-Oriented Programming,AOP)、事务管理、数据访问/集成以及Web应用开发等。本文将重点介绍Spring的配置方法,特别是对于新手来说...

    轻松学JavaWeb开发之Spring框架入门PPT学习教案.pptx

    本资源摘要信息是关于...7. 小结:本章是Spring的入门章节,首先我们为大家介绍Spring技术,以及为什么要使用Spring,然后介绍了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 2.0.x完全中文参考文档

    包含了一系列简单的示例,用于演示如何配置和使用Spring Security。 - **3.2. Contacts** 通过这个示例展示如何在实际的应用场景中使用Spring Security。 - **3.3. LDAP例子** 展示了如何使用LDAP作为用户...

Global site tag (gtag.js) - Google Analytics