java - @autowired 不适用于 struts2 + spring

标签 java spring struts2 autowired struts2-spring-plugin

我想制作一个基于struts2和spring的Web应用程序。首先,我测试了@Autowired是否在struts2上工作。但事实并非如此,dataSource 为空。我不知道如何解决它。请给我相关信息。

HelloWorld.java

package example;
import java.sql.Connection;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component
public class HelloWorld extends ActionSupport {
    @Autowired
    private BasicDataSource dataSource;
    public String execute() throws Exception {
        Connection con = dataSource.getConnection();
        con.close();
        return SUCCESS;
    }
}

applicationContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:mem" />
    <property name="maxActive" value="10" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

<context:annotation-config />
<context:component-scan base-package="example" />

web.xml

<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>

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

struts.xml

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

<package name="example" namespace="/example" extends="struts-default">
    <action name="HelloWorld" class="example.test.HelloWorld">
        <result>/example/HelloWorld.jsp</result>
    </action>
</package>

最佳答案

要使@Autowired在你的struts应用程序中工作,你需要给定的东西:

确保类路径中有 struts2 和 spring 插件。

将以下行放入 strtus.xml

<struts>
  <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
  ... 
</struts>

在spring的xml文件中配置Action类

<bean id="editAction" class="org.apache.struts.edit.action.EditAction" >

    <property name="editService" ref="editService" />

</bean>

并在 struts.xml 文件中给出 bean id,而不是给出操作类

<action name="edit" class="editAction" method="input">
    <result name="input">/edit.jsp</result>
</action>  

更多信息请咨询http://struts.apache.org/release/2.2.x/docs/spring-and-struts-2.htmlhttp://struts.apache.org/release/2.2.x/docs/spring-plugin.html

关于java - @autowired 不适用于 struts2 + spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20942123/

相关文章:

java - 将 (.net) BsonDocument 字符串转换为 (java) DBObject

IBM JVM 1.4.2 (WebSphere 6.0.2) 的 Java 分析器

Spring 缓存: force update cache based on condition

java - struts 2标签导致性能缓慢

java - FileUploadBase$SizeLimitExceededException : the request was rejected because its size (337867) exceeds the configured maximum (200)

struts2 - 如何从struts2中的验证中排除操作方法

java - 实现 keylistener 和 jpanel 的单独类

java - JComboBox 的自定义字体

java - Jackson中的多态反序列化没有注释

java - ModelMapper - 将 DTO 的属性映射到具有该属性的关系的实体内