nanocontainer对picocontainer容器作了一些扩充的包装,它提供了用多种脚本(groovy, .bsh, .js, .py or .xml等)配置容器管理的对象的功能,同时提供了对web容器的支持,并且针对struts、webwork也有专门的支持,这里我调试成功了在struts中如何使用nanocontainer容器。nanocontainer的网站上的文档提供的非常少,从这里可以下载到一个简单的web应用的例子。
http://dist.codehaus.org/nanocontainer/war/nanocontainer-sample-nanowar.war
但是这个war并不完整,解开war看到只有一些编译过的class,要看源代码可以从cvs上下载到
http://www.nanocontainer.org/Source+Repositories
并没有web.xml struts-config.xml等配置文件和jsp文件。nanocontainer也太不注重文档和例子代码的详细程度了。我自己补充完整了这个例子需要的其他文件,在下面贴出来。
运行这个例子需要以下几个文件:
1. web.xml
这个文件只要从struts提供的例子中的blank中找到一个空的web.xml文件,加入下面配置就可以了
<context-param>
<param-name>nanocontainer.groovy</param-name>
<param-value>/WEB-INF/nanocontainer.groovy</param-value>
</context-param>
<listener>
<listener-class>org.nanocontainer.nanowar.ServletContainerListener</listener-class>
</listener>
2.nanocontainer.groovy 放到/WEB-INF中,我在这里使用了groovy 脚本的配置方式。
pico = new org.picocontainer.defaults.DefaultPicoContainer(parent)
if(assemblyScope instanceof javax.servlet.ServletContext) {
pico.registerComponentImplementation(org.nanocontainer.sample.nanowar.service.defaults.DefaultCheeseService)
pico.registerComponentImplementation(org.nanocontainer.sample.nanowar.dao.simple.MemoryCheeseDao)
} else if(assemblyScope instanceof javax.servlet.http.HttpSession) {
} else if(assemblyScope instanceof javax.servlet.ServletRequest) {
}
3.struts-config.xml文件内容如下
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- ============================================ Data Source Configuration -->
<!-- ================================================ Form Bean Definitions -->
<form-beans>
<form-bean
name="inputForm"
type="org.nanocontainer.sample.nanowar.struts.CheeseForm"/>
</form-beans>
<!-- ========================================= Global Exception Definitions -->
<global-exceptions>
<!-- sample exception handler
<exception
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample -->
</global-exceptions>
<!-- =========================================== Global Forward Definitions -->
<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<!-- =========================================== Action Mapping Definitions -->
<action-mappings>
<action
path="/nanicoTest"
type="org.nanocontainer.sample.nanowar.struts.CheeseAction"
name="inputForm" >
<forward redirect="false" name="next"
path="/result.jsp" />
</action>
</action-mappings>
<!-- ============================================= Controller Configuration -->
<controller
processorClass="org.nanocontainer.nanowar.struts.PicoRequestProcessor"/>
<!-- ======================================== Message Resources Definitions -->
<message-resources parameter="MessageResources" />
</struts-config>
4. naoweb.jsp 一个测试的jsp form表单页面。
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<%
out.println(session.getAttribute(org.apache.struts.Globals.LOCALE_KEY)+"--<br>");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>zmlogin.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<html:errors/>
<html:form action="nanicoTest">
<INPUT type="hidden" name="method" value="vtest">
<table width="41%" border="1">
<tr>
<td width="26%">Name</td>
<td width="74%"><input type="text" name="name"></td>
</tr>
<tr>
<td>Country</td>
<td><input type="text" name="country"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit">
</div></td>
</tr>
</table>
<br>
</html:form>
</body>
</html:html>
5.提交的结果页面 result.jsp
<%
out.println("=="+request.getAttribute("cheesesOfTheWord"));
%>
将这几个文件保存,放到合适的位置,启动tomcat,请求naoweb.jsp文件即可。
我们可以从源码CheeseAction看到,CheeseService 是个接口,这个代码中并没有给出接口的实现,
并没有和某一个具体的实现产生依赖关系,可见依赖关系是在运行时由容器建立的,pico容器使用的是构造子注入,可以看到代码中有对应的构造器。容器是从nanocontainer.groovy配置文件中找到具体实现的。然而代码是可以正常运行的,这就是我们想要达到的理想的效果,真正的面向接口的编程。这样的结果对我们的单元测试是非常有利的。
public class CheeseAction extends Action {
private final CheeseService cheeseService;
public CheeseAction(CheeseService cheeseService) {
this.cheeseService = cheeseService;
}
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
CheeseForm cheeseForm = (CheeseForm) form;
if (!isEmpty(cheeseForm.getName())) {
Cheese cheese = new Cheese(cheeseForm.getName(), cheeseForm.getCountry());
cheeseService.save(cheese);
}
Collection cheeses = cheeseService.getCheeses();
request.setAttribute("cheesesOfTheWord", cheeses);
return mapping.findForward("next");
}
private boolean isEmpty(String s) {
return s == null || "".equals(s.trim());
}
}
补充:
读
org.nanocontainer.nanowar.struts.PicoRequestProcessor和
org.nanocontainer.nanowar.struts.ActionFactory 的源码。
processActionCreate方法在发出一个request请求时执行。该方法用于创建action,他覆盖了struts中原有的该方法操作,替代之用nanico容器来创建和管理action,这样可以完成对action对象的构造器注入。
ActionFactory中,getActionsContainer方法,用来取得ActionsContainer,用这个键值KeyConstants.ACTIONS_CONTAINER从request中取,如果没有就创建他并以该键值放到request中。
private MutablePicoContainer getActionsContainer(HttpServletRequest request) {
MutablePicoContainer actionsContainer = (MutablePicoContainer) request.getAttribute(KeyConstants.ACTIONS_CONTAINER);
if (actionsContainer == null) {
actionsContainer = new DefaultPicoContainer(containerFinder.findContainer(request));
request.setAttribute(KeyConstants.ACTIONS_CONTAINER, actionsContainer);
}
return actionsContainer;
}
在创建的过程中,用到了containerFinder.findContainer(request)方法,在代码org.nanocontainer.nanowar.ServletContainerFinder中,findContainer方法,将按request、session、Application的顺序查找,但是键值分别是 KeyConstants.REQUEST_CONTAINER 、KeyConstants.SESSION_CONTAINER、 KeyConstants.APPLICATION_CONTAINER.
以下是findContainer方法的代码片断。
MutablePicoContainer container = getRequestContainer(request);
if (container == null) {
container = getSessionContainer(request.getSession());
}
if (container == null) {
container = getApplicationContainer(request.getSession().getServletContext());
}