有話說在前
在上一篇Wheel實例教程
MVC框架Wheel簡單實例中,咱們介紹瞭如何使用Wheel建立一個簡單的web應用,可是若是按照上一篇的內容實現的話咱們全部的代理類(好比事務的注入,依賴注入,請求轉發)都是在運行是生成的,那麼這一節將採用另外一種方式,就是將代理類預先生成在war包中,這樣在server中運行咱們的項目的時候就無需動態建立了,這樣就很是快速和節省性能。固然採用這種方式咱們須要採用mavn來構建的,因此你得有點maven基礎才成。
global.properties
咱們在上一篇文章中有一行配置的global.properties以下:
generator.class.runtime=true
首先咱們要將這個值改爲false,這就是告訴程序,我不要在運行的時候生成了。
pom.xml
在pom.xml中添加以下內容:
<build>
<plugins>
<plugin>
<groupId>cn.wensiqun</groupId>
<artifactId>wheel-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>create-proxy-class</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
等等,wheel-maven-plugin是什麼,這個是配合wheel使用的插件,http://code.taobao.org/svn/WheelSampleApp/trunk 這是源碼地址。
隨便的講解下
到這裏所要作的就OK了,對咱們的web項目執行mvn clean install。 在到target目錄下去看咱們每一個class如今的樣子吧。 首先多了下面兩個class,用反編譯的軟件看看:
package cn.wensiqun.wheel.util.rs;
import jw.jwweb.mock.utils.UserResultConverter;
public class ResultSetConverterBuilderImpl
implements ResultSetConverterBuilder
{
public void build()
{
new UserResultConverter();
}
}
package cn.wensiqun.wheel.mvc;
import cn.wensiqun.wheel.mvc.dispatcher.HttpRequestDispatcher;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.HTMLResolver;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.JSONResolver;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.JSPResolver;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.PlaintextResolver;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.RedirectResolver;
import cn.wensiqun.wheel.mvc.dispatcher.asm.result.VelocityResolver;
import cn.wensiqun.wheel.mvc.exception.InvalidActionReturnValueException;
import cn.wensiqun.wheel.mvc.exception.InvalidRequestPathException;
import cn.wensiqun.wheel.mvc.http.WheelHttpServletRequest;
import cn.wensiqun.wheel.mvc.http.WheelHttpServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jw.jwweb.mock.servlet.MockServletAction;
public class HttpRequestDispatcherImpl
implements HttpRequestDispatcher
{
private static final HTMLResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_HTMLRESOLVER = new HTMLResolver("HTML");
private static final JSONResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_JSONRESOLVER = new JSONResolver("JSON");
private static final JSPResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_JSPRESOLVER = new JSPResolver("JSP");
private static final PlaintextResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_PLAINTEXTRESOLVER = new PlaintextResolver("PLAINTEXT");
private static final RedirectResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_REDIRECTRESOLVER = new RedirectResolver("REDIRECT");
private static final VelocityResolver RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_VELOCITYRESOLVER = new VelocityResolver("VELOCITY");
private void resultProcess(HttpServletRequest req, HttpServletResponse resp, String resultType, String path)
throws Exception
{
if ("HTML".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_HTMLRESOLVER.execute(req, resp, resultType, path);
return;
}
if ("JSON".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_JSONRESOLVER.execute(req, resp, resultType, path);
return;
}
if ("JSP".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_JSPRESOLVER.execute(req, resp, resultType, path);
return;
}
if ("PLAINTEXT".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_PLAINTEXTRESOLVER.execute(req, resp, resultType, path);
return;
}
if ("REDIRECT".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_REDIRECTRESOLVER.execute(req, resp, resultType, path);
return;
}
if ("VELOCITY".equals(resultType))
{
RESULTRESOLVER_CN_WENSIQUN_WHEEL_MVC_DISPATCHER_ASM_RESULT_VELOCITYRESOLVER.execute(req, resp, resultType, path);
return;
}
resp.setContentType("text/html");
resp.getOutputStream().close();
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
String result;
MockServletAction obj;
if (servletPath.equals("/MockServlet/execute.action"))
{
obj = MockServletAction._getInstance@();
obj.setHttpServletRequest(req);
obj.setHttpServletResponse(resp);
result = obj.execute();
if ("JSP_RES".equals(result))
{
resultProcess(req, resp, "JSP", "/servlet/test.jsp");
return;
}
if ("Redirect_RES".equals(result))
{
resultProcess(req, resp, "REDIRECT", "/servlet/test.jsp");
return;
}
if ("plaintext_RES".equals(result))
{
resultProcess(req, resp, "PLAINTEXT", "");
return;
}
if ("html_RES".equals(result))
{
resultProcess(req, resp, "HTML", "");
return;
}
if ("json_RES".equals(result))
{
resultProcess(req, resp, "JSON", "");
return;
}
if ("velocity_RES".equals(result))
{
resultProcess(req, resp, "VELOCITY", "/WEB-INF/test.vm");
return;
}
throw new InvalidActionReturnValueException(result, MockServletAction.class, MockServletAction.class.getDeclaredMethod("execute", new Class[0]));
}
String result;
MockServletAction obj;
if (servletPath.equals("/MockServlet/process.action"))
{
obj = MockServletAction._getInstance@();
obj.setHttpServletRequest(req);
obj.setHttpServletResponse(resp);
result = obj.execute();
if ("JSP_RES".equals(result))
{
resultProcess(req, resp, "JSP", "/servlet/test.jsp");
return;
}
if ("Redirect_RES".equals(result))
{
resultProcess(req, resp, "REDIRECT", "/servlet/test.jsp");
return;
}
if ("plaintext_RES".equals(result))
{
resultProcess(req, resp, "PLAINTEXT", "");
return;
}
if ("html_RES".equals(result))
{
resultProcess(req, resp, "HTML", "");
return;
}
if ("json_RES".equals(result))
{
resultProcess(req, resp, "JSON", "");
return;
}
if ("velocity_RES".equals(result))
{
resultProcess(req, resp, "VELOCITY", "/WEB-INF/test.vm");
return;
}
throw new InvalidActionReturnValueException(result, MockServletAction.class, MockServletAction.class.getDeclaredMethod("execute", new Class[0]));
}
throw new InvalidRequestPathException(servletPath);
}
public void doHead(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
throw new InvalidRequestPathException(servletPath);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
String result;
MockServletAction obj;
if (servletPath.equals("/MockServlet/execute.action"))
{
obj = MockServletAction._getInstance@();
obj.setHttpServletRequest(req);
obj.setHttpServletResponse(resp);
result = obj.execute();
if ("JSP_RES".equals(result))
{
resultProcess(req, resp, "JSP", "/servlet/test.jsp");
return;
}
if ("Redirect_RES".equals(result))
{
resultProcess(req, resp, "REDIRECT", "/servlet/test.jsp");
return;
}
if ("plaintext_RES".equals(result))
{
resultProcess(req, resp, "PLAINTEXT", "");
return;
}
if ("html_RES".equals(result))
{
resultProcess(req, resp, "HTML", "");
return;
}
if ("json_RES".equals(result))
{
resultProcess(req, resp, "JSON", "");
return;
}
if ("velocity_RES".equals(result))
{
resultProcess(req, resp, "VELOCITY", "/WEB-INF/test.vm");
return;
}
throw new InvalidActionReturnValueException(result, MockServletAction.class, MockServletAction.class.getDeclaredMethod("execute", new Class[0]));
}
String result;
MockServletAction obj;
if (servletPath.equals("/MockServlet/process.action"))
{
obj = MockServletAction._getInstance@();
obj.setHttpServletRequest(req);
obj.setHttpServletResponse(resp);
result = obj.execute();
if ("JSP_RES".equals(result))
{
resultProcess(req, resp, "JSP", "/servlet/test.jsp");
return;
}
if ("Redirect_RES".equals(result))
{
resultProcess(req, resp, "REDIRECT", "/servlet/test.jsp");
return;
}
if ("plaintext_RES".equals(result))
{
resultProcess(req, resp, "PLAINTEXT", "");
return;
}
if ("html_RES".equals(result))
{
resultProcess(req, resp, "HTML", "");
return;
}
if ("json_RES".equals(result))
{
resultProcess(req, resp, "JSON", "");
return;
}
if ("velocity_RES".equals(result))
{
resultProcess(req, resp, "VELOCITY", "/WEB-INF/test.vm");
return;
}
throw new InvalidActionReturnValueException(result, MockServletAction.class, MockServletAction.class.getDeclaredMethod("execute", new Class[0]));
}
throw new InvalidRequestPathException(servletPath);
}
public void doPut(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
throw new InvalidRequestPathException(servletPath);
}
public void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
throw new InvalidRequestPathException(servletPath);
}
public void doOptions(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
throw new InvalidRequestPathException(servletPath);
}
public void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws Exception
{
req = new WheelHttpServletRequest(req);
resp = new WheelHttpServletResponse(resp);
String servletPath = req.getServletPath();
throw new InvalidRequestPathException(servletPath);
}
}
上面那個是咱們將ResultSet轉換成java對象的工具類的初始化方法。下面那個就是很重要的MVC請求轉發的類了。在看看DAO吧
package jw.jwweb.mock.dao.impl;
import cn.wensiqun.wheel.db.template.MySQLTemplate;
import cn.wensiqun.wheel.exception.WheelRuntimeException;
import cn.wensiqun.wheel.util.Utils;
import cn.wensiqun.wheel.util.rs.ResultSetConverterContext;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Properties;
import jw.jwweb.mock.dao.MockDao;
import jw.jwweb.mock.entity.User;
public class MockDaoImpl extends MySQLTemplate
implements MockDao
{
private static MockDaoImpl _instance@;
private static Properties $sqlMapper;
public void insertUser(User user)
{
}
public void deleteUser(User user)
{
}
public void updateUser(User user)
{
}
private List allUser@ByProxy()
throws Exception
{
PreparedStatement prepareStatement = null;
prepareStatement = getStatement(getSql("all.user"), new Object[0]);
ResultSet rs = prepareStatement.executeQuery();
return ResultSetConverterContext.convertToEntities(User.class, rs);
}
public List fuzzyQueryUser(User user)
{
return null;
}
static
{
try
{
$sqlMapper = Utils.getProperties("/jw/jwweb/mock/dao/impl/MockDaoImplSQL.properties");
Utils.processParameterName($sqlMapper);
}
catch (Throwable e)
{
throw new WheelRuntimeException(e);
}
}
public static MockDaoImpl _getInstance@()
{
return new MockDaoImpl();
}
protected String getSql(String key)
{
return $sqlMapper.get(key).toString();
}
public List allUser()
throws Exception
{
List localList = null;
try
{
localList = allUser@ByProxy();
}
finally
{
super.close();
}
return localList;
}
}
已經添加了獲取sql配置文件的內容,而且初始化到$sqlMapper中;getSql方法也有內容了;同時將原先allUser的方法內容放到了allUser@ByProxy方法中,而allUser方法也添加了close方法。還有些自動生成的內容,大夥應該能看懂,下面接着進入service方法
package jw.jwweb.mock.service.impl;
import cn.wensiqun.wheel.annotation.DAO;
import cn.wensiqun.wheel.annotation.Singleton;
import cn.wensiqun.wheel.annotation.Transaction;
import cn.wensiqun.wheel.db.connection.DBConnection;
import cn.wensiqun.wheel.exception.WheelRuntimeException;
import java.io.PrintStream;
import java.util.List;
import jw.jwweb.mock.dao.MockDao;
import jw.jwweb.mock.dao.impl.MockDaoImpl;
import jw.jwweb.mock.entity.User;
import jw.jwweb.mock.service.MockService;
@Singleton
public class MockServiceImpl
implements MockService
{
@DAO
private MockDao mockDao;
private static MockServiceImpl _instance@;
private void &init&@ByProxy()
{
}
@Transaction
private List getUsers@ByProxy()
throws Exception
{
return this.mockDao.allUser();
}
@Transaction
private void addUser@ByProxy()
throws Exception
{
}
static
{
try
{
_instance@ = new MockServiceImpl();
}
catch (Throwable e)
{
throw new WheelRuntimeException(e);
}
}
public static MockServiceImpl _getInstance@()
{
return _instance@;
}
public MockServiceImpl()
{
try
{
this.mockDao = MockDaoImpl._getInstance@();
&init&@ByProxy();
}
catch (Throwable e)
{
throw new WheelRuntimeException(e);
}
}
public List getUsers()
throws Exception
{
List localList = null;
try
{
DBConnection.getInstance().startTransaction();
localList = getUsers@ByProxy();
}
finally
{
DBConnection.getInstance().endTransaction();
}
return localList;
}
public void addUser()
throws Exception
{
try
{
DBConnection.getInstance().startTransaction();
addUser@ByProxy();
}
finally
{
DBConnection.getInstance().endTransaction();
}
}
}
首先是在構造方法中,添加了代碼:this.mockDao = MockDaoImpl._getInstance@();這個就是依賴注入了。getUsers中的方法也加入了事務, 最後一個是Action,這裏面沒什麼花頭,就不介紹了,反編譯看看就能夠了