Announcement Announcement Module
Collapse
No announcement yet.
DataSourceInitializer#0': Cannot resolve reference to bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • DataSourceInitializer#0': Cannot resolve reference to bean

    I have implemented a simple Spring Batch app based on one of the tutorials on the web. Using the Spring Template Project I created a Batch Project. I have it working as expected, and now I am integrating that work into an existing Spring Project. When I call the launch-context1.xml file it complains about not finding the dataSource. I have searched long and far for resolutions with no success and would like some guidance. Is it a configuration issue tied to the jdbc settings? I have hit a wall on this and I need to understand how to resolve this in order to implement a database loader app.

    The error I am getting is:

    Code:
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
    MyClass.java
    Code:
        public class MyClass {
    
         ...
    
        @Autowired
        private static JobLauncher jobLauncher;
     
        @Autowired
        //   @Qualifier(value = "job")
        private static Job job;
       
        public static void main( String[] args ) throws JobExecutionAlreadyRunningException, JobRestartException,     JobInstanceAlreadyCompleteException, JobParametersInvalidException
        {
             ApplicationContext context = 
            		new ClassPathXmlApplicationContext("launch-context1.xml");
             
             JobExecution jobExecution = jobLauncher.run(job,
                     new JobParameters());
            System.out.println("Job ID: " + jobExecution.getJobId());
        }
    }
    launch-context1.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:batch="http://www.springframework.org/schema/batch"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<context:property-placeholder location="classpath:batch.properties" />
    
    	<context:component-scan base-package="com.cip.batch" />
    
    	<jdbc:initialize-database data-source="dataSource">
    		<jdbc:script location="${batch.schema.script}" />
    	</jdbc:initialize-database>
    
    	<batch:job-repository id="jobRepository" />
    
    	<import resource="classpath:/META-INF/spring/module-context1.xml" />
    
    
    
    </beans>
    module-context1.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:batch="http://www.springframework.org/schema/batch"
    	xsi:schemaLocation="
    	http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
    	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    	<description>Example job to get you started. It provides a skeleton for a typical batch application.</description>
     	
    	<batch:job id="job">
    	<batch:step id="job1">
    	<batch:tasklet transaction-manager="transactionManager">
    	<batch:chunk reader="fileReader"
    	                writer="fileWriter" commit-interval="10" />
    	        </batch:tasklet>
    	    </batch:step>
    	</batch:job>
    	
    	<bean name="fileReader"
    	    class="org.springframework.batch.item.file.FlatFileItemReader">
     	    <property name="resource" value="file:data/test.txt" /> 
    	    <property name="lineMapper" ref="fileLineMapper" />
    	</bean>
    	
    	<bean name="fileLineMapper"
        class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="fieldSetMapper" ref="fileMapper" />
            <property name="lineTokenizer" ref="fileLineTokenizer" />
    	</bean>
    
    	<bean name="fileLineTokenizer"
    	    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" />
    	    
    	</beans>
    batch.properties
    Code:
    # Placeholders batch.*
    #    for HSQLDB:
    batch.jdbc.driver=org.hsqldb.jdbcDriver
    batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
    # use this one for a separate server process so you can inspect the results
    # (or add it to system properties with -D to override at run time).
    # batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples
    batch.jdbc.user=sa
    batch.jdbc.password=
    batch.schema=
    batch.schema.script=classpath:/org/springframework/batch/core/schema-hsqldb.sql

  • #2
    I have resolved my issue and as expected it was a lack of understanding on how the pieces worked. I was missing some core beans which I am posting here in case someone else encounters the same issues:

    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:batch="http://www.springframework.org/schema/batch"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
     	<context:property-placeholder location="classpath:batch.properties" /> 
    
    	<context:component-scan base-package="com.mycode.myExample" />
     
     	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<property name="driverClassName" value="${batch.jdbc.driver}" />
    		<property name="url" value="${batch.jdbc.url}" />
    		<property name="username" value="${batch.jdbc.user}" />
    		<property name="password" value="${batch.jdbc.password}" />
    	</bean>
    	
    	<bean id="transactionManager" 
    		class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
    	
    	<bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<property name="jobRepository" ref="jobRepository" />
    	</bean>
    	
      	<jdbc:initialize-database data-source="dataSource">
    		<jdbc:script location="${batch.schema.script}" />
    	</jdbc:initialize-database>
    
    	<batch:job-repository id="jobRepository" />
    
    	<import resource="classpath:/META-INF/spring/module-context1.xml" />
    
    </beans>
    Code:
    public class myApp {
    
    	@Autowired
        private static JobLauncher jobLauncher;
     
        @Autowired
        private static Job job;
        
        public static void main( String[] args ) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException
        {
             ApplicationContext context = 
            		new ClassPathXmlApplicationContext("launch-context1.xml");
             JobLauncher jobLauncher = context.getBean(JobLauncher.class);
             Job job = context.getBean(Job.class);
             
             JobExecution jobExecution = jobLauncher.run(job,
                     new JobParameters());
             System.out.println("Job Status: " + jobExecution.getStatus().toString());
        }
    }

    Comment

    Working...
    X