Spring-Framework 源码阅读之AnnotationBeanUtils
Java程序员,就是要学会一个名字叫做“春”的东西,这玩意运用的非常的广泛,现在如果你的业务系统或者软件没有在这个东西上开发,都不要意思拿出来。因为你更不上时代了。在平时的工作的中基本都是简单的运用,没有深入的了解内部的肌理。这次我一定可以满满的看完里面的骨架。加油!加油!加油!
在之前我也看过一些讲Spring的书籍,比如<<Spring揭秘>>,《Spring技术内幕》。大体知道了Spring的工作流程,但是还是有些迷茫。有一点一知半解的感觉。接下来我准备以自己一知半解的半桶水的知识去阅读Spring的源码。根据自己的模模糊糊的感觉和去网上搜索来帮助阅读。这一次要地毯式的搜索阅读。可是有时也会觉得Spring 框架这么大,如此的阅读是不是浪费时间。但是又有另一种想法,只要我们把每个类的作用都了解清楚了,每个类都自己写一下单元测试。都跟进源码阅读。这样等我大部分的内容都阅读了,Spring的了解深度会更加清晰咯。
首先第一步就是下载Spring源码,然后导入Idea中,如下图所示。

Spring 最最最核心的地方就是Bean,所以我准备从spring-beans这个工程看起。首先第一个package: org.springframework.beans.annotation,这个包下就只有一个类AnnotationBeanUtils,该类就只有一个核心的方法,就是拷贝注解值到指定的类中。
public abstract class AnnotationBeanUtils {
/**
* Copy the properties of the supplied {@link Annotation} to the supplied target bean.
* Any properties defined in {@code excludedProperties} will not be copied.
* @param ann the annotation to copy from
* @param bean the bean instance to copy to
* @param excludedProperties the names of excluded properties, if any
* @see org.springframework.beans.BeanWrapper
*/
public static void copyPropertiesToBean(Annotation ann, Object bean, String... excludedProperties) {
copyPropertiesToBean(ann, bean, null, excludedProperties);
}
/**
* Copy the properties of the supplied {@link Annotation} to the supplied target bean.
* Any properties defined in {@code excludedProperties} will not be copied.
* <p>A specified value resolver may resolve placeholders in property values, for example.
* @param ann the annotation to copy from
* @param bean the bean instance to copy to
* @param valueResolver a resolve to post-process String property values (may be {@code null})
* @param excludedProperties the names of excluded properties, if any
* @see org.springframework.beans.BeanWrapper
*/
public static void copyPropertiesToBean(Annotation ann, Object bean, @Nullable StringValueResolver valueResolver,
String... excludedProperties) {
Set<String> excluded = new HashSet<>(Arrays.asList(excludedProperties));
Method[] annotationProperties = ann.annotationType().getDeclaredMethods(); //获取注解上的方法
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); //同过bean对象获取bean的定义BeanDefinition
for (Method annotationProperty : annotationProperties) { //遍历方法
String propertyName = annotationProperty.getName(); //获取方法名
if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) {
Object value = ReflectionUtils.invokeMethod(annotationProperty, ann); //获取注解方法上的值
if (valueResolver != null && value instanceof String) {
value = valueResolver.resolveStringValue((String) value); //处理value的值,StringValueResolver的作用比如处理占位符${}
}
bw.setPropertyValue(propertyName, value);//把该值设置到bean定义上。
}
}
}
}
从上面的解释,应该非常清楚该工具类的作用,现在我们来写一个例子来验证。
package com.qee.beans.annotation; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FooAnnotation {
String name(); int age();
}
package com.qee.beans.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Foo2Annotation {
String accountId();
}
package com.qee.beans.annotation; @FooAnnotation(name = "xiao ming", age = 23)
public class Foo {
private String names; private int age; @Foo2Annotation(accountId = "123456")
private String id; public String getNames() {
return names;
} public void setName(String names) {
this.names = names;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public void setAccountId(String id){
setId(id);
}
}
package com.qee.beans.annotation;
import org.springframework.beans.annotation.AnnotationBeanUtils;
public class AnnotationBeanUtilsTest {
public static void main(String[] args) throws NoSuchFieldException {
testCopyProperties();
testCopyProperties2();
testCopyPropertiesWithIgnore();
}
public static void testCopyProperties2() throws NoSuchFieldException {
Foo2Annotation annotation = Foo.class.getDeclaredField("id").getAnnotation(Foo2Annotation.class);
Foo foo = new Foo();
AnnotationBeanUtils.copyPropertiesToBean(annotation, foo);
System.out.println(foo.getId());
}
public static void testCopyProperties() {
FooAnnotation annotation = Foo.class.getAnnotation(FooAnnotation.class);
Foo foo = new Foo();
AnnotationBeanUtils.copyPropertiesToBean(annotation, foo);
System.out.println("Name : " + annotation.name() + " " + foo.getNames());
System.out.println("Age : " + annotation.age() + " " + foo.getAge());
}
public static void testCopyPropertiesWithIgnore() {
FooAnnotation annotation = Foo.class.getAnnotation(FooAnnotation.class);
Foo foo = new Foo();
foo.setName("Juergen Hoeller");
foo.setAge(30);
AnnotationBeanUtils.copyPropertiesToBean(annotation, foo, "name", "age");
System.out.println("Name : " + annotation.name() + " " + foo.getNames());
System.out.println("Age : " + annotation.age() + " " + foo.getAge());
}
}
从上面测试例子可以知道,AnnotationBeanUtils.copyPropertiesToBean把注解上的值拷贝给某个对象,只有某个对象有这个注解方法的setXX方法。并且如果该对象的某个属性已经有值了,就不会在拷贝注解上的值到该属性上。
从上面AnnotationBeanUtils的源码上,我们知道引入了新的Spring对象--StringValueResolver,BeanWrapper,ReflectionUtils。在之后的源码阅读中,在进行解析。在这里做一下红色标记,哇哈哈,哇哈哈,哇哈哈!!!!!!
待看的,等看看到这些包的时候,在继续分析下面的内容
org.springframework.util.StringValueResolver
org.springframework.beans.BeanWrapper
org.springframework.util.ReflectionUtils
Spring-Framework 源码阅读之AnnotationBeanUtils的更多相关文章
- Spring事务源码阅读笔记
1. 背景 本文主要介绍Spring声明式事务的实现原理及源码.对一些工作中的案例与事务源码中的参数进行总结. 2. 基本概念 2.1 基本名词解释 名词 概念 PlatformTransaction ...
- spring framework 源码
spring framework 各版本源码下载地址 现在spring的源码下载地址真是不好找,这次终于找到了.记录一下,以帮助需要的朋友. https://github.com/spring-pro ...
- Idea搭建spring framework源码环境
spring的源码目前放在github上,https://github.com/spring-projects/spring-framework 一.安装Git 二.安装Gradle gradle为解 ...
- Spring框架源码阅读之Springs-beans(一)容器的基本实现概述(待续)
去年通过实际框架代码的阅读,以及结合<Spring源码深度解析>和<Spring技术内幕>的阅读,对Spring框架内Bean模块有了一个整体性的认识.对此进行的总结性整理和回 ...
- Spring JdbcTemplate源码阅读报告
写在前面 spring一直以删繁就简为主旨,所以设计出非常流行的bean管理模式,简化了开发中的Bean的管理,少写了很多重复代码.而JdbcTemplate的设计更令人赞叹,轻量级,可做ORM也可如 ...
- 【面试】足够应付面试的Spring事务源码阅读梳理(建议珍藏)
Starting from a joke 问:把大象放冰箱里,分几步? 答:三步啊,第一.把冰箱门打开,第二.把大象放进去,第三.把冰箱门带上. 问:实现Spring事务,分几步? 答:三步啊,第一. ...
- Spring,tk-mapper源码阅读
Mybatis的源码学习(一): 前言: 结合spring本次学习会先从spring-mybatis开始分析 在学习mybatis之前,应该要对spring的bean有所了解,本文略过 先贴一下myb ...
- Spring Framework 源码编译导入
预先准备环境 Window 10 JDK环境 List item Gradle 以及其环境变量配置 spring-framework源码(https://gitee.com/mirrors/Sprin ...
- Robot Framework 源码阅读 day2 TestSuitBuilder
接上一篇 day1 run.py 发现build test suit还挺复杂的, 先从官网API找到了一些资料,可以看出这是robotframework进行组织 测试案例实现的重要步骤, 将传入的te ...
随机推荐
- 卷积神经网络的变种: PCANet
前言:昨天和大家聊了聊卷积神经网络,今天给大家带来一篇论文:pca+cnn=pcanet.现在就让我带领大家来了解这篇文章吧. 论文:PCANet:A Simple Deep Learning Bas ...
- Windows下彻底卸载删除SQL Serever2012
在安装了SQL Server2012之后,当由于某些原因我们需要卸载它时,我们应该怎么操作呢?相信这个问题困扰着不少人,博主经过亲身实践之后,给大家提供这样一种方法. 第一步.在控制面板里面找到程序— ...
- ORA-12638: 身份证明检索失败 解决方法
用PL/SQL或Navicat连接本地或远程Oracle数据库的时候报错:ORA-12638: 身份证明检索失败 解决方法: 开始 -> 所有程序 -> Oracle - Oracle_h ...
- jquery.qrcode.min.js(支持中文转化二维码)
详情请看:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/jqueryqrcodeminjs/ 今天还是要讲一下关于二维码的知识,前 ...
- JAVA类型擦除
Java泛型-类型擦除 一.概述 Java泛型在使用过程有诸多的问题,如不存在List<String>.class, List<Integer>不能赋值给List<Num ...
- [leetcode-581-Shortest Unsorted Continuous Subarray]
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- JAVA基础——Arrays工具类十大常用方法
Arrays工具类十大常用方法 原文链接:http://blog.csdn.net/renfufei/article/details/16829457 0. 声明数组 String[] aArray ...
- 如何使用mybatis对mysql数据库进行操作,batis的增删改查
1.先下载Mybatis和mysql connecrt的jar包 下载地址: 链接: https://pan.baidu.com/s/1kVFfF8N 密码: ypkb 导入jar包,maven的话可 ...
- ASP.NET Core Web 资源打包与压缩
本文将介绍使用的打包和压缩的优点,以及如何在ASP.NET Core应用程序中使用这些功能. 概述 在ASP.Net中可以使用打包与压缩这两种技术来提高Web应用程序页面加载的性能.通过减少从服务器请 ...
- Configure: error: freetype.h not found. 的解决办法
出现 Configure: error: freetype.h not found. 的解决办法 CentOS yum install freetype-devel Debian apt-get in ...