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概念: 面向切面编程,指扩 ...
随机推荐
- tensorflow 查看模型输入输出saved_model_cli show --dir ./xxxx --all
saved_model_cli show --dir ./xxxxxxxx --all 可以查看模型的输入输出,比如使用tensorflow export_model_inference.py 输出的 ...
- Windows内核驱动中操作文件
本页主题:如何在windows内核驱动中对文件操作,实现对文件的拷贝.粘贴.删除.查询信息等,这是很常用也是很简单的方法. 部分内容参考:http://www.cppblog.com/aurain/a ...
- mongo中用嵌套结构优势是什么
首先需要知道,MongoDB是NoSQL中的一种,是不直接支持Join的,这是NoSQL的一个特点,不需要直接支持Join,可以将横向扩展以及性能做到更好. 但是这不等于说MongoDB不能做Join ...
- 关于感受野 (Receptive field) 你该知道的事
Receptive field 可中译为“感受野”,是卷积神经网络中非常重要的概念之一. 我个人最早看到这个词的描述是在 2012 年 Krizhevsky 的 paper 中就有提到过,当时是各种不 ...
- hdu 3065 AC自动机 标记数组不清零
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目里面要我们计算每种单词出现的次数,重叠的也要计算,那么我们在查找的时候不要把标记单词结尾的 ...
- PCIE training
在PCIe链路可以正常工作之前,需要对PCIe链路进行链路训练,在这个过程中,就会用LTSSM状态机.LTSSM全称是Link Training and Status State Machine.这个 ...
- Java并发编程:深入剖析ThreadLocal(转)
目录大纲: 一.对ThreadLocal的理解 二.深入解析ThreadLocal类 三.ThreadLocal的应用场景 原文链接:http://www.cnblogs.com/dolphin052 ...
- RIDE安装操作
转载参考https://www.cnblogs.com/Ming8006/p/4998492.html 一.python安装 1.访问Python官网:https://www.python.org/ ...
- python基础 (序列化,os,sys,random,hashlib)
1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...
- Python基础与进阶
1 Python基础与进阶 欢迎来到Python世界 搭建编程环境 变量 | 字符串 | 注释 | 错误消除 他只用一张图,就把Python中的列表拿下了! 使用 If 语句进行条件测试 使用字典更准 ...