Results 1 to 2 of 2

Thread: when is bean instantiated from applicationContext.xml

  1. #1
    Join Date
    Sep 2010
    Posts
    28

    Default when is bean instantiated from applicationContext.xml

    Below is the sample applicationContext.xml

    Code:
    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:util="http://www.springframework.org/schema/util" 
    	xmlns:batch="http://www.springframework.org/schema/batch"
    	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    	<!-- 1) USE ANNOTATIONS TO CONFIGURE SPRING BEANS -->
    	<context:component-scan base-package="com.batch" />
    	<bean id="contextApplicationContextProvider" class="com.batch.fromdb.ApplicationContextProvider" />
    
    	<!-- 2) DATASOURCE, TRANSACTION MANAGER AND JDBC TEMPLATE  -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    			<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    			<property name="url" value="jdbc:sqlserver://NUSRV-DBDR\SQLINST1;DatabaseName=Nomura" />
    			<property name="username" value="Shashank" />
    			<property name="password" value="nuware@123" />
    			<property name="initialSize" value="3" />
    			<property name="maxActive" value="10" />
    		</bean>
    
    
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    
    	<!-- 3) JOB REPOSITORY - WE USE IN-MEMORY REPOSITORY FOR OUR EXAMPLE -->
    	<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    	</bean>
    
    	<!-- 4) LAUNCH JOBS FROM A REPOSITORY 
    	<bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<property name="jobRepository" ref="jobRepository" />
    	</bean>
    	-->
    
    	<!--
    		5) Define the job and its steps. In our case I use one step. Configure
    		its readers and writers
    	-->
    	<batch:job id="simpleJob1">
    			<batch:listeners>
    				<batch:listener ref="appJobExecutionListener" />
    			</batch:listeners>
    		<batch:step id="step1" >
    			<batch:tasklet>
    				<batch:chunk reader="cursorReader" writer="flatFileWriter1"	commit-interval="2" />
    				<batch:listeners>
    					<batch:listener ref="headerCallback" />
    					<batch:listener ref="footerCallback" />					
    				</batch:listeners>
    			</batch:tasklet>
    			
    		</batch:step>
    	</batch:job>
    	
    	
    	<batch:job id="simpleJob2">
    			<batch:listeners>
    				<batch:listener ref="appJobExecutionListener" />
    			</batch:listeners>
    		<batch:step id="step11">
    			<batch:tasklet >
    				<batch:chunk reader="cursorReader" writer="flatFileWriter2"	commit-interval="2" />
    				<batch:listeners>
    					<batch:listener ref="headerCallback" />
    					<batch:listener ref="footerCallback" />	
    				</batch:listeners>
    			</batch:tasklet>
    		</batch:step>
    	</batch:job>
    
    	<!-- ======================================================= -->
    	<!--  6) READER -->
    	<!-- ======================================================= -->
    	<bean id="cursorReader"	class="com.batch.fromdb.CustomReader" scope="step"/>
    
    	<!-- ======================================================= -->
    	<!--  7) WRITER -->
    	<!-- ======================================================= -->
    	<bean id="flatFileWriter1" class="com.batch.fromdb.CustomWriter" scope="step">
    		<property name="headerCallback" ref="headerCallback"/>
    		<property name="footerCallback" ref="footerCallback" />
    		<property name="resource" value="file:c:/tmp/simpleJob.txt"/>
    		<property name="lineAggregator">
    			<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
    				<property name="format" value="%-3.5s|%-1.2s"/>	
    				<property name="fieldExtractor">
    					<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
    						<property name="names" value="memberName,checkNumber" />
    					</bean>
    				</property>									
    			</bean>			
    		</property>
    	</bean>
    	
    	<bean id="flatFileWriter2" class="com.batch.fromdb.CustomWriter" scope="step">
    		<property name="headerCallback" ref="headerCallback"/>
    		<property name="footerCallback" ref="footerCallback" />
    		<property name="resource" value="file:c:/tmp/simpleJob111.txt"/>
    		<property name="lineAggregator">
    			<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
    				<property name="format" value="***  %-3.5s|%-20.20s  ***"/>	
    				<property name="fieldExtractor">
    					<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
    						<property name="names" value="memberName,checkNumber" />
    					</bean>
    				</property>									
    			</bean>			
    		</property>
    	</bean>
    	
    
    	
    	<!-- <bean id="flatFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    		<property name="resource" value="file:c:/tmp/ledgers.txt"/>
    		<property name="lineAggregator">
    			<bean class="org.springframework.batch.item.file.transform.FormatterLineAggregator">
    				<property name="format" value="%-3.5s|%-1.5s"/>	
    				<property name="fieldExtractor">
    					<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
    						<property name="names" value="memberName,checkNumber" />
    					</bean>
    				</property>									
    			</bean>			
    		</property>
    		<property name="headerCallback" ref="headerCallback" />
    		<property name="footerCallback" ref="footerCallback" />		
    	</bean>
    		-->
    	<bean id="headerCallback" class="com.batch.Header" />
    	<bean id="footerCallback" class="com.batch.Footer" />
    	
    	
    	<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
    		<property name="jobClass" value="com.batch.fromdb.FromDbLauncher" />
    		<property name="group" value="quartz-batch" />
    		<property name="jobDataAsMap">
    			<map>
    				<entry key="jobLocator" value-ref="jobRegistry" />
    				<entry key="jobLauncher" value-ref="jobLauncher" />
    			</map>
    		</property>
    	</bean>
    	
    	<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    		<property name="jobRegistry" ref="jobRegistry" />
    	</bean>
    	 
    	<bean id="scheduleFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
    	<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry" scope="singleton"/>
    
    	<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher" scope="singleton">
    		<property name="jobRepository" ref="jobRepository" />
    		<property name="taskExecutor" ref="taskExecutor"/>
    	</bean>
    	
    	<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" >
    		<property name="concurrencyLimit" value="6"/>
    	</bean>
    	
    <!--	<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ConcurrentTaskExecutor"/>-->
    <!--<bean id="taskExecutor" class="org.springframework.core.task.SyncTaskExecutor"/>-->
    
    </beans>
    I have few question regarding the instantiation of the beans.
    1. Is the bean instantiated only if we use it? or bean instances are created at the run-time when application Context is created ?
    2. The same question with batch:job. (will instance be created only if we use them)


    If we have unnecessary beans & jobs (XML definition in applicationContext.xml) will those be created even if not used... leading to performance issues?
    Last edited by kamath_sv; Jul 13th, 2011 at 04:46 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I suggest a read of the reference guide (chapter 3) as that explains about the context.

    1) Beans are instantiated by default (unless marked lazy, prototype or some custom scope which doesn't require eager instantiation for more info chapter 3 of the reference guide)

    2) See 1
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •