测试的Springboot版本: 2.6.4,禁止了循环依赖,但是可以通过application.yml开启(哈哈)
@Lazy注解解决循环依赖
情况一:只有简单属性关系的循环依赖
涉及的Bean:
- ASerivce及其实现类ASerivceImpl
- BSerivce及其实现类BSerivceImpl
com.example.demo.service.AService
package com.example.demo.service;
public interface AService {
void zaWaLuDo();
}
com.example.demo.service.impl.AServiceImpl
package com.example.demo.service.impl;
import com.example.demo.service.AService;
import com.example.demo.service.BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AServiceImpl implements AService {
@Autowired
public BService bService;
@Override
public void zaWaLuDo(){
System.out.println("ToKiOToMaLei!");
}
}
com.example.demo.service.BService
package com.example.demo.service;
public interface BService {
}
com.example.demo.service.impl.BServiceImpl
package com.example.demo.service.impl;
import com.example.demo.service.AService;
import com.example.demo.service.BService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BServiceImpl implements BService {
@Autowired
public AService aService;
}
此时ASerivce和BService构成循环依赖的关系:
测试类:com.example.demo.service.AServiceTest
package com.example.demo.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AServiceTest {
@Autowired
AService aService;
@Test
public void test(){
aService.zaWaLuDo();
}
}
此时运行test方法,将会报错:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$8(ClassBasedTestDescriptor.java:363)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:368)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$9(ClassBasedTestDescriptor.java:363)
......省略.....
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AServiceImpl': Unsatisfied dependency expressed through field 'bService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'BServiceImpl': Unsatisfied dependency expressed through field 'aService'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'AServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
最重要的一句应该是:
美观处理过:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'AServiceImpl': Unsatisfied dependency expressed through field 'bService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'BServiceImpl': Unsatisfied dependency expressed through field 'aService';
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean w