Is it possible to prevent a bean from being created? - spring-boot

I have an external dependency that instantiates a bean having a #Value property. Of course, if the property is not present, the application will crash (no default value).
Would it be possible that, under a certain profile, the bean is not created at all? Or inject at runtime the property to prevent the crash?

Related

How to provide custom logic to search for bean if it cannot be found in Spring Context

If the Spring context cannot find a bean referred to from my xml I want to be able to provide some custom logic (ie look in another Spring Context or create the bean programmatically) before a BeanNotFoundException (whatever the expection is) is thrown.
You can use the BeanFactoryPostProcessor to define your beans dynamically (click on the link for an explanation).
Alternatively if you want to act on your bean in a lazy manner (instead of at app startup), which I strongly recommend not to do since you will have bugs related to the state of you beans config that is not visible at compile time: Anyway, you can surely add to your ApplicationContext a custom singleton : instead of calling getBean() from spring, if nothing was found, your class will be called to respond which bean corresponds to a bean name, and you will indirectly have the opportunity to handle BeanNotFoundException
See a post about the second (not recommended) solution here : How to add Properties to an Application Context

Global application context/environment runtime properties

There are some properties which are not read from a configuration file but are acquired from some remote resources and are constant, for example, user ID from a database. They are global in app context (it means that each application context has same properties with different values).
I expected something like context.setProperty or context.setGlobalProperty or context.getEnvironment().setProperty, but I couldn't find such methods.
My current solution is to create a singleton lazy bean which is initialized during application initialization and then it can be autowired into any bean. But it seems to me that this is not the best method.
It's not a good idea to change environment during runtime.
Environment is also bean in context, so your solution is ok.
Also you can use static container with dynamic variables and fill it during runtime

Spring PropertyPlaceholderConfigurer loads property files during startup?

Anyone know, if Spring PropertyPlaceholderConfigurer loads property files during app startup?
If so, does it do by default or we have to set some kind of parameter to make sure it loads during app startup
PropertyPlaceholderConfigurer is a bean factory postprocessor. A bean post processor works with the bean factory before any bean has bean instantiated (other than other bean factory postprocessors).It alters the bean factory.
Therefore it always runs at application startup to resolve property values delimited with ${}. It will never execute again in the lifecycle of the Spring application.

Spring Bean ID Scope

I have multiple spring configuration files, where each defines beans for a different implementation of an interface. Therefore the contents are similar, but not identical.
Each contains a bean that, through its <constructor-arg> references another bean defined in the file. This referenced bean exists in all of the config files with the same name. My IDE (IntelliJ) prompts me as to which version of the bean I want to use (from which config file) but it seems to get a bit confused when I ctrl click the reference.
So I want to clarify the scope of how these config files are resolved - does spring always look for the bean definition inside the same file first?
During the spring bean initialization phase, all bean definitions are loaded first before anything else happens. Bean instantiation and dependency injection are done in later step.Therefore it should not matter to which configuration file was each bean defined.

Spring FactoryBean used before it is configured?

I have two FactoryBeans creating proxies for existing beans in the application context.
FactoryBeanA.getObject() is invoked as part of the singleton pre-instantiation, and it attempts to autowire the returned instance.
This autowiring needs a bean that is defined by FactoryBeanB, which has not yet been configured (had properties injected).
Can this be controlled in such a way, that I am sure both FactoryBeans are fully configured (properties injected) before any beans are attempted instantiated?
Edit:
Autowiring from FactoryBeanA objects have worked fine until I changed FactoryBeanB to require a property to be injected. After this change, I see autowiring for the A-bean try to invoke FactoryBeanB.getObject(), but this fails as properties has not yet been injected.
Problem was actually caused by my own mistake. FactoryBeanB was not configured properly as I thought.

Resources