struts2 用Spring實現Ioc

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

發佈了36 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章