无法使用 ClassPathXmlApplicationContext 加载我的配置文件,
Can't load my Config File with ClassPathXmlApplicationContext,
我正在使用 spring-框架和休眠开发一个 Maven 项目。我有这两种方法:
public void loadXml() throws IOException, URISyntaxException {
String filePathXml ="spring_hibernate_files/spring-hibernate4.xml";
File file = Resources.getResourceFile(filePathXml);
if(file.exists()){
ApplicationContext contextClassPath = new ClassPathXmlApplicationContext(file.getAbsolutePath());
}
}
public static File getResourceFile(String name){
return new File(Resources.class.getClassLoader().getResource(name).getFile());
}
这是spring-hibernate4.xml,位于resources/spring_hibernate_files/spring-hibernate4。xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- Import database.properties file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>spring_hibernate_files/database.properties</value>
</property>
</bean>
<!-- We use Apache Commons DBCP for a data source with connection pooling capability: -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- The following declaration is for automatic transaction support for the SessionFactory: -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>object.model.Website</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property></bean>
<!-- Set the Hibernate for all Implementation classes -->
<bean id="WebsiteDao" class="object.impl.WebsiteDaoImpl"><property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/></bean>
当我到达 运行 代码行时:
ApplicationContext contextClassPath = new ClassPathXmlApplicationContext(file.getAbsolutePath());
(注意:我已经尝试使用所有类型的模拟字符串来匹配文件),我有以下异常,我已经尝试并在网上搜索类似的问题,但现在我被卡住了。
这是例外情况:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130)
Caused by: java.lang.NoSuchMethodError: org.springframework.util.Assert.noNullElements([Ljava/lang/Object;Ljava/lang/String;)V
at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:77)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:137)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at spring.Test_Spring_Hibernate.loadXml(Test_Spring_Hibernate.java:53)
at spring.Test_Spring_Hibernate.main(Test_Spring_Hibernate.java:37)
... 5 more
提前。
更新 1
已经尝试过:
ApplicationContext contextClassPath2 =
new ClassPathXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");
ApplicationContext contextClassPath = new FileSystemXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");
但他发起了同样的例外。我把 mvn:dependency 树放在这里:
pastebin.com/a7tKKenB
更新 2[已解决]
好的是另一个版本 spring 的冲突依赖。我之前找不到,因为它像自定义库中的外部 jar 一样导入,而不像 Maven 依赖项。
谢谢大家的帮助。
这听起来像是与您的依赖版本发生了版本冲突。确保所有 Spring 依赖项都具有相同的版本。您可以使用命令 mvn dependency:tree
来概览您的依赖项。也许您可以 post 命令的输出,以便我们查看。
我正在使用 spring-框架和休眠开发一个 Maven 项目。我有这两种方法:
public void loadXml() throws IOException, URISyntaxException {
String filePathXml ="spring_hibernate_files/spring-hibernate4.xml";
File file = Resources.getResourceFile(filePathXml);
if(file.exists()){
ApplicationContext contextClassPath = new ClassPathXmlApplicationContext(file.getAbsolutePath());
}
}
public static File getResourceFile(String name){
return new File(Resources.class.getClassLoader().getResource(name).getFile());
}
这是spring-hibernate4.xml,位于resources/spring_hibernate_files/spring-hibernate4。xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- Import database.properties file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>spring_hibernate_files/database.properties</value>
</property>
</bean>
<!-- We use Apache Commons DBCP for a data source with connection pooling capability: -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- The following declaration is for automatic transaction support for the SessionFactory: -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="hibernate4AnnotatedSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>object.model.Website</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property></bean>
<!-- Set the Hibernate for all Implementation classes -->
<bean id="WebsiteDao" class="object.impl.WebsiteDaoImpl"><property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/></bean>
当我到达 运行 代码行时:
ApplicationContext contextClassPath = new ClassPathXmlApplicationContext(file.getAbsolutePath());
(注意:我已经尝试使用所有类型的模拟字符串来匹配文件),我有以下异常,我已经尝试并在网上搜索类似的问题,但现在我被卡住了。 这是例外情况:
Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:130) Caused by: java.lang.NoSuchMethodError: org.springframework.util.Assert.noNullElements([Ljava/lang/Object;Ljava/lang/String;)V at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:77) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:137) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at spring.Test_Spring_Hibernate.loadXml(Test_Spring_Hibernate.java:53) at spring.Test_Spring_Hibernate.main(Test_Spring_Hibernate.java:37) ... 5 more
提前。
更新 1 已经尝试过:
ApplicationContext contextClassPath2 =
new ClassPathXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");
ApplicationContext contextClassPath = new FileSystemXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");
但他发起了同样的例外。我把 mvn:dependency 树放在这里:
pastebin.com/a7tKKenB
更新 2[已解决] 好的是另一个版本 spring 的冲突依赖。我之前找不到,因为它像自定义库中的外部 jar 一样导入,而不像 Maven 依赖项。 谢谢大家的帮助。
这听起来像是与您的依赖版本发生了版本冲突。确保所有 Spring 依赖项都具有相同的版本。您可以使用命令 mvn dependency:tree
来概览您的依赖项。也许您可以 post 命令的输出,以便我们查看。