Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delay spring beans startup?

Tags:

spring

grails

Having spring application (actually grails app) that runs apache-activemq server as spring bean and couple of apache-camel routes. Application use hibernate to work with database. The problem is simple. Activemq+Camel starts up BEFORE grails injects special methods into hibernate domain objects (actually save/update methods etc). So, if activemq already has some data on startup - camel starts processing messages w/o having grails DAO methods injected. This fails with grails.lang.MissingMethodException. Must delay activemq/camel startup before Grails injects special methods into domain objects.

like image 987
Archer Avatar asked Aug 11 '10 16:08

Archer


People also ask

How do you delay a Spring bean?

If present and set to true, the @Bean or @Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. So inject BeanFactory in the bean responsible to get the token and use that factoryto initialize the bean after the token was retrieved.

How can you configured to lazily initialized the bean in Spring?

Setting the property value to true means that all the beans in the application will use lazy initialization. This configuration affects all the beans in the context. So, if we want to configure lazy initialization for a specific bean, we can do it through the @Lazy approach.

What is lazy initialization in Spring?

Lazy initialization can result in significantly reduced startup times as fewer classes are loaded and fewer beans are created during application startup. For example, a small web application that's using Actuator and Spring Security that normally starts in 2500ms will start in 2000ms with lazy initialization enabled.

Can there be singleton bean initialized lazily?

When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.


3 Answers

If all these are defined as spring bean, you can use

<bean id="activeMqBean" depends-on="anotherBean" />

This will make sure anotherBean is initialized before activeMqBean

like image 144
Bozho Avatar answered Nov 05 '22 10:11

Bozho


can you move MQ managment into a plugin? It would increase modularity and if you declare in plugin-descriptor

def loadAfter = ['hibernate']

you should have the desired behavior. Works for JBPM plugin

like image 44
Sammyrulez Avatar answered Nov 05 '22 10:11

Sammyrulez


I am not sure in your case but lazy loading may also help e.g.

<bean id="lazybean" class="com.xxx.YourBean" lazy-init="true">

A lazily-initialized bean indicates to the IoC container to create bean instance when it is first requested. This can help you delay the loading of beans you want.

like image 40
Gopi Avatar answered Nov 05 '22 09:11

Gopi