- 浏览: 208360 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
strong8808:
activemq5.8.0 客户端,服务端启动序列图 -
xurichusheng:
第一题,如果使用 not exists 的话,可以改成:SEL ...
SQL笔试题 -
dingjun1:
cuisuqiang 写道如何解决呢?我的是对了也照样缓存增加 ...
事务未正确关闭引起的HIBERNATE SESSION不能正确关闭 -
dingjun1:
aijezdm915 写道lz ,我也是在写项目描述是犯愁,能 ...
如果在简历中描述项目 -
aijezdm915:
lz ,我也是在写项目描述是犯愁,能否给个你的简历demo,我 ...
如果在简历中描述项目
AOP使用结总结
最近需要在原来的系统上添加业务日志,原有系统使用了spring2.0框架,立马想到用spring的AOP,由于考虑到系统运行环境在JDK1.4上,直接使用spring的AOP,不采用aspectj。
1、实现自己的日志业务类class LogMethodBeforeAdvice implements MethodBeforeAdvice
2、由于要记录方法指定参数和request中指定的对象,所以实现了自己的切入点,没有采用配置文件的方式
实现了自己的自动装配类,主要是考虑代理的bean与方法、参数等放在一个文件中,就不在这里再单独配置了
下面就是配置了
最近需要在原来的系统上添加业务日志,原有系统使用了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>
发表评论
-
solr/home 设置
2013-04-21 00:44 1209solr/home是solr实例化core核的依据和入口,是必 ... -
CronTrigger Tutorial
2011-02-24 18:58 872转载地址:http://www.quartz-schedule ... -
事务未正确关闭引起的HIBERNATE SESSION不能正确关闭
2010-10-30 13:46 1624问题现象: 第一 ... -
hibernate 四种抓取策略
2010-10-24 10:42 893转载:http://www.cnblogs.com ... -
cas分析
2010-05-10 13:20 1952Central Authentication Service ... -
FCKeditor2.6.5及后续版本 for PHP配置及中文上传乱码解决!
2009-12-10 13:37 1376对于Fckeditor上传中文名文件时显示乱码的问题,现公布方 ... -
xpath语法
2009-10-12 21:02 889转载:http://www.w3schools.com/XPa ... -
jstl标签中循环map
2009-10-12 20:58 2552<% Map map = new HashMap(); ... -
log4j输出多个自定义日志文件(分离日志)
2009-06-24 11:30 2563转载: http://wangjc-opal.iteye.co ... -
错误: 必须限制口令文件读取访问
2008-10-10 17:06 3062转载:http://hi.baidu.com/lifecodi ... -
log4j手动加载配置文件
2008-09-10 15:26 3136PropertyConfigurator.configur ... -
spring集成hibernate使用缓存简单实例
2008-07-08 18:20 2304这部分为转载:http://www ... -
lucene应用入门1
2008-07-06 16:49 2110简单的建立索引和索引 ... -
多对多查询的条件设置和删除问题
2008-07-03 14:50 1467select 父 from 父类 父 left join 父. ... -
DOM4J学习笔记
2008-07-01 15:07 3673DOM4J简单介绍 基本使用 读取XML文档 Import o ... -
延迟初始化错误(转载)
2008-06-12 17:52 980转载地址:http://lz726.ite ... -
UUID [转帖]
2008-05-20 15:25 891/***********本人原创,欢迎转载,转载请保留本人信息 ... -
一段简单的发送邮件代码
2008-04-28 15:57 1013package send; import java. ... -
重新学习 Hibernate fetch lazy cascade inverse[转载]
2008-04-21 11:55 1206转载:http://www.blogjava.ne ... -
HIBERNATE2 CLOB字段延迟加载办法[转载]
2008-04-18 13:49 3056[转载]http://callan.iteye.com/blo ...
相关推荐
主要讲述aop、控制反转IOC的原理、以及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.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....
SpringAOP概念拓展: 73 之前实现了目标方法的动态调用,现在来实现切面的动态调用。 74 4.2.2 AOP实现的两种模式 78 4.2.2.1 xml形式 78 XML形式拓展: 81 异常通知处理例子: 91 不用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 ...
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 ...
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的各项功能的同时,还能透彻理解Spring的内部实现,真正做到知其然知其所以然。...
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是简化新Spring应用程序的初始构建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种
工作5年J2EE经验文档总结, 文档包括69个J2EE相关文档 Extjs学习笔记 hibernate总结 J2EE经验总结 ...j2ee学习总结 ...JAVA经典总结 ...spring经验总结 ...Spring_AOP_学习小结 自己总结的spring 等几十个总结大全
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. 常用功能 ...
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. 常用功能 ...
C#静态成员和方法的学习小结 C#中结构与类的区别 C#中 const 和 readonly 的区别 利用自定义属性,定义枚举值的详细文本 Web标准和ASP.NET - 第一部分 XHTML介绍 在ASP.NET页面中推荐使用覆写(Override)而不是事件...