Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override default-lazy-init=true for Spring bean definitions

Tags:

java

spring

I am maintaining a large Java EE system. Most of the business logic is converted from EJB:s into POJO:s configured in several spring context configuration files. EJB:s are mostly used as Facades, that looks up the business logic spring beans from a context composed of all spring context configuration files mentioned earlier. For this we use the AbstractStatelessSessionBean provided with the spring framework.

All these configuration files have the default-lazy-init=true directive, which means that the business logic beans are not created until they are actually used by the system. This is preferable most of the time since republishing in developer mode becomes faster.

But when large merges are made, we are having problems finding all the configuration errors, such as missing dependencies.

My idea is to write some form of integration test, with the purpose of finding those errors. This means, i think, that I need to find a way to override all default-lazy-init=true declarations when creating the application context.

Is there any way of doing this programmatically, or perhaps with some test-only context file that includes all the actual context files?

like image 524
Jon Avatar asked Feb 29 '12 13:02

Jon


People also ask

What is default lazy-init in Spring?

By default in Spring, all the defined beans, and their dependencies, are created when the application context is created. In contrast, when we configure a bean with lazy initialization, the bean will only be created, and its dependencies injected, once they're needed.

Which one is true about lazy initialization of beans?

The lazy-init="default" setting on a bean only refers to what is set by the default-lazy-init attribute of the enclosing beans element. The implicit default value of default-lazy-init is false . If there is no lazy-init attribute specified on a bean, it's always eagerly instantiated.

Which will enable lazy initialization of Spring bean?

To enable lazy loading for specific beans, use lazy-init=”true” attribute on bean definitions in bean configuration xml files.

Are beans in Spring eagerly initialized by default?

Overview. By default, Spring creates all singleton beans eagerly at the startup/bootstrapping of the application context. The reason behind this is simple: to avoid and detect all possible errors immediately rather than at runtime.


1 Answers

Let's say currently you have a single applicationContext.xml file containing all bean definitions:

<beans default-lazy-init="true">

    <!-- all your beans -->

</beans>

Rename it to applicationContext-main.xml or something and remove default-lazy-init="true" attribute. Now create two applicationContext.xml files:

<beans default-lazy-init="true">

    <import resource="applicationContext-core.xml"/>

</beans>

and:

<beans default-lazy-init="false">

    <import resource="applicationContext-core.xml"/>

</beans>

As you can see the only difference is the value of default-lazy-init. During development your team can use the former version of applicationContext.xml that includes all the beans with lazy-init. On staging and testing environments switch it to the latter so that all beans included in applicationContext-core.xml are created eagerly.

like image 54
Tomasz Nurkiewicz Avatar answered Sep 18 '22 14:09

Tomasz Nurkiewicz