SpringBoot中有一个ControllerAdvice的注解,使用该注解表示开启了全局异常的捕获,我们只需在自定义一个方法使用ExceptionHandler注解然后定义捕获异常的类型即可对这些捕获的异常进行统一的处理。
全局异常处理12345...
循环依赖循环依赖是指两个对象相互依赖,形成了一个环形的调用链路。比如下面的例子:
123456@Component public class A { @Autowired B b; } @Component public cla...
什么是AOPAOP (Aspect Orient Programming),直译过来就是 面向切面编程,是对面向对象编程的一种补充。AOP通过动态代理的方式,为系统提供某种通用的功能。
AOP的应用场景典型的AOP使用场景有:
日志记录
事务管...
在Java中的不变对象 提到了在生成不变对象时,如果类中有引用类型的应用,那么在初始化传入参数和返回引用属性时,会通过引用类型的clone()方法克隆一个新的对象,从而隔离属性的引用“逃逸”到不变对象之外,起到改变不变对象内部状态的结果。但是clon...
在开发过程中,我们经常会遇到“不可变对象”的需求,所谓不可变对象(Immutable Object),顾名思义就是:对象一旦被创建后,对象所有的状态及属性在其生命周期内不会发生任何变化。比如业务中有一个关于员工的定义:
123456789101112...
Java 8引入了一个Optional类,用来解决空指针异常(NullPointerException)。刚开始使用的时候看到ifPresent(),isPresent()和get()这些函数,感觉和直接通过比较null值的方式差别不大,纯属脱了裤子...
Java中通过关键字synchronized来对对象加锁,同时配合wait(),notify(),notifyAll() 来实现等待-通知机制。但是在实际使用时,容易混淆wait()和notifyAll()的含义,下面会详细讨论一下这两个方法的工作原...
类加载过程描述JVM 类加载过程分为,加载、链接、初始化、使用和卸载这五个阶段,在链接中又包括:验证、准备、解析。
加载:Java将字节码数据从不同的数据源读取到JVM中,并映射为JVM认可的数据结构(Class对象),这里的数据源可能是各种各样的...
运行时数据区
程序计数器程序计数器,可以看做是当前线程执行代码的行号指示器,字节码解释器通过改变程序计数器的值来选择下一条执行的语句。分支,循环,跳转,异常处理,线程恢复都是通过这个计数器来完成的。在多线程中,每个线程都有自己的计数器,这个区域是线程...
单例模式可能是最常用到的设计模式了,但是想要正确的使用单例模式却并不简单。我们先从最简单最常用的方式开始:
懒汉式12345678910public class Singleton { private static Singleton...