我有一个名为 SpringUtil 的 bean,它是
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
public static <T> T getBean(String beanName) {
return (T) context.getBean(beanName);
}
...
}
还有另一个bean在其init方法中使用了SpringUtil,因此它需要在它之前将SpringUtil作为bean加载。我尝试了 @ConditionalOnBean(SpringUtil.class) 但它不起作用。有什么解决方法吗?或者从根本上讲,让 SpringUtil 在所有其他 bean 之前初始化。
最佳答案
您可以 Autowiring SpringUtil,Spring 将管理依赖关系:
@Autowired
private SpringUtil util;
另一种方法是 Autowiring setter :
@Autowired
private setSpringUtil(SpringUtil springUtil)
关于java - 如何强制在另一个 bean 之后创建一个 spring bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123823/