我遇到了Bean实例化排序很重要的问题。目前下面的Bean3正在运行基于数据库的缓存放置操作,Bean 1使用代理Bean2针对新创建的缓存进行查询。在Bean1被实例化之前,即在Spring容器出现之前,Bean3和Bean2的优先级被完全实例化。这些bean在单独的JARS中,Bean1引用到Bean1中不使用Autowired。相反,服务定位器正在给它一个参考。我们使用Spring 2.5.2,而不是使用XML来实例化bean。任何帮助感谢!Spring bean实例化排序
JAR1(Spring项目)
@Service ("bean3") public class Bean3 implements ApplicationListener { public void onApplicationEvent() { //load data from DB and populate cache } public void getCache(){ //get data from cache }
}
@Service ("bean2") public class Bean2 { @Autowired private Bean3 bean3; private void methodA(){ bean3.getCache(); } }
JAR2(非Spring项目)
public class Bean1{ Bean2 bean2 = SpringServiceLocator.getBean("bean2") public void methodB(){ bean2.methodA(); } }
您使用基于构造函数的自动装配吗? – Scott
我们在JAR1中启用了自动发现模式以进行自动装配。 – Noosphere
将Bean1依赖于Bean2(或创建一个依赖关系链)是否可行?)Bean1的方法何时被调用? –