Alok Omkar
Alok Omkar

Reputation: 628

Spring Framework : Loading multiple .xml configuration files

I am trying to come up with a program that loads multiple configuration .xml files in spring framework.

This is the error I have encountered:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JDBCTemplateFileLog' defined in class path resource [BeansFileLog.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required

These are my xml files:

  1. Beans-All-Modules.xml

    <import resource="BeansJDBC.xml" /> <import resource="BeansFileLog.xml" /> <import resource="BeansCFAE.xml" />

  2. BeansJDBC.xml

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/TEST"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean>

  3. BeansFileLog.xml

        '<bean id="JDBCTemplateFileLog" class="in.customfileaccessevent.JDBCTemplateFileLog">
            <property name="dataSource" ref="dataSource" />
        </bean>`
    
  4. BeansCFAE.xml

    <bean id="customFileAccessEvent" class="in.customfileaccessevent.CustomFileAccessEvent"> <constructor-arg index="0" type="java.lang.String"> <value>E:/HelloWorld.doc</value> </constructor-arg> </bean>

    `<bean id="customEventHandler" class="in.customfileaccessevent.CustomFileAccessEventHandler" />
    
    <bean id="customFileAccessEventPublisher" class="in.customfileaccessevent.CustomFileAccessEventPublisher" />`
    

I have added external jar file "MysqlConnecterJ.. .jar" to the program. Still I am unable to figure out what the problem is?

The MainApp.java program is as follows:

`public class MainAppAllModules {

public static void main(String args[]){
    ConfigurableApplicationContext context = 
            new ClassPathXmlApplicationContext("Beans-All-Module.xml");
    //ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"BeansCFAE.xml", "BeansJDBC.xml"});

            CustomFileAccessEvent ce = (CustomFileAccessEvent) context.getBean("customFileAccessEvent");
    CustomFileAccessEventPublisher cvp = 
            (CustomFileAccessEventPublisher) context.getBean("customFileAccessEventPublisher");
    cvp.publish(ce);  
    /*ConfigurableApplicationContext context1 = 
            new ClassPathXmlApplicationContext("BeansJDBC.xml");*/  
    JDBCTemplateFileLog flogJDBCTemplate = (JDBCTemplateFileLog) context.getBean("JDBCTemplateFileLog");
    System.out.println("File Log Creation");
    flogJDBCTemplate.create("Sample", "Sample");



}

}`

Upvotes: 2

Views: 3457

Answers (1)

CorayThan
CorayThan

Reputation: 17825

You need to make a dataSource bean for "dataSource" to reference. Your xml files are importing fine. Try this tutorial to do that.

Upvotes: 1

Related Questions