如何获得spring bean的依赖?

时间:2014-05-20 04:23:58

标签: java spring java-ee spring-mvc

我正在尝试使用以下代码:

ConfigurableListableBeanFactory factory = ((AbstractRefreshableApplicationContext) applicationContext)
            .getBeanFactory();
factory.getDependenciesForBean(beanName)

但我面临这个错误。

java.lang.ClassCastException: org.springframework.context.support.GenericApplicationContext cannot be cast to org.springframework.context.support.AbstractRefreshableApplicationContext

如果你遇到这个问题,请提供宝贵意见,我该怎样办法?

1 个答案:

答案 0 :(得分:2)

这是有效的

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
ConfigurableListableBeanFactory bf = ctx.getBeanFactory();
bf.getDependenciesForBean(beanName)