问题记录-FeigonClient与HandlerInterceptorAdapter循环依赖-FeigonClient时间字段解析错误
(昨天天气晴朗,小区的路上暖洋洋的,路中央还趴着一只橘白猫,我凑近了点,它一点都不认生,甚至在地上打滚,露出肚皮,
这一定是一只宠物猫,可能跑丢了。我犹豫了下要不要蹲下摸摸它,它又翻过身来,舔了舔前爪,冲我喵喵叫了几声。我再细细看它,
体态健硕,毛发光泽很好,很健康,我想拿点什么喂喂它,可是家里好像什么吃的都没有,又站了几分钟,我无奈走开了,希望下一个路过的邻居会收养它吧)
最近遇到两个问题记录下:
一.FeigonClient与HandlerInterceptorAdapter循环依赖
遇到实际问题:
自己的类继承了HandlerInterceptorAdapter 类(A),过滤解析登录信息的,同时成员变量包含了FeigonClient接口类(B)获取登录信息,
B 初始化依赖 A( HandlerInterceptorAdapter ),同时A类依赖成员变量B类 ,循环依赖启动失败
网上查了不少方式,尝试了只有一种成功,记录下。
错误信息:
Error creating bean with name 'autoWOrderBusiness': Unsatisfied dependency expressed through field 'webAppApiRouteBusiness'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webAppApiRouteBusiness': Unsatisfied dependency expressed through field 'routePCFegionService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'routePCFegionService': Unsatisfied dependency expressed through field 'routePCFegionClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.t3.ts.cc.webapp.api.feign.autoworder.RouteFeignClient': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcConfiguration': Unsatisfied dependency expressed through field 'cuaCustomerFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.t3.ts.cc.webapp.api.feign.CuaCustomerFeignClient':FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mvcResourceUrlProvider': Requested bean is currently in creation: Is there an unresolvable circular reference?
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'mvcResourceUrlProvider': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:339)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:239)
at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:196)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:133)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:136)
at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:101)
at org.springframework.cloud.context.named.NamedContextFactory.getInstance(NamedContextFactory.java:145)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.get(FeignClientFactoryBean.java:220)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.feign(FeignClientFactoryBean.java:84)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:257)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:247)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171)
解决方式:
FegionClient不作为成员变量被使用,需要使用时 动态获取bean
@Component
public class SpringUtils implements ApplicationContextAware {
/**
* ddd
*/
private static ApplicationContext applicationContext;
/**
* sss
* @param applicationContext app
* @throws BeansException b
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
/**
* sss
* @param beanName b
* @param <T> t
* @return res
*/
public static <T> T getBean(String beanName) {
if (applicationContext.containsBean(beanName)) {
return (T) applicationContext.getBean(beanName);
} else {
return null;
}
}
/**
* dd
* @param baseType b
* @param <T> t
* @return res
*/
public static <T> Map<String, T> getBeansOfType(Class<T> baseType) {
return applicationContext.getBeansOfType(baseType);
}
}
CuaCustomerFeignClient cuaCustomerFeignClient = SpringUtils.getBean("cuaCustomerFeignClient");
FegionClient 里的注解方式,用 qualifier = "cuaCustomerFeignClient" 和通过名字获取bean的 getBean 方法呼应 :
@FeignClient(name = "cua-customer-api", url = "${cuaCustomerUrl}", qualifier = "cuaCustomerFeignClient")
public interface CuaCustomerFeignClient {
应该还可以深究一下:
二.FeigonClient时间字段解析错误
时间格式为”2019-06-21T10:09:06.000+0000“ 时在FegonClient和其他http客户端直接用 Date类接受数据会抛出异常,用String类可以,但是后续也需要转
或者用 T = fegionClient.getXX()
A = JSONObject.parseObject(JSONObject.toJSONString(T),A.class)
的方式 alibaba的json工具可以解析。
具体待研究原因。