这是我参与8月更文挑战的第四天,活动详情查看:8月更文挑战
功能介绍
见名知其意,该模块属于spirng framewok中最为核心的模块,spring许多功能都需要该模块的支持。其中包括:
子模块名称 | 描述信息 |
---|---|
org.springframework.asm |
对Java本身字节码的操作,描述向JavaAgent 的同学应该对这块比较熟悉 |
org.springframework.cglib |
集成动态代理模块 |
org.springframework.core |
spring本身core模块,也是整个项目最核心的模块 |
org.springframework.lang |
注解类模块 |
org.springframework.util |
工具模块包括:比较器,并发、xml等工具类 |
org.springframework.objenesis |
集成objenesis模块 |
第三方依赖
dependencies {
cglib("cglib:cglib:${cglibVersion}@jar") // cglib动态代理
objenesis("org.objenesis:objenesis:${objenesisVersion}@jar") // objenesis集成
coroutines(project(path: ":kotlin-coroutines", configuration: 'classesOnlyElements'))
compile(files(cglibRepackJar))
compile(files(objenesisRepackJar))
compile(project(":spring-jcl")) // 日志
compileOnly(project(":kotlin-coroutines"))
compileOnly("io.projectreactor.tools:blockhound") // java agent[监控工具](https://github.com/reactor/BlockHound)
optional("net.sf.jopt-simple:jopt-simple") // 命令行解析工具
optional("org.aspectj:aspectjweaver") // eclipse旗下的http://www.eclipse.org/aspectj/
optional("org.jetbrains.kotlin:kotlin-reflect")
optional("org.jetbrains.kotlin:kotlin-stdlib")
optional("io.projectreactor:reactor-core") // 非阻塞 IO
optional("io.reactivex:rxjava") // Reactive Extension for Java
optional("io.reactivex:rxjava-reactive-streams") // rxjava streams操作
optional("io.reactivex.rxjava2:rxjava") // 向下兼容集成rxjava2
optional("io.reactivex.rxjava3:rxjava") // 集成rxjava3
optional("io.netty:netty-buffer") // nio框架集成
// test 专用直接跳过
testCompile("io.projectreactor:reactor-test") // reactor 测试包
testCompile("com.google.code.findbugs:jsr305") // 这个比较有意思,使用google的findbugs
testCompile("javax.annotation:javax.annotation-api") // 一些java的注解api,比如:@PostConstruct、@Resource、@PreDestroy等,反正全是注解,感兴趣的同学可以研究研究
testCompile("javax.xml.bind:jaxb-api") // 操作xml
testCompile("com.fasterxml.woodstox:woodstox-core") // 快速开源且符合StAX(STreaming Api for Xml processing)规范的XML处理器(相当于写入,序列化)
testCompile("org.xmlunit:xmlunit-assertj") // xml 测试相关
testCompile("org.xmlunit:xmlunit-matchers") // xml匹配相关
testCompile(project(":kotlin-coroutines"))
testCompile("io.projectreactor.tools:blockhound")
}
复制代码
从以上引入的第三方依赖可以得知,spring-core模块中用到的动态代理,jvm监控功能,支持命令行解析,aop,响应式编程。测试采用的是xmlunit相关的。这里介绍一下有关objecesis,其它的暂跳过,毕竟侧重点为spring本身。
objenesis
以下是官方的介绍
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 instantiated in non-standard ways.
简单来说就是:用来创建特定的对象。
主要解决:由于不是所有的类都有无参构造器又或者类构造器是private,在这样的情况下,如果我们还想实例化对象,class.newInstance是无法满足的。由此objenesis诞生了,它就是来解决这些问题的。
实现原理:根据对象的类型、JVM 版本、JVM 供应商和安全管理器来对对象进行实例化。使用ConcurrentHashMap
进行缓存对象,然后使用putIfAbsent
进行添加,最后用到asm字节码动态操作并实例化目标对象。
public class TestInstance {
public String name;
private int x;
private Integer y;
public TestInstance(String name, int x, Integer y) {
this.name = name;
this.x = x;
this.y = y;
}
public void print() {
System.out.println("name = " + name);
System.out.println("x = " + x);
System.out.println("y = " + y);
}
public static void main(String[] args) {
Objenesis objenesis = new ObjenesisStd(true);
TestInstance test = objenesis.newInstance(TestInstance.class);
test.print();
System.out.println("===========");
TestInstance testInstance = new TestInstance("new", 1, null);
testInstance.print();
}
}
复制代码
那么spring用它做了什么呢,它是实现了Objenesis
接口,并且使用ConcurrentReferenceHashMap
进行缓存处理,应用与aop动态代理、spring-context以及spirng-web模块都有涉及。