Google

Locations of visitors to this page

Startup phases

Sometimes it is required to have some beans initialized before others. Spring offers the dependes-on mechanism, but it is not suitable for all scenarios.

ti-core provides a simple way to specify startup phases and configuration options to assign beans to startup phases.

The hello world example here is distribution of startup import beans. Using the startup phases, every module can define a set of beans that perform some data operations - like upgrading and prefilling the database - ensuring that these beans are initialized before other beans.

The configuration must be done in two steps. First, the phases must be defined, as schown in the configuration below:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tiCore="http://antidoto.sf.net/subsystem/ti-core"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
	http://antidoto.sf.net/subsystem/ti-core http://antidoto.sf.net/xsd/ti-core.xsd
	">
	
	<tiCore:startupPhasesConfiguration>
		<tiCore:startupPhase name="phase1" description="Check database structure, alter tables" />
		<tiCore:startupPhase name="phase2" description="Prefill database with minimal dataset" />
		<tiCore:startupPhase name="phase3" description="Some final checks" />
	</tiCore:startupPhasesConfiguration>
		
</beans>
			

The configuration above defines 3 phases in a well defined order: phase1, phase2, phase3. The phases are started in the same order as they are defined in the configuration element, the first starts first.

To assign a specific bean to configured startup phase, ti-core defines a <tiCore:startupPhaseSelector /> component, as shown in the following configuration sample:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tiCore="http://antidoto.sf.net/subsystem/ti-core"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
	http://antidoto.sf.net/subsystem/ti-core http://antidoto.sf.net/xsd/ti-core.xsd
	">
	
	<bean id="somepojo" class="de.pgt.ti.core.SomePojo">
		<tiCore:startupPhaseSelector startupPhase="phase2" />	
	</bean>
		
</beans>
			

As shown in the startup phases configuration above, it is possible to provide a description for each phase. If available, this description will be added to the logging statements as shown here:

13:14:51,572  INFO StartupPhase:65 - *********************************************
13:14:51,572  INFO StartupPhase:66 - ******* Startup phase: phase1 with 0 registered.
13:14:51,572  INFO StartupPhase:69 - ******* Description: Check database structure, alter tables
13:14:51,572  INFO StartupPhase:70 - *********************************************

(...)

13:14:51,572  INFO StartupPhase:65 - *********************************************
13:14:51,572  INFO StartupPhase:66 - ******* Startup phase: phase2 with 1 registered.
13:14:51,572  INFO StartupPhase:69 - ******* Description: Prefill database with minimal dataset
13:14:51,572  INFO StartupPhase:70 - *********************************************

(...)

13:14:51,572  INFO StartupPhase:74 - Startup phase2 triggering init for bean: somepojo