springMvc统一异常处理
先配置处理异常的类,然后在分析源码
mvc统一处理异常的实现常见的异常如下,基本都是参数校验的异常。参数校验需要配合jsr303的注解校验哦。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167@Slf4j@RestControllerAdvice@Conditi ...
spring-spi
java有java的spidubbo有dubbo的spispring也有自己的spi
spring spi 入口类为org.springframework.core.io.support.SpringFactoriesLoader
springBoot main启动时就用到了
1234567891011121314151617181920212223public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { ... // Context initialize 监听器 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class)); // 所有的ApplicationListener setListeners((Collection) getSpringFactoriesInstances(Ap ...
springBoot容器启动流程
main方法启动时,springBoot启动流程的各个生命周期会以事件通知的方式,把事件告知其他程序前期通过spring-spi获取所有监听事件的类
spring启动的大体流程为以下的几个方法 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475public class EventPublishingRunListener implements SpringApplicationRunListener { ... private final SimpleApplicationEventMulticaster initialMulticaster = new SimpleApplicationEventMulticaster(); public EventPublishingRunListener(SpringAppli ...
获取spring启动环境的工具类
必须用到的枚举工具类
参考通用枚举的---使用例子一 通用枚举
定义ENV枚举1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859@Slf4jpublic enum ENV implements IEnums<String> { RELEASE("正式", "release", "prod"), PRE("预发", "pre"), TEST("测试", "test", "test1", "test2", "test3"), DEV("dev", "dev"), LOCAL("本地", "local&q ...
分析spring的Environment(配置文件)的加载
项目立项->开发->测试->维护->上线->维护,这几个过程中分为不同的环境。不同的环境不同业务有着不同的逻辑。spring完美支持启动的时候加载不同的配置文件。我们通过指定不同的spring.profiles.active即可实现加载不同的配置文件。不管怎么样默认会加载如下几个配置文件
1234567891011121314151617181920212223public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered { ... // Note the order is from least to most specific (last one wins) private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,f ...