Argo:58同城开源的轻量级web框架 https://github.com/58code/Argo
作为仅有Java语言背景,从没接触过web容器,Spring、Servlet,tomcat、jetty的门外汉,我心中最大的疑惑是Argo是如何工作的?
附带的例子samples/hello-world,我找不到Main函数。从文档Readme中看到,运行就一条指令
maven jetty:run
maven是类似于ant的一个项目管理工具,但jetty是什么? 在这条指令背后到底干了些什么? 我丝毫看不出这条指令和Argo有什么关系
- jetty
在下面的两个网页中,我找到一些答案。
http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin
http://wiki.eclipse.org/Jetty/Reference/Jetty_Architecture
1).jetty是类似于tomcat的web容器,并有connectors、scanIntervalSeconds这些可配置选项。
我以为jetty配置选项中会有关联指向Argo,不过在samples / hello-world / pom.xml文件中,我只找到了这些:
<plugin>
<!-- http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<stopPort>9966</stopPort>
<stopKey>foo</stopKey>
<scanIntervalSeconds>0</scanIntervalSeconds>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>80</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
</plugin>
唯一和hello-world有关的可能就是webAppConfig,但是这里只配置了一个路径contextPath。
2).jetty:run将会默认根据下列文件部署web应用
- resources in ${basedir}/src/main/webapp
- classes in ${project.build.outputDirectory}
- web.xml in ${basedir}/src/main/webapp/WEB-INF/
但是我发现src/main/webapp目录下只有3个html,没有任何配置文件。没有web.xml。唯一处理的就是HelloController.java,HomeController.java。
3).jetty具有如下架构

Connector接收Http连接。Handle处理Connector传来的请求,再产生应答。ThreadPool分配的线程完成对应的工作。
4).配置jetty主要就是设置connector和handler,可以由以下几种方法配置jetty:
- In code. See the examples in the Jetty 7 Latest Source XRef.
- With Jetty XML - dependency injection style XML format.
- With your dependency injection framework of choice: Spring or XBean.
- Using Jetty WebApp and Context Deployers.
没有Main函数调用jetty,没有xml配置文件,没有webapp配置文件。除了第三种使用dependency injection,其它的都可以排除。
找到了切入点,Argo通过依赖注入配置jetty。
什么是依赖注入呢?
- 依赖注入
http://stackoverflow.com/a/140655/732267
http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html
//<-强烈推荐
DependencyInjection.pdf中例子:
class MovieLister...
public Movie[] moviesDirectedBy(String arg) {
List allMovies = finder.findAll(); //<---
for (Iterator it = allMovies.iterator(); it.hasNext();) {
Movie movie = (Movie) it.next();
if (!movie.getDirector().equals(arg)) it.remove();
}
return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]);
}
使用依赖注入,能够消除应用程序类MovieLister对具体MovieFinder类的依赖。MovieLister的使用者可以根据自己的环境插入一个合适的MovieFinder实现。
依赖注入的实现方法有:构造子注入(Constructor Injection)、设值注入(Setter Injection)、接口注入(Interface Injection)
Argo中HelloController的基类实现了ArgoController接口
/**
* 所有的Controller必须实现的接口
*
*/
public interface ArgoController {
/**
* Controller被injector实例化后,将立即调用本方法进行初始化,代替构造函数
*/
void init();
}
由此可见,Argo中使用的是接口注入。
- web container
jetty通过接口注入,获得配置信息。同时在这个过程中启动Hello world应用。
tomcat和jetty也是类似的
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/</path> <!--/argo -->
<port>80</port>
</configuration>
</plugin>
- 总结
Argo使用接口注入的方式实现的依赖注入,相关的接口为com.bj58.argo.ArgoController。
Argo通过依赖注入实现配置,jetty/tomcat根据配置决定是否将请求交给。
本文介绍了Argo,一个轻量级的Java Web框架,以及其与Jetty容器的结合。通过阅读源码,作者发现Argo通过依赖注入配置Jetty,特别是使用接口注入的方式。Jetty的`jetty:run`命令根据指定的资源部署应用,而在Argo的示例中,没有使用XML或web.xml配置文件。作者强调了依赖注入的概念,提到了构造子注入、设值注入和接口注入,并指出Argo使用了接口注入。最后,文章总结了Argo如何利用Jetty作为web容器启动并处理请求。
5303

被折叠的 条评论
为什么被折叠?



