在Struts 2中实现IoC
IoC(Inversion of Control,以下译为控制反转)随着Java社区中轻量级容器(Lightweight Contianer)的推广而越来越为大家耳熟能详。在此,我不想再多费唇舌来解释“什么是控制反转”和“为什么需要控制反转”。因为互联网上已经有非常多的文章对诸如此类的问题作了精彩而准确的回答。大家可以去读一下Rod Johnson和Juergen Hoeller合著的《Expert one-on-one J2EE Development without EJB》或Martin Fowler所写的《Inversion of Control Containers and the Dependency Injection pattern》。
言归正传,本文的目的主要是介绍在Struts 2中实现控制反转。
历史背景
众所周知,Struts 2是以Webwork 2作为基础发展出来。而在Webwork 2.2之前的Webwork版本,其自身有一套控制反转的实现,Webwork 2.2在Spring 框架的如火如荼发展的背景下,决定放弃控制反转功能的开发,转由Spring实现。值得一提的是,Spring确实是一个值得学习的框架,因为有越来越多的开源组件(如iBATIS等)都放弃与Spring重叠的功能的开发。因此,Struts 2推荐大家通过Spring实现控制反转。
具体实现
首先,在开发环境中配置好Struts 2的工程。对这部分仍然有问题的朋友,请参考我的早前的文章。
然后,将所需的Spring的jar包加入到工程的构建环境(Build Path)中
本文使用的是Spring 2.0,Spring强烈建议大家在使用其jar包时,只引用需要的包,原因是Spring是一个功能非常强大的框架,其中有些功能是您不需要的;而且Spring提倡的是“按需所取”,而不是EJB的“爱我就要爱我的一切”。当然,如果你怕麻烦或者是不清楚每个包的作用,引用一个Spring的总包也未尝不可。
接下来,就要修改WEB-INF\web.xml文件了,内容为:
<? xml version="1.0" encoding="UTF-8" ?> < 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 > Struts 2 IoC Demo </ display-name > < filter > < filter-name > struts-cleanup </ filter-name > < filter-class > org.apache.struts2.dispatcher.ActionContextCleanUp </ filter-class > </ filter > < filter > < filter-name > struts2 </ filter-name > < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class > </ filter > < filter-mapping > < filter-name > struts-cleanup </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < filter-mapping > < filter-name > struts2 </ filter-name > < url-pattern > /* </ url-pattern > </ filter-mapping > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < welcome-file-list > < welcome-file > index.html </ welcome-file > </ welcome-file-list > </ web-app >
package tutorial; import java.util.Set; public interface ChatService { Set < String > getUserNames();} 清单3 tutorial.ChatService.java 然后,再创建一个默认实现ChatServiceImpl,代码如下: package tutorial; import java.util.HashSet; import java.util.Set; public class ChatServiceImpl implements ChatService { public Set < String > getUserNames() { Set < String > users = new HashSet < String > (); users.add( " Max " ); users.add( " Scott " ); users.add( " Bob " ); return users; } } package tutorial; import java.util.Set; import com.opensymphony.xwork2.ActionSupport; public class ChatAction extends ActionSupport { private static final long serialVersionUID = 8445871212065L ; private ChatService chatService; private Set < String > userNames; public void setChatService(ChatService chatService) { this .chatService = chatService; } public Set < String > getUserNames() { return userNames; } @Override public String execute() { userNames = chatService.getUserNames(); return SUCCESS; } } <? 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 ="chatService" class ="tutorial.ChatServiceImpl" /> < bean id ="chatAction" class ="tutorial.ChatAction" scope ="prototype" > < property name ="chatService" > < ref local ="chatService" /> </ property > </ bean > </ beans >
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts > < include file ="struts-default.xml" /> < package name ="Struts2_IoC" extends ="struts-default" > < action name ="Chat" class ="chatAction" > < result > /UserList.jsp </ result > </ action > </ package > </ struts >
<% @ page contentType = " text/html; charset=UTF-8 " %> <% @ taglib prefix = " s " uri = " /struts-tags " %> < html > < head > < title > User List </ title > </ head > < body > < h2 > User List </ h2 > < ol > < s:iterator value ="userNames" > < li >< s:property /></ li > </ s:iterator > </ ol > </ body > </ html >