作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我们有一个原型(prototype)范围的 bean。
public class FooConfiguration {
@Bean
@Scope("prototype")
public Foo foo(@Autowired Bar bar) {
return new Foo(bar);
}
}
我们将这个 bean 注入(inject)到一个类 TheDependent
中。
@Component
public class TheDependent {
@Autowired
private Foo foo;
}
但是还有一个。
@Component
public class AnotherOne {
@Autowired
private Foo foo;
}
在每个 @Autowired
中,都会创建一个新的 Foo
实例,因为它使用了 @Scope("prototype")
注释。
我想从工厂方法 FooConfiguration#foo(Bar)
访问“依赖”类。可能吗? Spring 能否将某种context 对象注入(inject)到工厂方法的参数中,提供有关注入(inject)点的信息?
最佳答案
是的。您可以将 DefaultListableBeanFactory
注入(inject)到 bean 工厂方法的参数中,它是包含所有 bean 信息的 spring bean 容器:
@Bean
@Scope("prototype")
public Foo foo(@Autowired Bar bar , DefaultListableBeanFactory beanFactory) {
//Get all the name of the dependent bean of this bean
for(String dependentBean : beanFactory.getDependentBeans("foo")){
//Get the class of each dependent bean
beanFactory.getType(dependentBean);
}
return new Foo(bar);
}
关于java - 从 bean factory 访问 injectee 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944088/
假设我们有一个原型(prototype)范围的 bean。 public class FooConfiguration { @Bean @Scope("prototype") public
我是 Jersey 2 的新手。到目前为止,我使用 Jersey 1.x 和 Spring,并希望使用 HK2 实现。 看完tutorial我写了以下内容: @ManagedBean @Path("p
我是一名优秀的程序员,十分优秀!