当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':