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概念: 面向切面编程,指扩 ...
随机推荐
- 上位机与三菱FX3U通过FX-232-BD通信
PLC侧设置: 和校验+协议4 读D200单字: 05 30 30 46 46 57 52 30 44 30 32 30 30 30 31 返回“201”:02 30 30 46 46 30 ...
- 大数据入门到精通17--union all 和disctinct 的用法
一.union all 的用法.使用union all 或者 unionselect * from rental where rental_id <10union allselect * fro ...
- 解题(DirGraCheckPath--有向图的遍历(深度搜索))
题目描述 对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径. 给定图中的两个结点的指针DirectedGraphNode* a, DirectedGraphNode* b(请不要在意数据类 ...
- C# 自制报表组件 EzReportBuild 2.0
组件无闪烁.画面流畅,效率一般,支持SQL和ACCESS两种.可以完成报表设计.预览.打印等功能,提供接口函数,可以将设计.预览等嵌入到自定的winform中调用,使用简单.每份报表可设置多页,每页可 ...
- JUnit源码分析 - 扩展 - 自定义RunListener
RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...
- gym 101982 B题 Coprime Integers
题目链接:https://codeforces.com/gym/101982/attachments 贴一张图吧: 题目意思就是给出四个数字,a,b,c,d,分别代表两个区间[a,b],[c,d],从 ...
- Apache和Nginx运行原理解析
Web服务器 Web服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务. 应用层使用HTTP协议. HTML文档格式. 浏览器统一资源定位器(URL). Web服 ...
- jsp3
普通传值: a1.jsp <form action="a2.jsp" method="post"> 用户名:<input type=" ...
- project5 大数据
[概念] build和run不是一样的,要看输出,不要误解. [方法论] 先要搞懂每个方法的使用,不能乱写.文件名不要写成字符串内容. 又忘记用lab code了,该死. programcreek是个 ...
- k8s中yaml文常见语法
在k8s中,所有的配置都是 json格式的.但为了读写方便,通常将这些配置写成yaml 格式,其运行的时候,还是会靠yaml引擎将其转化为json,apiserver 也仅接受json的数据类型. y ...