Spring中可以复用的工具类&特性记录
Spring 里有用工具类:
GenericTypeResolver 解析泛型类型。核心逻辑还是调用 ResolvableType
ResolvableType 解析泛型类型
BeanWrapper 利用反射修改 JavaBean 的属性值。(支持修改嵌套属性)
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中可以复用的工具类&特性记录的更多相关文章
- Spring中提供的集合工具类util CollectionUtils
转自:https://blog.csdn.net/fangwenzheng88/article/details/78457850 CollectionUtils类 /* * Copyright 200 ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...
- spring boot 结合Redis 实现工具类
自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...
- OpenJDK源码研究笔记(四)-编写和组织可复用的工具类和方法
本篇主要讲解java.util.Arrays这个针对数组的工具类. 1.可复用的工具类和方法. 这个工具类里,包含很多针对数组的工具方法,如 排序.交换.二分查找.比较.填充.复制.hashcode ...
- commons-collections包中的常用的工具类
commons-collections包中的常用的工具类 <dependency> <groupId>commons-collections</groupId> & ...
- Hutool中那些常用的工具类和方法
Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...
- spring项目中 通过自定义applicationContext工具类获取到applicationContext上下文对象
spring项目在服务器启动的时候 spring容器中就已经被创建好了各种对象,在我们需要使用的时候可以进行调用. 工具类代码如下 import org.springframework.beans.B ...
- 项目ITP(四) javaweb http json 交互 in action (服务端 spring 手机端 提供各种工具类)勿喷!
前言 系列文章:[传送门] 洗了个澡,准备写篇博客.然后看书了.时间 3 7 分.我慢慢规律生活,向目标靠近. 很喜欢珍惜时间像叮当猫一样 正文 慢慢地,二维码实现签到将要落幕了.下篇文章出二维码实 ...
随机推荐
- [开发笔记]-Linq to xml学习笔记
最近需要用到操作xml文档的方法,学习了一下linq to xml,特此记录. 测试代码: class Program { //参考: LINQ to XML 编程基础 - luckdv - 博客园 ...
- 私有IP地址共有三个范围段
在现在的网络中,IP地址分为公网IP和私有IP地址.公网IP是在Internet使用的IP地址,而私有IP地址是在局域网中使用的IP地址. 由于我们目前使用的IP V4协议的限制,现在IP地址的数量是 ...
- 配置nginx
咱不玩服务器,只在把人家的配置拷贝一份,建个自己的测试服务器 1. 如果nginx已配置(相当于windows在环境变量中配置了path吧) 查找nginx配置路径: whereis nginx 一般 ...
- python datetime unix时间戳以及字符串时间戳转换
将python的datetime转换为unix时间戳 import time import datetime dtime = datetime.datetime.now() ans_time = ti ...
- MATLAB——textscan
%读取带变量名的数据集的程序,寇文红2007年5月24日于畅春园. %这是一个读取数据集的程序,目的是把如下带变量名的数据矩阵读进MATLAB.% <DTYYYYMMDD>,<TIM ...
- Linux gcc编译之-std选项
用GCC编译代码时候后面带有-std=c++1z 的选项,这是指定c/c++的标准.具体的标准如下,详细信息可以看引用里面的详细说明
- java-selenium下载百度图片
package download; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStre ...
- SpagoBI 教程 Lesson 3: Highchart Dashboards
SpagoBI Lesson 3: Highchart Dashboards Business Intelligence dashboards Every car comes with a dash ...
- 锐捷 ac ap 连接 记录
需要用到锐捷的ac管理2台ap.记录一下. 参考文档 锐捷WLAN无线产品一本通(V6.0): http://www.ruijie.com.cn/fw/wd/58033 1.确认AC无线交换机和AP是 ...
- C++ 查询某个变量的类型
#include <typeinfo> int iii = 100; printf("%s\n",typeid(iii).name());//类型 详见:http:// ...