我試圖想出一個程序,加載春天框架中的多個配置.xml文件。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文件:
豆類,全Modules.xml
<import resource="BeansJDBC.xml" /> <import resource="BeansFileLog.xml" /> <import resource="BeansCFAE.xml" />
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>
BeansFileLog.xml
'<bean id="JDBCTemplateFileLog" class="in.customfileaccessevent.JDBCTemplateFileLog"> <property name="dataSource" ref="dataSource" /> </bean>`
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" />`
我已經添加外部jar文件「MysqlConnecterJ .. .jar「添加到程序中。但我仍然無法弄清楚問題所在?
的MainApp.java程序如下:
'公共類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");
}
}`
感謝您的快速回復。我經歷了你介紹給我的教程。我試圖在我的程序中結合兩個代碼;一個是自定義文件訪問事件,第二個是Spring jdbc示例。我按照教程的指示,似乎無法調試它。 –
實現它的數據源部分後,當您嘗試運行該程序時是否出現錯誤? – CorayThan
我從頭開始重新編寫一切。它似乎已經成功了。我試圖將相同的程序文件複製到另一個軟件包,經過一些編輯,它工作。我發現它很奇怪,你能否進一步解釋這個問題?我還發現了'<豆的xmlns = 「http://www.springframework.org/schema/beans」 的xmlns:的xsi = 「http://www.w3.org/2001/XMLSchema-instance」 的xsi :的schemaLocation = 「http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd」>'在我的xml文件2.5.xsd 。我正在使用Spring 4.0 –