简体   繁体   中英

Can't load my Config File with ClassPathXmlApplicationContext,

i'm working on a maven project with spring-framework and hibernate. I have these two method:

 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());
}

This is the spring-hibernate4.xml, located in 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>

When i arrived to run the line of code:

ApplicationContext contextClassPath = new ClassPathXmlApplicationContext(file.getAbsolutePath());

(NOTE:I've tried with all kind of analog string for match the file), I have the following exception, i've tried and search on the web with similar problema but now i'm stuck. This is the exception:

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_Hiber nate.java:53) at spring.Test_Spring_Hibernate.main(Test_Spring_Hibernate.java:37) ... 5 more

ty in advance.

UPDATE 1 Already tried with:

ApplicationContext contextClassPath2 =
       new ClassPathXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");
ApplicationContext contextClassPath = new FileSystemXmlApplicationContext("spring_hibernate_files/spring-hibernate4.xml");

But he's launch the same exception. I put here the mvn:dependency tree:

pastebin.com/a7tKKenB

UPDATE 2[SOLVED] OK is was a conflict dependency of another version of spring. I can't found before because it was import like a external jar in a custom library and not like a maven dependency. Ty to everyone for the help.

This sounds like a version conflict with your dependency versions. Make sure, that all Spring dependencies have the same version. You can use the command mvn dependency:tree to get an overview of your dependencies. Maybe you could post the output of the command so we can have a look.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM