一、前言
关于spring-context包中定义org.springframework.instrument.classloading.LoadTimeWeaver类加载器运行接口、org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver对于glassfish类加载器实现类,通过该源码示例有足于熟悉了解glassfish加载器入口及自定义glassfish类加载器等。
二、源码示例
1.LoadTimeWeaver接口
package org.springframework.instrument.classloading;@b@@b@import java.lang.instrument.ClassFileTransformer;@b@@b@public abstract interface LoadTimeWeaver@b@{@b@ public abstract void addTransformer(ClassFileTransformer paramClassFileTransformer);@b@@b@ public abstract ClassLoader getInstrumentableClassLoader();@b@@b@ public abstract ClassLoader getThrowawayClassLoader();@b@}
2.GlassFishLoadTimeWeaver实现类
package org.springframework.instrument.classloading.glassfish;@b@@b@import java.lang.instrument.ClassFileTransformer;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import org.springframework.core.OverridingClassLoader;@b@import org.springframework.instrument.classloading.LoadTimeWeaver;@b@import org.springframework.util.Assert;@b@import org.springframework.util.ClassUtils;@b@@b@public class GlassFishLoadTimeWeaver@b@ implements LoadTimeWeaver@b@{@b@ private static final String INSTRUMENTABLE_LOADER_CLASS_NAME = "org.glassfish.api.deployment.InstrumentableClassLoader";@b@ private final ClassLoader classLoader;@b@ private final Method addTransformerMethod;@b@ private final Method copyMethod;@b@@b@ public GlassFishLoadTimeWeaver()@b@ {@b@ this(ClassUtils.getDefaultClassLoader());@b@ }@b@@b@ public GlassFishLoadTimeWeaver(ClassLoader classLoader)@b@ {@b@ Assert.notNull(classLoader, "ClassLoader must not be null");@b@ try@b@ {@b@ instrumentableLoaderClass = classLoader.loadClass("org.glassfish.api.deployment.InstrumentableClassLoader");@b@ this.addTransformerMethod = instrumentableLoaderClass.getMethod("addTransformer", new Class[] { ClassFileTransformer.class });@b@ this.copyMethod = instrumentableLoaderClass.getMethod("copy", new Class[0]);@b@ }@b@ catch (Throwable ex) {@b@ throw new IllegalStateException("Could not initialize GlassFishLoadTimeWeaver because GlassFish API classes are not available", ex);@b@ }@b@@b@ ClassLoader clazzLoader = null;@b@@b@ for (ClassLoader cl = classLoader; (cl != null) && (clazzLoader == null); cl = cl.getParent()) {@b@ if (instrumentableLoaderClass.isInstance(cl))@b@ clazzLoader = cl;@b@@b@ }@b@@b@ if (clazzLoader == null)@b@ {@b@ throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: A [" + instrumentableLoaderClass@b@ .getName() + "] implementation is required.");@b@ }@b@@b@ this.classLoader = clazzLoader;@b@ }@b@@b@ public void addTransformer(ClassFileTransformer transformer)@b@ {@b@ try@b@ {@b@ this.addTransformerMethod.invoke(this.classLoader, new Object[] { transformer });@b@ }@b@ catch (InvocationTargetException ex) {@b@ throw new IllegalStateException("GlassFish addTransformer method threw exception", ex.getCause());@b@ }@b@ catch (Throwable ex) {@b@ throw new IllegalStateException("Could not invoke GlassFish addTransformer method", ex);@b@ }@b@ }@b@@b@ public ClassLoader getInstrumentableClassLoader()@b@ {@b@ return this.classLoader;@b@ }@b@@b@ public ClassLoader getThrowawayClassLoader()@b@ {@b@ try {@b@ return new OverridingClassLoader(this.classLoader, (ClassLoader)this.copyMethod.invoke(this.classLoader, new Object[0]));@b@ }@b@ catch (InvocationTargetException ex) {@b@ throw new IllegalStateException("GlassFish copy method threw exception", ex.getCause());@b@ }@b@ catch (Throwable ex) {@b@ throw new IllegalStateException("Could not invoke GlassFish copy method", ex);@b@ }@b@ }@b@}