Spring核心
BeanFactoryPostProcessor与BeanPostProcessor使用 创建pc过程
https://www.liangzl.com/get-article-detail-8613.html
BeanFactory 与ApplicationContext 设计思想
https://www.chkui.com/article/spring/spring_core_context_and_ioc
非侵入式扩展功能
https://www.chkui.com/article/spring/spring_core_bean_post_processors
Spring在Bean Validation
@Configuration
@ComponentScan("chkui.springcore.example.hybrid.springvalidation.service")
public class SpringValidationConfig {
@Bean("validator")
public Validator validator() {
return new LocalValidatorFactoryBean();
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) {
MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
postProcessor.setValidator(validator);
return postProcessor;
}
}
//对方法进行参数校验
try {
PersonService personService = ctx.getBean(PersonService.class);
personService.execute(null, 1);//传递参数
} catch (ConstraintViolationException error) {
error.getConstraintViolations().forEach(err -> {//输出校验错误信息
print("Field: ", err.getPropertyPath());
print("Error: ", err.getMessage());
});
}
@Validated({Creation.class}) @RequestBody UserDTO userDTO
但是出现其他字段不执行验证的问题,找了一大圈,发现@Validated在分组验证时并没有添加Default.class的分组,而其他字段默认都是Default分组,所以需要让分组接口继承Default:
public interface Creation extends Default {
}
https://www.cnblogs.com/hujihon/p/5357481.html
PropertyEditor 字符串到实体转换
使用Spring的BeanWrapper接口,可以快速的将Properties数据结构转换为一个JavaBean实体。
https://www.chkui.com/article/spring/spring_core_string_to_entity
数据的类型转换
Spring的类型转换的基础是Converter<S, T>(以下简称转换器)接口
ConverterFactory<S, R>
ConversionService 为数据转换预设了大量的Converter 包含了几乎所有Java常规类型的数据格式转换
public abstract class Device {
public void pares(String text){ //字符串转换为实体
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
int begIndex = text.indexOf(field.getName());
int endIndex = text.indexOf(";", begIndex);
String sub = text.substring(begIndex, endIndex), value = sub.split("=")[1];
field.setAccessible(true);
field.set(this, value);
}
};
public String value(){ //实体转换为字符串
Field[] fields = this.getClass().getDeclaredFields();
StringBuilder sb = new StringBuilder();
for (Field field : fields) {
sb.append(field.getName());
sb.append("=");
sb.append(field.get(this).toString());
sb.append(";");
}
return sb.toString();
}
}
public interface ConverterFactory<S, R> {
<T extends R> Converter<S, T> getConverter(Class<T> targetType);
}
public class String2DeviceConverterFactory implements ConverterFactory<String, Device> {
public <T extends Device> Converter<String, T> getConverter(Class<T> targetType) {
return new String2DeviceConverter(targetType);
}
// Device的通用转换器
static class String2DeviceConverter<T extends Device> implements Converter<String, Device> {
private Class<? extends Device> klass;
public String2DeviceConverter(Class<? extends Device> klass) {
this.klass = klass;
}
public T convert(String source) {
Device device = null;
device = klass.newInstance();
device.pares(source);
return (T) device;
}
}
}
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConversionConfig.class);
// 获取ConversionService
ConversionService service = ctx.getBean(ConversionService.class);
// 字符串转换为整型
int i = service.convert("123456", Integer.class);
// 字符串转换为浮点
float f = service.convert("1234.56", Float.class);
// 源生列表转换为List
List<?> list = service.convert(new int[] { 1, 2, 3, 4, 5, 6 }, List.class);
// 源生列表转换为Set
Set<?> set = service.convert(new int[] { 1, 2, 3, 4, 5, 6 }, Set.class);
// 枚举转换
Gender gender = service.convert("Male", Gender.class);
// 使用自定义转换器
PC pc = service.convert("cpu=amd;ram=kingston;graphic=Navidia;", PC.class);
// UUID转换
UUID uuid = service.convert("f51b4b95-0925-4ad0-8c62-4daf3ea7918f", UUID.class);
// 字符串转换为Optional<PC>
Optional<PC> options = service.convert("cpu=amd;ram=kingston;graphic=Navidia;", Optional.class);
// 使用TypeDescriptor描述进行转换
String source = "123456789";
int result = (int) service.convert(source, TypeDescriptor.valueOf(source.getClass()),
TypeDescriptor.valueOf(Integer.class));
_G.print(result);
}
enum Gender {
Male, Female, Other
}
https://www.chkui.com/article/spring/spring_core_type_conversion
https://www.chkui.com/article/spring/spring_core_data_validation
https://www.chkui.com/article/java/java_bean_validation
https://www.chkui.com/article/spring/spring_core_jsr250_and_resource
Spring核心的更多相关文章
- 第一章 spring核心概念
一.Spring作用:管理项目中各种业务Bean(service类.Dao类.Action类),实例化类,属性赋值 二.Spring IOC(Inversion of Control )控制反转,也被 ...
- SSM 五:Spring核心概念
第五章:Spring核心概念 一.Spring Ioc 优点: 1.低侵入式设计 2.独立于各种应用服务器 3.依赖注入特性将组建关系透明化,降低耦合度 4.面向切面编程的特性允许将通用性任务集中式处 ...
- Spring核心——Bean的定义与控制
在Sring核心与设计模式的文章中,分别介绍了Ioc容器和Bean的依赖关系.如果阅读过前2文就会知道,Spring的整个运转机制就是围绕着IoC容器以及Bean展开的.IoC就是一个篮子,所有的Be ...
- spring 核心
1 Spring 1.1 专业术语了解 1.1.1 组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架 非侵入式设计 引入了 ...
- Spring核心简介
Spring简介 Spring是一个开源.轻量级框架.在诞生之初,创建Spring的主要目的是用来替代更加重量级的企业级Java技术,尤其是EJB(Enterprise JavaBean).从最初的挑 ...
- spring核心容器
容器:用来包装或装载物品的储存器 web服务器与jsp.servlet的关系: 从程序文件存放的位置 程序文件要放到web服务器上 从程序执行的方式 程序的从初始化到消亡都是web服务器管理的 从以 ...
- Spring核心思想:“控制反转”,也叫“依赖注入” 的理解
@Service对应的是业务层Bean,例如: @Service("userService") public class UserServiceImpl implements Us ...
- 初识Spring——Spring核心容器
一. IOC和DI基础 IOC-Inversion of Control,译为控制反转,是一种遵循依赖倒置原则的代码设计思想. 所谓依赖倒置,就是把原本的高层建筑依赖底层建筑“倒置”过来,变成底层建筑 ...
- 一头扎进Spring之---------Spring核心容器----------
1.什么是 IOC/DI? IOC(Inversion of Control)控制反转:所谓控制反转,就是把原先我们代码里面需要实现的对象创建.依赖的代码,反转给容器来帮忙实现.那么必然的我们需要创建 ...
- Spring核心AOP(面向切面编程)总结
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/75208354冷血之心的博客) 1.AOP概念: 面向切面编程,指扩 ...
随机推荐
- Dapper查询返回Datatable
dapper封装的扩展方法中,没有直接返回datatable的方法,项目中有些时候需要用到这样的返回格式,而为了项目数据框架的统一性, 不好直接用其他框架,如果直接将查询出来的泛型集合转datatab ...
- Maven工程控制台乱码问题(IDEA)
转自https://blog.csdn.net/love_live2/article/details/79311978 个人推荐使用文章中介绍的第三种方法,感觉挺好用的
- PCA降维参数介绍
https://www.cnblogs.com/pinard/p/6243025.html#undefined
- jemter聚合报告参数指标
1.聚合报告指标含义 2.性能指标的名称与含义 1)并发: 所有用户在同一时刻对系统执行操作,一般指做同一件事情或操作.2)在线: 所有用户在一段时间内对系统执行操作.3) ...
- nginx新增tcp模板
最近在装nginx时,发现新增了tcp模板,装了一遍,现记录下来过程. 1.下载nginx源码包,并解压 2.下载tcp模板压缩包https://github.com/yaoweibin/nginx_ ...
- springboot使用Redis,监听Redis键过期的事件设置与使用代码
我使用的是Windows下的Redis服务,所以一下Redis设置都是在Windows平台进行. 1.修改Redis配置文件 1.1:Windows下的Redis存在两个配置文件 修改带有servic ...
- EL表达式、JSTL标签库
一.EL(Expression Language)表达式 语法结构:${var} 若要停用对EL表达式的评估的话,需要使用page指令将isELIgnored属性值设为true: <%@ pag ...
- 探索未知种族之osg类生物---渲染遍历之认识SceneView
前言 我们在进行osg程序的开发时,最常用到的场景管理方式是“场景节点树”的结构, a 场景树底端的叶节点(osg::Geode)包含了各种需要渲染的几何体的顶点和渲染状态信息: b ...
- SqlServer数据库链接字符串
完整链接字符串: 1."DataSourse=.\你的实例;Initial Catalog=yourdatabase;User ID=*;Password=*;Trusted_Connect ...
- QTcpSocket 相关知识总结
1. 连接服务器 m_tcpSocket->connectToHost("127.0.0.1", 9877); connected = m_tcpSocket->wa ...