1

尝试将 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>

豆类.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>

堆栈跟踪错误

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)

几周以来,我一直在为这种整合而苦苦挣扎,这次我虽然很接近,但我在这里,再次陷入困境......

关于可能导致这种情况的任何想法?不同库之间是否存在某种不兼容?

4

1 回答 1

3

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 依赖项中。

于 2015-11-22T18:47:09.153 回答