如何让Wheel预生成Class

一世执手 提交于 2019-12-04 06:10:59

有话说在前

在上一篇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,这里面没什么花头,就不介绍了,反编译看看就可以了
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!