dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Dependency Cycle When Using Spring Boot +Dubbo + Spring Data JPA

Open lzf8888 opened this issue 8 years ago • 2 comments

在使用Spring Boot 1.5.x 版本+Spring Data JPA时,并使用Dubbot(2.5.6/8),如果在Service里注入Repository,就有机会触发Dependency Cycle error。 在上层代码中,如果autowire service的顺序不同,也会触发这个Denpendency Cycle,如: @Autowired private OrganizationService organizationService; @Autowired private CityDictionaryService cityDictionaryService; 不会报错,但是调换声明顺序就会。 如果删除Dubbo的Dependency,问题解决。

Dependency Cycle和EntityManager有关,而且很诡异,无论是自定义EntityManager还是Spring自动创建的都会报错。 报错信息:

Description:

The dependencies of some of the beans in the application context form a cycle:

testTravelController defined in file [省略TestTravelController.class] ↓ organizationServiceImpl defined in file [省略e\impl\OrganizationServiceImpl.class] ↓ departmentRepository ↓ (inner bean)#1e495414 ┌─────┐ | orderEntityManagerFactory ↑ ↓ | com.xxx.jtxt.oa.common.service.UserService ↑ ↓ | userServiceImpl defined in file [省略\impl\UserServiceImpl.class] ↑ ↓ | userRepositoryImpl ↑ ↓ | com.xxx.jtxt.oa.common.service.OrganizationService └─────┘

另:stackOverflow上也有人遇到了类似问题: https://stackoverflow.com/questions/41608705/the-dependencies-of-some-of-the-beans-in-the-application-context-form-a-cycle

lzf8888 avatar Dec 20 '17 05:12 lzf8888

Have you used Constructor Injection? Maybe this post can help you http://www.baeldung.com/circular-dependencies-in-spring

Do you use dubbo Annotation in your project? I suspect it's a Spring related problem, may have nothing to do with dubbo, but i am not sure yet. Can you provide a runnable demo so we can figure out the problem?

chickenlj avatar Dec 21 '17 03:12 chickenlj

Yes, the project used the constructor injection for test mock purpose. But, these classes have no dependency circle in logic! Please note that when Dubbo dependency removed, all the injection works well without any other changes! There are just normal services class with repository injected.

E.g, public ServiceAImpl{

@Autowired public ServiceAImpl(UserRepo A) .... }

public ServiceBImpl{

@Autowired public ServiceAImpl(DeptRepo A,CityRepo B) .... }

then the circle error happens...the circle relies on the entityMananger, it's obviously strange. Please test the Spring boot + dubbo +Spring data jpa, with the One Service class has two repo injected, then you will know what I mean.

lzf8888 avatar Dec 23 '17 14:12 lzf8888