1.導入jar包
Org.springframework.bean.jar
Org.springframework.context.jar
Org.springframework.core.jar
Org.springframework.web.jar
Org.springframework.asm.jar
Org.springframework.expression.jar
Struts2-sping-plugin.jar
2.定義service類,作爲要注入的類
3.定義applictionContext.xml,在Web-Inf目錄下,如下
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
<bean id ="bookService" class ="com.service.SpringIocService" />
<bean id ="bookAction" class ="com.test.action.SpringIocAction" scope ="prototype" >
<property name ="bookService" >
<ref local ="bookService" />
</property >
</bean >
</beans>
(注意action的屬性scope ="prototype", 表示每次請求都實例化一個新的對象)
4.更改web.xml,加入
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
5.在struts.properties中加入struts.objectFactory=spring
6.更改struts.xml文件中的<action>
<action name="" Class="sping配置的beanID">