项目启动一般会报错某个bean加载报错,报错关键信息如下:
org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'resUseTimeConfigController': Unsatisfied dependency expressed through field 'resUseTimeConfigService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'resUseTimeConfigServiceImpl': Bean with name 'resUseTimeConfigServiceImpl' has been injected into other beans [resUseTimeConfigSupport] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
主要原因是因为两个bean相互依赖导致的,最简单的解决方案是加上@Lazy注解,在两个类的@Autowired下面再加上该注解即可。
//ClassB类里面依赖了ClassA
@Autowired
@Lazy//加注解
private ClassA classA;
//ClassA类里面依赖了ClassB
@Autowired
@Lazy//加注解
private ClassB classB;