在springboot中:没有在spring容器中的类想调用spring容器中的类的方法:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* <p>获取java bean的工具</p>
*
* @author xqh
* @create 2021/3/11 10:25
*/
@Component
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
/**
* @param beanName
* @return
*/
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
}
调用的方式:RestTemplate = ApplicationContextUtils.getBean("restTemplate");