Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does having many unused beans in a Spring Bean Context waste significant resources?

Tags:

java

spring

My model layer is being used by a handful of different projects and I'd like to use a single XML Spring Configuration file for the model regardless of which project is using it.

My question is: Since not all beans are used in all projects am I wasting resources to any significant amount if there not being instantiated? I'm not too sure how lazy Spring is about loading them since it's never been an issue until now.

Any ideas?

like image 781
Allain Lalonde Avatar asked Sep 19 '08 00:09

Allain Lalonde


People also ask

Do Spring prototype beans need to be destroyed manually?

Therefore it's usually not necessary to explicitly destroy a prototype bean. However, in the case where a memory leak may occur as described above, prototype beans can be destroyed by creating a singleton bean post-processor whose destruction method explicitly calls the destruction hooks of your prototype beans.

What are beans in the concept of Spring beans?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

Why do we need Spring beans?

The bean is an important concept of the Spring Framework and as such, if you want to use Spring, it is fundamental to understand what it is and how it works. By definition, a Spring bean is an object that form the backbone of your application and that is managed by the Spring IoC container.


2 Answers

Taken from the Spring Reference Manual:

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process. Generally this is a good thing, because it means that any errors in the configuration or in the surrounding environment will be discovered immediately (as opposed to possibly hours or even days down the line).

However, there are times when this behavior is not what is wanted. If you do not want a singleton bean to be pre-instantiated when using an ApplicationContext, you can selectively control this by marking a bean definition as lazy-initialized. A lazily-initialized bean indicates to the IoC container whether or not a bean instance should be created at startup or when it is first requested.

When configuring beans via XML, this lazy loading is controlled by the 'lazy-init' attribute on the [bean element] ; for example:

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>

But, unless your beans are using up resources like file locks or database connections, I wouldn't worry too much about simple memory overhead if it is easier for you to have this one configuration for multiple (but different) profiles.

like image 60
MetroidFan2002 Avatar answered Nov 14 '22 08:11

MetroidFan2002


In addition to the other comments: it's also possible to specify a whole configuration file to be lazily initialized, by using the 'default-lazy-init' attribute on the <beans/> element; for example:

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>

This is much easier than adding the lazy-init attribute to every bean, if you have a lot of them.

like image 32
Tom De Leu Avatar answered Nov 14 '22 08:11

Tom De Leu