1 源码解析
1.1 源码解析1(jdk中的应用1)
1.2 源码解析2(jdk中的应用2)
1.3 源码解析3(Spring中的应用1)
1.4 源码解析4(Spring中的应用2)
1 源码解析
1.1 源码解析1(jdk中的应用1)
java.util.Comparator(策略类)作为比较器的应用
package java.util; public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); }
java.util.ArrayList(策略类应用)
package java.util; import java.lang.reflect.*; public class Arrays { public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } }
1.2 源码解析2(jdk中的应用2)
java.util.Comparator(策略类)作为比较器的应用
package java.util;
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
java.util.TreeMap(策略类应用)
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable { /** * The comparator used to maintain order in this tree map, or * null if it uses the natural ordering of its keys. * * @serial */ private final Comparator<? super K> comparator;
//此处涉及到泛型的应用,关于泛型==》详细可以查看文章 ==》java后台==》数据类型==》泛型使用部分 final int compare(Object k1, Object k2) { return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2) : comparator.compare((K)k1, (K)k2); } }
1.3 源码解析3(Spring中的应用1)诸多方法被策略行为类调用
org.springframework.core.io.Resource(策略抽象)
package org.springframework.core.io; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URL; public interface Resource extends InputStreamSource { boolean exists(); boolean isReadable(); boolean isOpen(); URL getURL() throws IOException; URI getURI() throws IOException; File getFile() throws IOException; long contentLength() throws IOException; long lastModified() throws IOException; Resource createRelative(String var1) throws IOException; String getFilename(); String getDescription(); }
策略行为类:FileSystemResource,ClassPathResource,InputStreamResource,PathResource等等
1.4 源码解析4(Spring中的应用2之bean的初始化应用)(特殊之处是策略行为之间继承,共同实现策略类)
InstantiationStrategy(初始化策略接口)
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.beans.factory.support; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; public interface InstantiationStrategy { Object instantiate(RootBeanDefinition var1, String var2, BeanFactory var3) throws BeansException; Object instantiate(RootBeanDefinition var1, String var2, BeanFactory var3, Constructor<?> var4, Object[] var5) throws BeansException; Object instantiate(RootBeanDefinition var1, String var2, BeanFactory var3, Object var4, Method var5, Object[] var6) throws BeansException; }
SimpleInstantiationStrategy (策略父类)
package org.springframework.beans.factory.support; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.AccessController; import java.security.PrivilegedAction; import java.security.PrivilegedExceptionAction; import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanFactory; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; public class SimpleInstantiationStrategy implements InstantiationStrategy { private static final ThreadLocal<Method> currentlyInvokedFactoryMethod = new ThreadLocal(); public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { ... } protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy"); } public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, final Constructor<?> ctor, Object[] args) { ... } protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, Constructor<?> ctor, Object[] args) { throw new UnsupportedOperationException("Method Injection not supported in SimpleInstantiationStrategy"); } public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, Object factoryBean, final Method factoryMethod, Object[] args) { ... } }
CglibSubclassingInstantiationStrategy (策略子类)
package org.springframework.beans.factory.support; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanInstantiationException; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.BeanFactory; import org.springframework.cglib.core.SpringNamingPolicy; import org.springframework.cglib.proxy.Callback; import org.springframework.cglib.proxy.CallbackFilter; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.Factory; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import org.springframework.cglib.proxy.NoOp; public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationStrategy { private static final int PASSTHROUGH = 0; private static final int LOOKUP_OVERRIDE = 1; private static final int METHOD_REPLACER = 2; public CglibSubclassingInstantiationStrategy() { } protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { return this.instantiateWithMethodInjection(beanDefinition, beanName, owner, (Constructor)null, (Object[])null); } protected Object instantiateWithMethodInjection(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, Constructor<?> ctor, Object[] args) { return (new CglibSubclassingInstantiationStrategy.CglibSubclassCreator(beanDefinition, owner)).instantiate(ctor, args); } ... }
诸葛