我有 2 个 bean 一个 bean 是 Singleton 另一个是原型(prototype)
@Scope(value="singleton")
@Component
public class A{
@Autowired
B b;
}
另一个 bean ,
@Scope(value="prototype")
@Component
public class B{
}
通过代码我可以分别得到 A 和 B 的 bean 范围,
bean.getClass().getAnnotations(Scope.class)
如何获取已在 A 中 Autowiring 的 Bean B 的范围。由于 A 是单例,当我尝试在 A 中获取 B bean 的范围时,它是否也应该是单例..
不知道如何通过代码得到它......
最佳答案
您可以将 BeanFactory 用于 BeanDefintion,它包含范围
@Autowired
ConfigurableWebApplicationContext applicationContext;
...
applicationContext.getBeanFactory().getBeanDefinition("beanName").getScope()
(getBeanFactory() 在 ConfigurableWebApplicationContext 中定义,这是一个由除 StubWebApplicationContext 之外的每个具体 ApplicationContext 实现的接口(interface))
关于java - 如何获取 Autowiring 的bean的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51408157/