Spring Framework:加载多个.xml配置文件

时间:2013-08-03 06:53:21

标签: mysql xml spring

我正在尝试提出一个在spring框架中加载多个配置.xml文件的程序。

这是我遇到的错误:

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

这些是我的xml文件:

  1. 豆-全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" />`
    
  5. 我已将外部jar文件“MysqlConnecterJ .. .jar”添加到程序中。我仍然无法弄清楚问题是什么?

    MainApp.java程序如下:

    `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");
    
    
    
    }
    

    }`

1 个答案:

答案 0 :(得分:1)

您需要为&#34; dataSource&#34;创建一个dataSource bean。参考。您的xml文件导入正常。请尝试使用this tutorial