- 浏览: 283445 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (158)
- 默认类别 (22)
- tomcat study (5)
- spring study (2)
- hibernate study (2)
- jms study (8)
- acegi study (7)
- linux study (3)
- database study (19)
- appserver study (3)
- cvs study (10)
- mysql study (2)
- ajax study (5)
- uttest study (5)
- uml study (2)
- subversion study (3)
- xml study (6)
- japanese study (7)
- english study (2)
- loadrunner study (2)
- annotation study (0)
- security study (10)
- perl study (0)
- it lecture (14)
- view study (5)
- unicode study (1)
- net study (6)
- rule study (5)
- sdo study (1)
- jbpm study (1)
最新评论
-
xieruilin:
问题解决 。。。3Q
经常在安装ORACLE数据库时,出现一个ORA-12638 凭证检索失败 -
xiebiao110:
嗯不错,我也在看tomcat6,tomcat4,分模块来分析, ...
3、tomcat中的设计模式。 -
alloyer:
非常牛叉,再有个实例就完美了!
学习acegi-security -
bulain:
少了ehcache的jar包。
acegi的MethodSecurityInterceptor -
yuen:
你好,我才开始看acegi,把你的这个例子运行了一下,出错了, ...
acegi的MethodSecurityInterceptor
今天对Proxy和cglib的原理进行了一些学习,主要是参考网上找来的资料:
准备工作,对cglig和proxy代理进行相关的学习。
1、cglib的学习,cglib是靠继承关系来实现代理的目的,即具体代码如下:
2、proxy代理,主要靠实现接口来实现代理的目的,即具体代码如下:
3、Spring中AOP的实现。剖析Spring中的cglig和proxy。
Spring中的核心模块为:bean和aop;其中bean模块对aop提供了支持。
准备工作,对cglig和proxy代理进行相关的学习。
1、cglib的学习,cglib是靠继承关系来实现代理的目的,即具体代码如下:
public class AOPInstrumenter implements MethodInterceptor { private static Log logger = LogFactory.getLog(AOPInstrumenter.class); private Enhancer enhancer = new Enhancer(); public Object getInstrumentedClass(Class clz) { enhancer.setSuperclass(clz); enhancer.setCallback(this); return enhancer.create(); } public Object intercept(Object o, Method method, Object[] methodParameters,MethodProxy methodProxy) throws Throwable { logger.debug("Before Method =>" + method.getName()); Object result = methodProxy.invokeSuper(o, methodParameters); logger.debug("After Method =>" + method.getName()); return result; } }
2、proxy代理,主要靠实现接口来实现代理的目的,即具体代码如下:
public class AOPHandler implements InvocationHandler { private static Log logger = LogFactory.getLog(AOPHandler.class); private List interceptors = null; private Object originalObject; /** * 返回动态代理实例 * * @param obj * @return */ public Object bind(Object obj) { this.originalObject = obj; return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this); } /** * 在Invoke方法中,加载对应的Interceptor,并进行 * 预处理(before)、后处理(after)以及异常处理(exceptionThrow)过程 */ public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { Object result = null; Throwable ex = null; InvocationInfo invInfo = new InvocationInfo(proxy, method, args,result, ex); logger.debug("Invoking Before Intercetpors!"); invokeInterceptorsBefore(invInfo); try { logger.debug("Invoking Proxy Method!"); result = method.invoke(originalObject, args); invInfo.setResult(result); logger.debug("Invoking After Method!"); invokeInterceptorsAfter(invInfo); } catch (Throwable tr) { invInfo.setException(tr); logger.debug("Invoking exceptionThrow Method!"); invokeInterceptorsExceptionThrow(invInfo); throw new AOPRuntimeException(tr); } return result; } /** * 加载Interceptor * * @return */ private synchronized List getIntercetors() { if (null == interceptors) { interceptors = new ArrayList(); //Todo:读取配置,加载Interceptor实例 interceptors.add(new MyInterceptor()); } return interceptors; } /** * 执行预处理方法 * * @param invInfo */ private void invokeInterceptorsBefore(InvocationInfo invInfo) { List interceptors = getIntercetors(); int len = interceptors.size(); for (int i = 0; i < len; i++) { ((Interceptor) interceptors.get(i)).before(invInfo); } } /** * 执行后处理方法 * * @param invInfo */ private void invokeInterceptorsAfter(InvocationInfo invInfo) { List interceptors = getIntercetors(); int len = interceptors.size(); for (int i = len - 1; i >= 0; i--) { ((Interceptor) interceptors.get(i)).after(invInfo); } } /** * 执行异常处理方法 * * @param invInfo */ private void invokeInterceptorsExceptionThrow(InvocationInfo invInfo) { List interceptors = getIntercetors(); int len = interceptors.size(); for (int i = len - 1; i >= 0; i--) { ((Interceptor) interceptors.get(i)).exceptionThrow(invInfo); } } }
public class AOPFactory { static AOPHandler txHandler ; private static Log logger = LogFactory.getLog(AOPFactory.class); /** * 根据类名创建类实例 * * @param clzName * @return * @throws ClassNotFoundException */ public static Object getClassInstance(String clzName) { Class cls; try { cls = Class.forName(clzName); return (Object) cls.newInstance(); } catch (ClassNotFoundException e) { logger.debug(e); throw new AOPRuntimeException(e); } catch (InstantiationException e) { logger.debug(e); throw new AOPRuntimeException(e); } catch (IllegalAccessException e) { logger.debug(e); throw new AOPRuntimeException(e); } } /** * 根据传入的类名,返回AOP代理对象 * * @param clzName * @return */ public static Object getAOPProxyedObject(String clzName) { txHandler = new AOPHandler(); Object obj = getClassInstance(clzName); return txHandler.bind(obj); } /** * @return Returns the txHandler. */ public static AOPHandler getTxHandler() { return txHandler; } }
public interface Interceptor { public void before(InvocationInfo invInfo); public void after(InvocationInfo invInfo); public void exceptionThrow(InvocationInfo invInfo); }
public class MyInterceptor implements Interceptor { private static Log logger = LogFactory.getLog(MyInterceptor.class); public void before(InvocationInfo invInfo) { logger.debug("Pre-processing"); } public void after(InvocationInfo invInfo) { logger.debug("Post-processing"); } public void exceptionThrow(InvocationInfo invInfo) { logger.debug("Exception-processing"); } }
3、Spring中AOP的实现。剖析Spring中的cglig和proxy。
Spring中的核心模块为:bean和aop;其中bean模块对aop提供了支持。
public abstract class AbstractBeanFactory implements ConfigurableBeanFactory { public Object getBean(String name, Class requiredType, Object[] args) throws BeansException { String beanName = transformedBeanName(name); Object bean = null; // Eagerly check singleton cache for manually registered singletons. Object sharedInstance = null; synchronized (this.singletonCache) { sharedInstance = this.singletonCache.get(beanName); } if (sharedInstance != null) { if (sharedInstance == CURRENTLY_IN_CREATION) { throw new BeanCurrentlyInCreationException(beanName); } if (logger.isDebugEnabled()) { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } bean = getObjectForSharedInstance(name, sharedInstance); } else { // Check if bean definition exists in this factory. RootBeanDefinition mergedBeanDefinition = null; try { mergedBeanDefinition = getMergedBeanDefinition(beanName, false); } catch (NoSuchBeanDefinitionException ex) { // Not found -> check parent. if (this.parentBeanFactory instanceof AbstractBeanFactory) { // Delegation to parent with args only possible for AbstractBeanFactory. return ((AbstractBeanFactory) this.parentBeanFactory).getBean(name, requiredType, args); } else if (this.parentBeanFactory != null && args == null) { // No args -> delegate to standard getBean method. return this.parentBeanFactory.getBean(name, requiredType); } throw ex; } checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args); // Create bean instance. if (mergedBeanDefinition.isSingleton()) { synchronized (this.singletonCache) { // re-check singleton cache within synchronized block sharedInstance = this.singletonCache.get(beanName); if (sharedInstance == null) { if (logger.isInfoEnabled()) { logger.info("Creating shared instance of singleton bean '" + beanName + "'"); } this.singletonCache.put(beanName, CURRENTLY_IN_CREATION); try { sharedInstance = createBean(beanName, mergedBeanDefinition, args); this.singletonCache.put(beanName, sharedInstance); } catch (BeansException ex) { this.singletonCache.remove(beanName); throw ex; } } } bean = getObjectForSharedInstance(name, sharedInstance); } else { // It's a prototype -> create a new instance. bean = createBean(name, mergedBeanDefinition, args); } } // Check if required type matches the type of the actual bean instance. if (requiredType != null && !requiredType.isAssignableFrom(bean.getClass())) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return bean; } }
public abstract class AbstractBeanFactory implements ConfigurableBeanFactory { protected Object getObjectForSharedInstance(String name, Object beanInstance) throws BeansException { String beanName = transformedBeanName(name); // Don't let calling code try to dereference the // bean factory if the bean isn't a factory. if (isFactoryDereference(name) && !(beanInstance instanceof FactoryBean)) { throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass()); } // Now we have the bean instance, which may be a normal bean or a FactoryBean. // If it's a FactoryBean, we use it to create a bean instance, unless the // caller actually wants a reference to the factory. if (beanInstance instanceof FactoryBean) { if (!isFactoryDereference(name)) { // Return bean instance from factory. FactoryBean factory = (FactoryBean) beanInstance; if (logger.isDebugEnabled()) { logger.debug("Bean with name '" + beanName + "' is a factory bean"); } try { beanInstance = factory.getObject(); } catch (Exception ex) { throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex); } if (beanInstance == null) { throw new FactoryBeanNotInitializedException( beanName, "FactoryBean returned null object: " + "probably not fully initialized (maybe due to circular bean reference)"); } } else { // The user wants the factory itself. if (logger.isDebugEnabled()) { logger.debug("Calling code asked for FactoryBean instance for name '" + beanName + "'"); } } } return beanInstance; } }
public class ProxyFactoryBean extends AdvisedSupport implements FactoryBean, BeanFactoryAware, AdvisedSupportListener { public Object getObject() throws BeansException { return this.singleton ? getSingletonInstance() : newPrototypeInstance(); } private Object getSingletonInstance() { if (this.singletonInstance == null) { super.setFrozen(this.freezeProxy); this.singletonInstance = createAopProxy().getProxy(); } return this.singletonInstance; } }
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable { public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { Class targetClass = this.advised.getTargetSource().getTargetClass(); logger.debug("Creating JDK dynamic proxy" + (targetClass != null ? " for [" + targetClass.getName() + "]" : "")); } Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); } }
public class Cglib2AopProxy implements AopProxy, Serializable { public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { Class targetClass = this.advised.getTargetSource().getTargetClass(); logger.debug("Creating CGLIB2 proxy" + (targetClass != null ? " for [" + targetClass.getName() + "]" : "")); } Enhancer enhancer = new Enhancer(); try { Class rootClass = this.advised.getTargetSource().getTargetClass(); if (AopUtils.isCglibProxyClass(rootClass)) { enhancer.setSuperclass(rootClass.getSuperclass()); } else { enhancer.setSuperclass(rootClass); } enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised)); enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class)); enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised)); Callback[] callbacks = getCallbacks(rootClass); enhancer.setCallbacks(callbacks); if(CglibUtils.canSkipConstructorInterception()) { enhancer.setInterceptDuringConstruction(false); } Class[] types = new Class[callbacks.length]; for (int x = 0; x < types.length; x++) { types[x] = callbacks[x].getClass(); } enhancer.setCallbackTypes(types); // generate the proxy class and create a proxy instance Object proxy; if (this.constructorArgs != null) { proxy = enhancer.create(this.constructorArgTypes, this.constructorArgs); } else { proxy = enhancer.create(); } return proxy; } catch (CodeGenerationException ex) { throw new AspectException("Couldn't generate CGLIB subclass of class '" + this.advised.getTargetSource().getTargetClass() + "': " + "Common causes of this problem include using a final class or a non-visible class", ex); } catch (IllegalArgumentException ex) { throw new AspectException("Couldn't generate CGLIB subclass of class '" + this.advised.getTargetSource().getTargetClass() + "': " + "Common causes of this problem include using a final class or a non-visible class", ex); } catch (Exception ex) { // TargetSource.getTarget failed throw new AspectException("Unexpected AOP exception", ex); } } }
相关推荐
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结
Spring IOC AOP学习示例代码,包含Spring常用操作示例和所有所需jar文件。参考博客:http://blog.csdn.net/daijin888888/article/details/51735291
NULL 博文链接:https://microjava.iteye.com/blog/525796
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot
这个是上文描述的那个项目资料,spring -aop
Spring中的AOP可以控制权限,在我们做项目的时候,用Struts的过滤方法也可以实现。 当然这个文件只是提供测试,我们主要是学习这个AOP的思想。大家下载后可以再摸索......
SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo
Spring Aop 学习笔记
这是一个学习spring框架aop技术的一个很好的实例。
spring的aop使用实例,对于深入学习spring这有很大的作用
spring框架aop使用的jar包,需要链接到博客里,因为学习spring经常对所用 jar包迷糊,需要做整理。还望不要误删了....
spring aop经典例子的源代码,这个是学习spring aop特性的第一步
NULL 博文链接:https://linres.iteye.com/blog/281221
基于xml代码的SpringAOP实例详解,适合初步学习springAOP的开发人员了解该功能的使用。
帮助初学者深入理解Spring的好资料
很好的学习SpringAOP的资料,对于初学者和中级开发者都有很大帮助
spring aop简单例子,入门学习的好资料