Spring 里有用工具类:

GenericTypeResolver 解析泛型类型。核心逻辑还是调用 ResolvableType
ResolvableType 解析泛型类型

BeanWrapper 利用反射修改 JavaBean 的属性值。(支持修改嵌套属性)

  dubbo 中的 com.alibaba.dubbo.common.bytecode.Wrapper 实现了类似的功能
public class Foo {
private int id;
private String name;
......
} public static void main(String[] args) {
Foo foo = new Foo(); // org.springframework.beans.BeanWrapper给对象属性赋值
BeanWrapper bw = new BeanWrapperImpl(foo);
bw.setPropertyValue("id", 123); // 给 Foo的id属性赋值 // com.alibaba.dubbo.common.bytecode.Wrapper给对象属性赋值
Wrapper.getWrapper(Foo.class).setPropertyValue(foo, "id", 123); // 给 Foo的id属性赋值
System.out.println(JSON.toJSONString(foo));
}

IntrospectionSupport 设置 JavaBean 属性。 IntrospectionSupport.setProperties(Object target, Map<String, ?> props, String optionPrefix)

org.springframework.core.Constants : 处理 public static final 字段

org.springframework.util.ReflectionUtils :反射相关
org.springframework.util.ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);

取属性值:

@Autowired
private Environment env;  
env.getProperty("spring.redis.pool.max-idle")

获取资源文件:

ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
Resource[] source = resourceLoader.getResources(filePath); // 支持ant风格的路径匹配(?、*、**)

流处理:

org.springframework.util.StreamUtils  (流拷贝、从流中读取数据.....)

通用常量:

org.springframework.http.MediaType
如:APPLICATION_JSON_UTF8_VALUE --> application/json;charset=UTF-8

org.springframework.http.HttpStatus
如:OK(200, "OK")

Junit自动回滚:

@Test
@Rollback
@Transactional
public void testRollback(){
xxxService.insert(instance);
}

-----------------------------------------------------技巧----------------------------------------------------

1. 找一个带命名空间(namespace)配置的处理类,可以从 NamespaceHandler 接口入手,找实现类

例如: <context:component-scan base-package="com.kvn.xx" />
可以从 NamespaceHandler 的实现类中找到 ContextNamespaceHandler

public class ContextNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
registerBeanDefinitionParser("mbean-export", new MBeanExportBeanDefinitionParser());
registerBeanDefinitionParser("mbean-server", new MBeanServerBeanDefinitionParser());
}
}

然后就能找到 component-scan 的处理类为: ComponentScanBeanDefinitionParser

2. springMvc 使用时,如果代码有进行地址重定向(sendRedirect),可以从下面步骤入手(判断是否服务器重定向&&相应的 Controller)

断点打在 DispatcherServlet#doDispatch() ,观察 request 和 response

查看图片

Spring中可以复用的工具类&特性记录的更多相关文章

  1. Spring中提供的集合工具类util CollectionUtils

    转自:https://blog.csdn.net/fangwenzheng88/article/details/78457850 CollectionUtils类 /* * Copyright 200 ...

  2. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  3. 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)

    前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...

  4. spring boot 结合Redis 实现工具类

    自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...

  5. OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法

    本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法.  这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...

  6. commons-collections包中的常用的工具类

    commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...

  7. Hutool中那些常用的工具类和方法

    Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...

  8. spring项目中 通过自定义applicationContext工具类获取到applicationContext上下文对象

    spring项目在服务器启动的时候 spring容器中就已经被创建好了各种对象,在我们需要使用的时候可以进行调用. 工具类代码如下 import org.springframework.beans.B ...

  9. 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!

    前言 系列文章:[传送门] 洗了个澡,准备写篇博客.然后看书了.时间 3 7 分.我慢慢规律生活,向目标靠近.  很喜欢珍惜时间像叮当猫一样 正文 慢慢地,二维码实现签到将要落幕了.下篇文章出二维码实 ...

随机推荐

  1. Java8中list转map

    第一种: 取list中某2个字段作为Map的K,V public Map<Long, String> getIdNameMap(List<Account> accounts) ...

  2. 【C】——extern

    直接上例子: 1.c #include<stdio.h> int main() { extern int a; a += ; printf("%d\n",a); tex ...

  3. Android Studio xcode单步调试 WebRTC Android & iOS

    mac环境 如何在 Android Studio 里单步调试 WebRTC Android 的 native 代码. WebRTC 代码下载 depot tools 是 chromium 代码库管理工 ...

  4. 微信中关闭网页输入内容时的安全提示 [干掉 “防盗号或诈骗,请不要输入QQ密码”]

    未设置之前: 需要把域名加入白名单 设置方法:微信公共平台后台-->公众号设置--->功能设置--->填写业务域名即可.

  5. hashMap 临界值初步理解

    import java.util.*; public class Bs { //Integer.highestOneBit((number - 1) << 1)分解 public stat ...

  6. ResNets和Inception的理解

    ResNets和Inception的理解 ResNet解析

  7. 使用Photoshop合成两张不完整的图片

    一.准备工作 软件环境:PhotoshopCS6 目标:将两张不完整的图片合并成一张完整的图片. 二.操作步骤 1,新建一张画布,参数:15*12厘米,像素300. 2,对第一张不完整的图片选择魔棒工 ...

  8. CodeCombat地牢关卡Python代码

    最近迷上了玩CodeCombat,特将地牢关卡的Python代码整理如下,供有兴趣的人学习交流探讨 1,Kithgard地牢 hero.moveRight() hero.moveDown() hero ...

  9. Mybatis系列(三):Mybatis实现关联表查询

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4264440.html 一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 ...

  10. 新版本IntelliJ IDEA 构建maven,并用Maven创建一个web项目

    之前都没试过用maven来管理过项目,但是手动找包导包确实不方便,于是今天用2016版的IDEA进行了maven的初尝试. 打开IDEA,创建新项目: 然后选择Maven,以及选择自己电脑的jdk: ...