1.spring-core概览
spring-core是spring框架的基石,它为spring框架提供了基础的支持。
spring-core从源码上看,分为6个package,分别是asm,cglib,core,lang,objenesis和util。
1.1 asm
关于asm的内幕参见博客:
spring源码分析之spring-core asm概述
1.2 cglib
关于cglib的内幕参见博客
cglib源码分析--转
1.3 core
1.4 lang
四个注解接口
/** * Indicates that the annotated element uses the Http Server available in * {@code com.sun.*} classes, which is only available on a Sun/Oracle JVM. * * @author Stephane Nicoll * @since 4.1 */ @Retention(RetentionPolicy.CLASS) @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE}) @Documented public @interface UsesSunHttpServer { }
/** * Indicates that the annotated element uses Java 7 specific API constructs, * without implying that it strictly requires Java 7. * * @author Stephane Nicoll * @since 4.1 */ @Retention(RetentionPolicy.CLASS) @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE}) @Documented @Deprecated public @interface UsesJava7 { }
/** * Indicates that the annotated element uses Java 8 specific API constructs, * without implying that it strictly requires Java 8. * * @author Stephane Nicoll * @since 4.1 */ @Retention(RetentionPolicy.CLASS) @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE}) @Documented @Deprecated public @interface UsesJava8 { }
/** * Indicates that the annotated element uses an API from the {@code sun.misc} * package. * * @author Stephane Nicoll * @since 4.3 */ @Retention(RetentionPolicy.CLASS) @Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE}) @Documented public @interface UsesSunMisc { }
1.5 Objenesis
官网:http://objenesis.org/
Objenesis is a small Java library that serves one purpose:
- To instantiate a new object of a particular class.
Objenesis是专门用于实例化一些特殊java对象的一个工具,如私有构造方法,带参数的构造等不能通过class.newInstance()实例化的,通过它可以轻松完成。
When would you want this?
Java already supports this dynamic instantiation of classes using Class.newInstance()
. However, this only works if the class has an appropriate constructor. There are many times when a class cannot be instantiated this way, such as when the class contains:
- Constructors that require arguments.
- Constructors that have side effects.
- Constructors that throw exceptions.
As a result, it is common to see restrictions in libraries stating that classes must require a default constructor. Objenesis aims to overcome these restrictions by bypassing the constructor on object instantiation.
Typical uses
Needing to instantiate an object without calling the constructor is a fairly specialized task, however there are certain cases when this is useful:
- Serialization, Remoting and Persistence - Objects need to be instantiated and restored to a specific state, without invoking code.
- Proxies, AOP Libraries and Mock Objects - Classes can be subclassed without needing to worry about the super() constructor.
- Container Frameworks - Objects can be dynamically instantatiated in non-standard ways.
示例:
Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer MyThingy thingy1 = (MyThingy) objenesis.newInstance(MyThingy.class); // or (a little bit more efficient if you need to create many objects) Objenesis objenesis = new ObjenesisStd(); // or ObjenesisSerializer ObjectInstantiator thingyInstantiator = objenesis.getInstantiatorOf(MyThingy.class); MyThingy thingy2 = (MyThingy)thingyInstantiator.newInstance(); MyThingy thingy3 = (MyThingy)thingyInstantiator.newInstance(); MyThingy thingy4 = (MyThingy)thingyInstantiator.newInstance();
SpringObjenesis封装了Objenesis,涉及了一个spring封装的数据结构org.springframework.util.ConcurrentReferenceHashMap
A ConcurrentHashMap that uses soft or weak references for both keys and values. This class can be used as an alternative to Collections.synchronizedMap(new WeakHashMap<K, Reference<V>>()) in order to support better performance when accessed concurrently. This implementation follows the same design constraints as ConcurrentHashMap with the exception that null values and null keys are supported. NOTE: The use of references means that there is no guarantee that items placed into the map will be subsequently available. The garbage collector may discard references at any time, so it may appear that an unknown thread is silently removing entries. If not explicitly specified, this implementation will use soft entry references. Since: 3.2
可以做缓存使用,不保证命中率。
1.6 util包
spring提供了丰富的util工具类,ObjectUtil和classUtil是最基本的两个类:
Class的定义:
/** * Instances of the class {@code Class} represent classes and * interfaces in a running Java application. An enum is a kind of * class and an annotation is a kind of interface. Every array also * belongs to a class that is reflected as a {@code Class} object * that is shared by all arrays with the same element type and number * of dimensions. The primitive Java types ({@code boolean}, * {@code byte}, {@code char}, {@code short}, * {@code int}, {@code long}, {@code float}, and * {@code double}), and the keyword {@code void} are also * represented as {@code Class} objects. * * <p> {@code Class} has no public constructor. Instead {@code Class} * objects are constructed automatically by the Java Virtual Machine as classes * are loaded and by calls to the {@code defineClass} method in the class * loader. * * <p> The following example uses a {@code Class} object to print the * class name of an object: * * <blockquote><pre> * void printClassName(Object obj) { * System.out.println("The class of " + obj + * " is " + obj.getClass().getName()); * } * </pre></blockquote> * * <p> It is also possible to get the {@code Class} object for a named * type (or for void) using a class literal. See Section 15.8.2 of * <cite>The Java™ Language Specification</cite>. * For example: * * <blockquote> * {@code System.out.println("The name of class Foo is: "+Foo.class.getName());} * </blockquote> * * @param <T> the type of the class modeled by this {@code Class} * object. For example, the type of {@code String.class} is {@code * Class<String>}. Use {@code Class<?>} if the class being modeled is * unknown. * * @author unascribed * @see java.lang.ClassLoader#defineClass(byte[], int, int) * @since JDK1.0 */ public final class Class<T> implements java.io.Serializable, GenericDeclaration, Type, AnnotatedElement
说明
Java程序在运行时,Java运行时系统一直对所有的对象进行所谓的运行时类型标识。这项信息纪录了每个对象所属的类。虚拟机通常使用运行时类型信息选准正确方法去执行,用来保存这些类型信息的类是Class类。Class类封装一个对象和接口运行时的状态,当装载类时,Class类型的对象自动创建。
Class 没有公共构造方法。Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的,因此不能显式地声明一个Class对象。
虚拟机为每种类型管理一个独一无二的Class对象。也就是说,每个类(型)都有一个Class对象。运行程序时,Java虚拟机(JVM)首先检查是否所要加载的类对应的Class对象是否已经加载。如果没有加载,JVM就会根据类名查找.class文件,并将其Class对象载入。
基本的 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也都对应一个 Class 对象。
每个数组属于被映射为 Class 对象的一个类,所有具有相同元素类型和维数的数组都共享该 Class 对象。
一般某个类的Class对象被载入内存,它就用来创建这个类的所有对象。
一、如何得到Class的对象呢?有三种方法可以的获取:
1、调用Object类的getClass()方法来得到Class对象,这也是最常见的产生Class对象的方法。例如:
MyObject x;
Class c1 = x.getClass();
2、使用Class类的中静态forName()方法获得与字符串对应的Class对象。例如:
Class c2=Class.forName("MyObject"),Employee必须是接口或者类的名字。
3、获取Class类型对象的第三个方法非常简单。如果T是一个Java类型,那么T.class就代表了匹配的类对象。例如
Class cl1 = Manager.class;
Class cl2 = int.class;
Class cl3 = Double[].class;
注意:Class对象实际上描述的只是类型,而这类型未必是类或者接口。例如上面的int.class是一个Class类型的对象。由于历史原因,数组类型的getName方法会返回奇怪的名字。
二、Class类的常用方法
1、getName()
一个Class对象描述了一个特定类的属性,Class类中最常用的方法getName以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。
2、newInstance()
Class还有一个有用的方法可以为类创建一个实例,这个方法叫做newInstance()。例如:
x.getClass.newInstance(),创建了一个同x一样类型的新实例。newInstance()方法调用默认构造器(无参数构造器)初始化新建对象。
3、getClassLoader()
返回该类的类加载器。
4、getComponentType()
返回表示数组组件类型的 Class。
5、getSuperclass()
返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的超类的 Class。
6、isArray()
判定此 Class 对象是否表示一个数组类。
三、Class的一些使用技巧
1、forName和newInstance结合起来使用,可以根据存储在字符串中的类名创建对象。例如
Object obj = Class.forName(s).newInstance();
2、虚拟机为每种类型管理一个独一无二的Class对象。因此可以使用==操作符来比较类对象。例如:
if(e.getClass() == Employee.class)...
Object定义:
/** * Class {@code Object} is the root of the class hierarchy. * Every class has {@code Object} as a superclass. All objects, * including arrays, implement the methods of this class. * * @author unascribed * @see java.lang.Class * @since JDK1.0 */ public class Object
参考文献:
【1】http://lavasoft.blog.51cto.com/62575/15433
微信公众号: 架构师日常笔记 欢迎关注!