`
DavyJones2010
  • 浏览: 154865 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Guice Webapp Insight

阅读更多

guice web dataflow:

Basic Design Concept:

  1) HttpRequest/HttpResponse will always be created by WebContainer(jetty/tomcat) and Guice will reuse/wrap them.

  2) For Servlet that match the url pattern in ServletModule, they will be created by Guice instead, their service() will called by guice filtering.

  3) For Servlet that doesn't match the url pattern defined in ServletModule, they will be created by WebContainer directly.

 

1) Listener:

<listener>
	<listener-class>edu.xmu.webapp.core.MyListener</listener-class>
</listener>
public class MyListener extends GuiceServletContextListener {
	@Override
	protected Injector getInjector() {
		return Guice.createInjector(new MyServletModule());
	}
}
public class MyServletModule extends ServletModule {
	@Override
	protected void configureServlets() {
		serve("*.html").with(MyServlet.class);
	}
}

2) Listener startup:

public abstract class GuiceServletContextListener
    implements ServletContextListener {
  public void contextInitialized(ServletContextEvent servletContextEvent) {
    final ServletContext servletContext = servletContextEvent.getServletContext();

    GuiceFilter.servletContext = new WeakReference<ServletContext>(servletContext);

    Injector injector = getInjector(); // will invoke our own getInjector() in listener; MyServletModule.configureServlets() will be invoked
    injector.getInstance(InternalServletModule.BackwardsCompatibleServletContextProvider.class)
        .set(servletContext);
    servletContext.setAttribute(INJECTOR_NAME, injector);
  }
}
// MyListener
@Override
protected Injector getInjector() {
	return Guice.createInjector(new MyServletModule());
}
// Guice
public static Injector createInjector(Stage stage,
   Iterable<? extends Module> modules) {
   return new InternalInjectorCreator()
     .stage(stage)
     .addModules(modules)
     .build();
}
// Elements
public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules) {
  RecordingBinder binder = new RecordingBinder(stage);
  for (Module module : modules) {
    binder.install(module); // here our own module(MyServletModule) will be installed
  }
  return Collections.unmodifiableList(binder.elements);
}
// MyServletModule
@Override
protected void configureServlets() {
	serve("*.html").with(MyServlet.class);
}

3) Filter:

<filter>
	<filter-name>guiceFilter</filter-name>
	<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>guiceFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

4) Serve Request with guice created servlet:

    Request URL:

http://localhost:8080/webapp-core/aaa.html
// GuiceFilter
public void doFilter(ServletRequest servletRequest,
    ServletResponse servletResponse, FilterChain filterChain) {
 filterPipeline.dispatch(servletRequest, servletResponse, filterChain);
}
// ManagedFilterPipeline
public void dispatch(ServletRequest request, ServletResponse response,
    FilterChain proceedingFilterChain) {
  new FilterChainInvocation(filterDefinitions, servletPipeline, proceedingFilterChain)
        .doFilter(withDispatcher(request, servletPipeline), response);
}
// FilterChainInvocation
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse){
  servletPipeline.service(servletRequest, servletResponse);
}
// ServletPipeline
public boolean service(ServletRequest request, ServletResponse response) {
  //stop at the first matching servlet and service
  for (ServletDefinition servletDefinition : servletDefinitions) {
    if (servletDefinition.service(request, response)) { // Here our own servlet is invoked
      return true;
    }
  }
  //there was no match...
  return false;
}

5) Serve Request with web container created servlet:

http://localhost:8080/webapp-core/formalServlet
<servlet>
	<servlet-name>formalServlet</servlet-name>
	<servlet-class>edu.xmu.webapp.servlet.FormalServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>formalServlet</servlet-name>
	<url-pattern>/formalServlet</url-pattern>
</servlet-mapping>
// GuiceFilter
public void doFilter(ServletRequest servletRequest,
    ServletResponse servletResponse, FilterChain filterChain) {
  filterPipeline.dispatch(servletRequest, servletResponse, filterChain);
}
// ManagedFilterPipeline
public void dispatch(ServletRequest request, ServletResponse response,
    FilterChain proceedingFilterChain) {
  new FilterChainInvocation(filterDefinitions, servletPipeline, proceedingFilterChain)
      .doFilter(withDispatcher(request, servletPipeline), response);
}
// FilterChainInvocation
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
  index++;

  // Our url doesn't match any of defined guice serve url, thus serviced is false
  final boolean serviced = servletPipeline.service(servletRequest, servletResponse);

  //dispatch to the normal filter chain only if one of our servlets did not match
  if (!serviced) {
    proceedingChain.doFilter(servletRequest, servletResponse);
  }
}
// proceedingChain is an instance of "FilterChain", it will invoke all the following filters configed in web.xml, and then invoke correspond service() in target Servlet.
// From here, guice has transferred the filter/servlet control to web container(tomcat/jetty).

 

Reference Links:

1) http://insidecoding.com/2013/02/05/using-google-guice-in-web-applications/

 

分享到:
评论

相关推荐

    guice.jar/guice.jar

    guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar

    guice-3.0.rar

    Guice-3.0是Guice的一个版本,包含了核心库guice-3.0.jar,以及与Spring和Struts2集成的扩展库guice-spring-3.0.jar和guice-struts2-plugin-3.0.jar。 1. **Guice核心概念**: - **依赖注入**:Guice的核心机制,...

    基于guice的简单项目

    **基于Guice的简单项目** 在Java开发中,依赖注入(Dependency Injection,简称DI)是一种设计模式,它有助于降低代码间的耦合度,提高代码的可测试性和可维护性。Guice是Google提供的一款轻量级的DI框架,它简化了...

    Guice用户中文指南

    ### Guice用户中文指南 #### 一、简介 Guice是一个专门为Java 5及后续版本设计的超轻量级依赖注入框架。它旨在简化应用程序组件之间的依赖管理,并提供了一个更为简洁、灵活的方式来处理对象间的耦合关系。Guice的...

    Guice 中文文档 例子

    **Guice 概述** Guice 是 Google 推出的一款轻量级的依赖注入框架,专为 Java 5 及其后续版本设计。依赖注入(Dependency Injection,简称 DI)是一种设计模式,它允许开发者在不直接创建对象的情况下,将依赖关系...

    guice-4.0-API文档-中文版.zip

    赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版...

    google guice 3.0源码

    Google Guice,全称为GoogleInject,是一个轻量级的依赖注入框架,由Google开发并开源。Guice的目标是简化Java应用程序的构造和管理,通过自动装配对象依赖关系,让开发者可以专注于业务逻辑而不是对象的创建和组装...

    Google Guice: Agile Lightweight Dependency Injection Framework

    ### Google Guice: 敏捷轻量级依赖注入框架详解 #### 一、引言与背景 在现代软件开发中,依赖注入(Dependency Injection, DI)已成为构建灵活、可维护和可测试应用程序的重要手段之一。Google Guice作为一款100%...

    guice超轻量级依赖注入

    Guice,全称为Google Guice,是一款由Google开发的轻量级依赖注入(Dependency Injection,简称DI)框架,主要用于简化Java应用的初始化和组件管理。依赖注入是一种设计模式,它可以帮助开发者解耦代码,提高软件的...

    shiro,guice集成

    ### Apache Shiro 与 Guice 集成详解 #### 概述 在现代软件开发过程中,集成不同的框架和技术是常见的需求。Apache Shiro 是一个强大的、易用的 Java 安全框架,提供了认证、授权、加密和会话管理功能。而 Google ...

    google Guice 1.0 用户指南 中文

    "google Guice 1.0 用户指南 中文" Guice 是一个超轻量级的、下一代的、为 Java 5 及后续版本设计的依赖注入容器。它可以帮助 Java 企业应用开发社区解决连接对象方面的问题,例如 Web 应用如何访问中间层服务、...

    初试Guice测试文件

    【标题】: Guice框架初探与测试实践 在Java世界中,依赖注入(Dependency Injection,简称DI)是一种常见的设计模式,它可以帮助我们构建松耦合、可测试的代码。Guice是Google提供的一款轻量级的DI框架,用于简化...

    初试Guice(转)

    **初试Guice** Guice,全称Google Guice,是一款轻量级的依赖注入(Dependency Injection,简称DI)框架,由Google开发并开源。它主要用于简化Java应用程序的构造和管理,通过DI来解耦代码,使代码更易于测试、维护...

    Google Guice需要的jar

    Google Guice是一个轻量级的依赖注入框架,由Google开发并维护,主要用于简化Java应用程序的构建和管理。依赖注入(Dependency Injection,简称DI)是一种设计模式,它可以帮助开发者减少代码间的耦合,提高代码的可...

    guice入门学习资料

    guice 学习资料,快速掌握guice的编程技巧以及了解其机制。

    guice-3.0-API文档-中英对照版.zip

    赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)-...

    guice-multibindings-3.0-API文档-中文版.zip

    赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...

    Guice用户指南翻译

    **标题:“Guice用户指南翻译”** **描述:** Guice是Google开发的一款轻量级依赖注入(DI)框架,它使得Java应用的组件管理变得简单。依赖注入是一种设计模式,它有助于降低代码间的耦合度,提高软件的可测试性...

Global site tag (gtag.js) - Google Analytics