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概念: 面向切面编程,指扩 ...
随机推荐
- MFC新建工程中目录包含中文,资源文件打开失败
※尽量不适用中文,各种未知错误,嘿嘿 此方法临时解决问题,可以使程序运行,后续是否还有错误是未知数 需要修改3处位置: 1.资源文件中.rc 右键,点击“查看代码”,找到带中文的资源ID,把中文修改掉 ...
- Linux localtime_r调用的一个小问题
我们一个项目中有如下代码: time_t loc_time; loc_time = time(NULL); localtime_r(&loc_time,&ptr); 这段代码本意是获取 ...
- MySql中SQL语句与其他数据库不一样的地方
目前发现的mysql与其他数据库如SqlServer.Oracle不同的地方 mysql中的注释(--)后要多加一个空格才生效 mysql中查询条件的字符串可以是双引号 mysql中查询条件的字符串不 ...
- C#使用File.Create()创建文件后资源被占用
由于文件被占用不能读写,所以报错“另一个程序正在使用此文件进程无法访问” 解决方法是在创建文件后立即Dispose掉 File.Create(path).Dispose();
- HC-05蓝牙模块配对步骤
参考:https://blog.csdn.net/m0_37182543/article/details/76383247
- TCP报文格式
转载自https://blog.csdn.net/mary19920410/article/details/58030147 1.TCP报文格式 TCP报头中的源端口号和目的端口号同IP数据报中的源I ...
- CentOS 6.8下网卡配置、桥接模式和NAT连接模式、VMware虚拟机克隆网卡配置
模式一:桥接模式: 1. 在VMware中安装好虚拟机后,虚拟机网卡设置:选择桥接模式 2. 查看本机的网络信息: 找到ip.子网掩码.网关.DNS等. 找一个没有使用的ip,例如:192.168.1 ...
- 从零开始学spring cloud(九) -------- 超时机制,断路器模式介绍
目前存在的问题: 现在我们假设一下,服务提供者响应非常缓慢,那么消费者对提供者的请求就会被强制等待,直到服务返回.在高负载场景下,如果不做任何处理,这种问题很可能造成所有处理用户请求的线程都被耗竭,而 ...
- 词根:lun = moon, 表示“月亮”
词根:lun = moon, 表示“月亮” lunar [lun月亮,-ar形容词后缀,…的] 月亮的,太阴的,似月的,新月形的 semilunar [semi-半,lun月亮,-ar形容词后缀,…的 ...
- wpf 加载资源文件
方法一:App.xaml页面上写 <Application x:Class="LanguageChange.App" xmlns="http://schemas.m ...