UnsatisfiedDependencyException之spring迴圈依賴
阿新 • • 發佈:2018-12-08
當A和B的service互相呼叫的時候,容易引發迴圈依賴。這時候需要不使用注入。
package com.uplus.wei; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringContextUtils implements ApplicationContextAware { public static ApplicationContext applicationContext = null; @Override public void setApplicationContext( ApplicationContext applicationContext) throws BeansException { if (SpringContextUtils.applicationContext == null) { SpringContextUtils.applicationContext = applicationContext; } } // 獲取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } public static <T> T getBean( Class<T> clazz) { return getApplicationContext().getBean(clazz); } public static Object getBean( String name) { return getApplicationContext().getBean(name); } public static <T> T getBean( String name, Class<T> requiredType) { return getApplicationContext().getBean(name, requiredType); } public static boolean containsBean( String name) { return getApplicationContext().containsBean(name); } public static boolean isSingleton( String name) { return getApplicationContext().isSingleton(name); } public static Class<? extends Object> getType( String name) { return getApplicationContext().getType(name); } }
可以通過這個類來獲取其中一個service的類
WxMpUser weixinuser = SpringContextUtils.getBean(WxMpService.class).getUserService().userInfo(openId);
異常資訊
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'wechatMpConfiguration':