java.lang.NoSuchMethodError:org.springframework.beans.support.ResourceEditorRegistrar。<init>

时间:2015-11-22 16:13:59

标签: java spring maven struts2 struts2-spring-plugin

尝试将Spring 4.2.2.RELEASE与Struts2 2.3.24.1集成。我通过在操作中手动实例化我的Service类来测试Spring是否正在工作

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
DbServicesHibernate dbServices = (DbServicesHibernate) context.getBean("dbServices");

一旦有效,我希望Spring能够通过自动装配自动注入dbServices依赖关系,因此我添加了struts2-spring-plugin 2.3.24.1和修改后的web.xml。生成的文件和错误堆栈跟踪为:

struts.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.devMode" value="true" />

    <package name="issues" extends="struts-default">

        <action name="GetCbtionList" class="coproject.cpweb.actions.GetCbtionList">
            <result name="SUCCESS">/views/dummy.jsp</result>
        </action>

    </package>

</struts>

行动类

public class GetCbtionList extends ActionSupport{

    private static final long serialVersionUID = 1L;

    public String execute() throws Exception  {

        User user = new User();
        user.setFirstname("Jose");
        user.setPassword("iris");

        User user2 = new User();
        user2.setFirstname("Rosa");
        user2.setPassword("iras");

        dbServices.saveUser(user);
        dbServices.saveUser(user2);

        return "SUCCESS";
    }

    DbServicesIf dbServices;

    public DbServicesIf getDbServices() {
        return dbServices;
    }

    public void setDbServices(DbServicesIf dbServices) {
        this.dbServices = dbServices;
    }

}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!-- The web.xml, also known as the deployment descriptor, defines a Java Servlet web application. 
         This document is a mandatory element of any web application and must reside within WEB-INF.  The 
         deployment descriptor defines all the servlets and servlet filters that belong to this web
         application. -->

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>CoProject</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/ *</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>views/issue_navigator_dum.jsp</welcome-file>
    </welcome-file-list>

     <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans.xml</param-value>
    </context-param>

</web-app>

的beans.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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="com.mysql.jdbc.Driver" />
       <property name="url" value="jdbc:mysql://localhost:3306/test" />
       <property name="username" value="jaof" />
       <property name="password" value="iris" />
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="annotatedClasses">
            <list>
                <value>coproject.cpweb.utils.db.entities.User</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">true</prop>
            </props>
        </property>
    </bean>

    <bean id="dbServices" class="coproject.cpweb.utils.db.services.DbServicesHibernate">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

</beans>

Maven依赖

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- STRUTS -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24.1</version>
        </dependency>

        <!-- HIBERNATE -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.2.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>


        <!-- SPRING --> 
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.24.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

    </dependencies>

Stacktrace错误

INFO: Initializing Spring root WebApplicationContext
nov 22, 2015 4:51:24 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
nov 22, 2015 4:51:24 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Sun Nov 22 16:51:24 CET 2015]; root of context hierarchy
nov 22, 2015 4:51:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/beans.xml]
nov 22, 2015 4:51:25 PM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
java.lang.NoSuchMethodError: org.springframework.beans.support.ResourceEditorRegistrar.<init>(Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/PropertyResolver;)V
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:622)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:512)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

nov 22, 2015 4:51:25 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoSuchMethodError: org.springframework.beans.support.ResourceEditorRegistrar.<init>(Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/PropertyResolver;)V
    at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory(AbstractApplicationContext.java:622)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:512)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4728)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5166)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我几周来一直在努力进行这种整合,这一次,虽然我已经接近了,但我在这里,再次陷入困境......

对于可能导致此问题的任何想法?这是不同的图书馆之间的某种不兼容性吗?

1 个答案:

答案 0 :(得分:2)

对struts2-spring-plugin的maven依赖:2.3.24.1包含对spring-web的依赖:3.0.5.RELEASE,它依次包含对spring-beans的依赖:3.0.5.RELEASE。

所以,spring-beans:3.0.5.RELEASE是用于org.springframework.beans.support.ResourceEditorRegistrar类的引用,它与spring 4.2.2.RELEASE的其余部分不兼容。

我在spring-beans中加入了依赖:4.2.2.RELEASE在我的层次结构之上,即在pom.xml文件中为

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>

现在不再使用通过struts2-spring-plugin链接的spring-beans 3.0.5.RELEASE,并且spring-beans-4.2.2.RELEASE.jar文件包含在Maven依赖项中。