Can @RefreshScope be impatient
New @RefreshScope
to Spring Cloud is great. But the side effect is that the beans become lazy loaded. Most of the time this is not a problem, but eagerly loaded beans allow DI, property value setting, etc. to be sure at startup time and not later at runtime. Is it possible to use @RefreshScope
and force loaded beans to load?
+3
source to share
1 answer
I solved this "problem" by implementing a listener that catches the Refresh event. Then I iterate over each component in the context and call the getClass () method. (Any other method call on the component will cause it to be created - getClass () is convenient since it exists for all components.)
@Service
public class RefreshListener {
private final Logger logger = LoggerFactory.getLogger(RefreshListener.class);
@Autowired
ApplicationContext applicationContext;
@EventListener
public void onRefreshScopeRefreshed(final RefreshScopeRefreshedEvent event) {
logger.info("Received Refresh event. Refreshing all beans...");
for (String beanName : applicationContext.getBeanDefinitionNames()) {
logger.info("Refreshing bean " + beanName);
applicationContext.getBean(beanName).getClass();
}
}
}
Hope it helps.
0
source to share