title: Using Guice dependency injection in HttpServlet with Kotlin
body: <p>Im having a trouble with injecting dependencies into my <code>HttpServlet</code>. If I use constructor injection, I get servlet instantiation error, because of absence of empty constructor (even when i use <code>kotlin-noarg</code> plugin). If I use <code>@Inject</code> on <code>lateinit var</code> I get <code>lateinit property name has not been initialized</code> error. What am I doing wrong?</p>
  
  <p>Here is my code:</p>
  
  <p>HomeController.kt</p>
  
  <pre><code>import com.google.inject.Inject
  import com.google.inject.Singleton
  import com.google.inject.name.Named
  import javax.servlet.annotation.WebServlet
  import javax.servlet.http.HttpServlet
  import javax.servlet.http.HttpServletRequest
  import javax.servlet.http.HttpServletResponse
  
  @Singleton
  @WebServlet(name = "Hello", value = ["/hello"])
  class HomeController: HttpServlet() {
  
      @Inject
      @Named("app-url")
      lateinit var name: String
  
      override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {
          res.writer.write("Hello, $name!")
      }
  }
  </code></pre>
  
  <p>MainModule.kt</p>
  
  <pre><code>class MainModule : Module {
  
      /**
       * Contributes bindings and other configurations for this module to `binder`.
       */
      override fun configure(binder: Binder) {
          val conf = ConfigFactory.load()
  
          binder.bind(String::class.java).annotatedWith(Names.named("app-url")).toInstance(conf.getString("appUrl")) // TODO: move to env
  
      }
  }
  </code></pre>
  
  <p>MyGuiceServletConfig.kt</p>
  
  <pre><code>import com.google.inject.Guice
  import com.google.inject.Injector
  import com.google.inject.servlet.GuiceServletContextListener
  
  class MyGuiceServletConfig : GuiceServletContextListener() {
      override fun getInjector(): Injector {
          return Guice.createInjector(
                  MyServletModule(),
                  MainModule()
          )
      }
  }
  </code></pre>
  
  <p>MyServletModule.kt</p>
  
  <pre><code>class MyServletModule: ServletModule() {
  
      override fun configureServlets() {
          serve("/home").with(HomeController::class.java)
      }
  }
  </code></pre>
  
  <p>webapp/WEB-INF/web.xml</p>
  
  <pre><code>&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0"&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.google.inject.servlet.GuiceFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;guiceFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;tj.alif.core.app.guice.MyGuiceServletConfig&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>