0

I can not understand the following documentation sentence

(...) the registration of new beans at runtime (concurrently with live access to the factory) is not officially supported and may lead to concurrent access exceptions, an inconsistent state in the bean container, or both.

I understand that it can't be injected at runtime, but previously we can read in the documentation:

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContext implementations also permit the registration of existing objects that are created outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods.

So how to implement this form of injection, not at the runtime. Now I'm confused.

João Dias
  • 16,277
  • 6
  • 33
  • 45
z Yiazzz
  • 1
  • 2
  • Can you share the link where you find this information? Thanks! – João Dias Oct 29 '21 at 19:10
  • [In addition to bean definitions that...](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition) – z Yiazzz Oct 30 '21 at 03:52
  • I believe this is relative to the dynamic registration of beans not following the regular way, e.g. [`GenericWebApplicationContext.registerBean()`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/GenericApplicationContext.html#registerBean-java.lang.Class-org.springframework.beans.factory.config.BeanDefinitionCustomizer...-). What is exactly that you want to do? – João Dias Oct 30 '21 at 13:40
  • I want to know when to inject bean, which will produce the exception described in the document. Because when I search for dynamic injection, Google is all about how to inject, and doesn't mention the exceptions mentioned in the document. Therefore, I just want to know under what circumstances there will be exceptions. Maybe I'm thinking a little too much. – z Yiazzz Oct 31 '21 at 08:13
  • Indeed you are. You usually don't dynamically register beans and even when you do, you do it using `GenericWebApplicationContext.registerBean()` and not the mentioned methods (which seems to me to be used for anything else than simply registering beans). – João Dias Oct 31 '21 at 13:35
  • Well, it was a wonderful experience. Thank you from the other side of the ocean for your patient explanation. – z Yiazzz Nov 01 '21 at 07:10

1 Answers1

0

I might not be the right person to answer this, but I would like to give a link where it has been answered beautifully, take a look might be the similar thing you asking.

bean injected dynamically

Adarsh Gupta
  • 121
  • 1
  • 5