Предположим, у меня есть ленивый управляемый Spring bean-компонент MyBean в такой пользовательской области:
Код: Выделить всё
@Configuration
public class MyConfiguration {
@Scope("custom")
@Lazy
@Bean
MyBean myBean () {
return new MyBean();
}
}
Код: Выделить всё
@Component
class MyBeanCounter{
void checkIfMyBeanIsInstantiated () {
// Check if there is an instance of MyBean within Spring context
}
}
< br />
Очевидная идея — внедрить MyBean следующим образом:
Код: Выделить всё
@Component
class MyBeanCounter {
@Autowired
MyBean myBean;
void checkIfMyBeanIsInstantiated () {
if (myBean != null) {// this doesn't trigger the bean creation
// there is an instance
}
}
}
The above solution doesn't work because @Autowired MyBean myBean; does instantiate the bean. Replacing it with @Lazy @Autowired MyBean myBean; still doesn't work since I end up with an injected proxy.
Is there any solution?
Источник: https://stackoverflow.com/questions/622 ... stantiated
Мобильная версия