Dagger Servlet Extension

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

Dagger Application Layer Libs Dependency Injection
GroupId

GroupId

com.leacox.dagger
ArtifactId

ArtifactId

dagger-servlet
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Dagger Servlet Extension
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

Download dagger-servlet

How to add to project

<!-- https://jarcasting.com/artifacts/com.leacox.dagger/dagger-servlet/ -->
<dependency>
    <groupId>com.leacox.dagger</groupId>
    <artifactId>dagger-servlet</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.leacox.dagger/dagger-servlet/
implementation 'com.leacox.dagger:dagger-servlet:1.0.1'
// https://jarcasting.com/artifacts/com.leacox.dagger/dagger-servlet/
implementation ("com.leacox.dagger:dagger-servlet:1.0.1")
'com.leacox.dagger:dagger-servlet:jar:1.0.1'
<dependency org="com.leacox.dagger" name="dagger-servlet" rev="1.0.1">
  <artifact name="dagger-servlet" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.leacox.dagger', module='dagger-servlet', version='1.0.1')
)
libraryDependencies += "com.leacox.dagger" % "dagger-servlet" % "1.0.1"
[com.leacox.dagger/dagger-servlet "1.0.1"]

Dependencies

compile (5)

Group / Artifact Type Version
com.google.guava : guava jar 16.0
com.squareup.dagger : dagger jar 1.2.1
org.slf4j : slf4j-api jar 1.7.6
com.squareup.dagger : dagger-compiler Optional jar 1.2.1
javax.inject : javax.inject jar 1

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.0.1

test (2)

Group / Artifact Type Version
org.easymock : easymock jar 3.2
org.testng : testng jar 6.8.7

Project Modules

There are no modules declared in this project.

dagger-servlet

Dagger Servlet is a port of the Guice Servlet project and Jersey Guice project for use with Dagger injection. It is split into two projects, dagger-servlet and dagger-jersey, for servlets and jersey respectively. Similarly to guice-servlet and jersey-guice these projects allow you to move most of your web application configuration out of the web.xml and into your Java code.

Using dagger-servlet

Include the dagger-servlet jar

<dependency>
  <groupId>com.leacox.dagger</groupId>
  <artifactId>dagger-servlet</artifactId>
  <version>1.0.1</version>
</dependency>

Configuring the web.xml

Create a ServletContextListener for your application that extends from DaggerServletContextListener. Add your application context listener and DaggerFilter to the web.xml file.

<web-app>
    <listener>
        <listener-class>com.example.MyServletContextListener</listener-class>
    </listener>
    <filter>
        <filter-name>Dagger Filter</filter-name>
        <filter-class>com.leacox.dagger.servlet.DaggerFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Dagger Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Configuring Dagger modules

Dagger modules can be configured for application wide scope and for request scope. Start by creating an application wide Dagger module containing all of your application wide bindings. The application wide module should declare ServletModule as an include to get the dagger-servlet defined bindings. Next create another module for all of your request scoped bindings. Any @Singleton bindings defined in the request scoped module will be singletons for a given request. The request module must declare ServletRequestModule as an include to get the dagger-servlet request scoped bindings.

Add the modules to your DaggerServletContextListener implementation so that the dagger injections are available when the web application starts up.

@Module(injects = {...}, includes = ServletModule.class)
public class MyModule {}

@Module(injects = {...}, includes = ServletRequestModule.class)
public class MyRequestModule {}

public class MyServletContextListener extends DaggerServletContextListener {
    @Override
    protected Class<?>[] getBaseModules() {
        return new Class<?>[]{MyModule.class};
    }

    @Override
    protected Class<?>[] getRequestScopedModules() {
        return new Class<?>[]{MyRequestModule.class};
    }
}

Binding Servlets and Filters

With DaggerServletContextListener servlet and filter mappings can be configured in code instead of the web.xml file. These bindings are configured in the configureServlets method. Filters will be applied in the order they are declared.

public class MyServletContextListener extends DaggerServletContextListener {
    @Override
    protected void configureServlets() {
        filter("/my/*").through(MyFilter.class);
        
        serve("/my/*").with(MyServlet.class);
    }
}

Every Servlet and Filter class must be a singleton. If you cannot add the @Singleton binding, then they must be defined in a singleton @Provides method.

Using request scope

All request scoped bindings should be configured in a module that is included in the DaggerServletContextListener#getRequestScopedModules. To create a binding that has the same lifetime as a request declare it in your request scoped module and annotate the binding as @Singleton. Non-singleton bindings in the request module will create a new instance for each injection. Your module should also include ServletRequestModule to get the request scoped bindings provided by dagger-servlet.

ServletRequestModule provides the following request scoped bindings:

  • javax.servlet.ServletRequest
  • javax.servlet.ServletResponse
  • javax.servlet.http.HttpSession

Using dagger-jersey

Include dagger-jersey jar

<dependency>
  <groupId>com.leacox.dagger</groupId>
  <artifactId>dagger-jersey</artifactId>
  <version>1.0.1</version>
</dependency>

Configuring dagger-jersey

Start by configuring the web.xml file just like above for dagger-servlet. dagger-jersey provides JerseyModule and JerseyRequestModule that should be included in your application wide module and request module respectively. Jersey resource classes are typically request scoped, so you should add them to the injects field of your request module and mark them as @Singleton. Finally bind the DaggerContainer to /*.

Below is a simple example of a DaggerServletContextListener implementation and modules for Jersey:

MyContextListener extends DaggerServletContextListener {
   @Override
   protected Class<?>[] getBaseModules() {
       return new Class<?>[]{ MyAppModule.class };
   }

   @Override
   protected Class<?>[] getRequestScopedModules() {
       return new Class<?>[]{ MyRequestModule.class };
   }

   @Override
   protected void configureServlets() {
       serve("/*").with(DaggerContainer.class);
   }
}

@Module(
        injects = MyClass.class,
        includes = JerseyModule.class
)
class MyAppModule {
    /* Your app wide provides here {@literal *}/
}

@Module(
        injects = {
            MyRequestScopedClass.class,
            MyResource.class
        },
        includes = JerseyRequestModule.class
)
class MyRequestModule {
    /* Your request scoped provides here {@literal *}/
}

@Singleton
class MyResource {}

Versions

Version
1.0.1
1.0.0