Describe the bug
I had a custom interceptor,and i tried to put a feign service on both interceptor and spring service,but it make a mistaken,my appliction startup fail, about The dependencies of some of the beans in the application context form a cycle.
Sample My interceptor.
@Slf4j
public class MultitenancyWebInterceptor extends MultitenancyWebInterceptorAdapter {
private long lastStamp = -1L;
@Autowired
private RedisHelper redisHelper;
@Autowired
private FeignService feignService;
...
}
My default configuration.
@Configuration
public class DefaultConfiguration {
@Bean
@ConditionalOnMissingBean
public MultitenancyWebInterceptorAdapter multitenancyWebInterceptorAdapter() {
return new MultitenancyWebInterceptor();
}
}
My web configuration.
@Configuration
public class MultitenancyWebInterceptorConfig implements WebMvcConfigurer {
@Autowired
private MultitenancyWebInterceptorAdapter multitenancyWebInterceptorAdapter;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(multitenancyWebInterceptorAdapter).addPathPatterns("/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
My spring service.
@Service
public class UserServiceImpl implements UserService {
@Autowired
private FeignService feignService;
...
}
The application log.
userAdminContorller (field private a.service.UserService a.controller.admin.UserAdminContorller.userService)
↓
userServiceImpl (field private b.service.FeignService a.service.impl.UserServiceImpl.feignService)
┌─────┐
| b.service.feignService
↑ ↓
| org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑ ↓
| multitenancyWebInterceptorConfig (field private b.assembly.interceptor.MultitenancyWebInterceptorAdapter b.property.config.MultitenancyWebInterceptorConfig.multitenancyWebInterceptorAdapter)
↑ ↓
| multitenancyWebInterceptorAdapter (field private b.service.FeignService b.assembly.interceptor.MultitenancyWebInterceptor.feignService)
↑ ↓
| mvcResourceUrlProvider
└─────┘
I tried to get the bean using ApplicationContext in MultitenancyWebInterceptor, application can start, but the FeignService is null.
I tried to put the feign service in MultitenancyWebInterceptor only,or put the feign service in UserServiceImpl only,it can work, but i want to use both of them.
Comment From: zero3h
I got a test that when i move b.service.FeignService to a.service.FeignService, it can work.Buit i want to put it in different package.I just want to define the feign service as base service.
Comment From: spencergibb
Inject an ObjectProvider<FeignClient>
Comment From: zero3h
It work!!! Thank you sooo much.I thank i got a knowledge of the new characteristics for spring 4.3.
Like this:
@Slf4j
public class MultitenancyWebInterceptor extends MultitenancyWebInterceptorAdapter {
@Autowired
private ObjectProvider<FeignService> objectProvider;
@Override
public boolean way {
...
objectProvider.getIfUnique().xxx();
...
}
}
Comment From: nuvan
Hi all. Sorry for digging this ticket again but what's the difference between an ObjectProvider vs using @Lazy when Autowiring the field in this context?