2013-03-29 5 views
8

私は簡単に公開できる基本JAXRSサービスがありますが、一度は依存性注入APIを使用したいと考えています.Goice Guiceは最高のものと思われます。これを念頭に置いて、私はそれを統合しようとしましたが、ドキュメントは少し重いが起こっていると私はGoogle GuiceをJaxRS(ジャージー)で使用する方法

  • のWeb.xml
  • コンテキストの適切な組み合わせを試してみて、見つけるために周り狩りに持ってきました@Singletonまたは@Requestまたは何もしてサービスに注釈を付けるかどうかリスナー(私はServletContainerまたはGuiceContainerを使用する必要があります)
  • サービス
  • (私は@Singletonで注釈を付ける必要があります - ドキュメントは、私がすべきと言うが、その後デフォルトはスコープを要求することを言います)
  • コンストラクタパラメータに注釈を付けるかどうか@InjectParam

現在のところ、私はGoogle Guiceからエラーを受け取り、@InjectParam注釈を使用するかどうかに基づいて変更します。

私は@InjectParamに注釈を付ける場合は、私はこれは私のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <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> 

    <listener> 
     <listener-class>com.hillingar.server.ServletContextListener</listener-class> 
    </listener> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
</web-app> 

これで

Mar 29, 2013 9:54:59 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: Missing dependency for constructor public com.hillingar.server.rest.UserService(com.hillingar.server.dao.interfaces.UserDao,com.hillingar.server.SessionUtility) at parameter index 0 
    SEVERE: Missing dependency for constructor public com.hillingar.server.rest.UserService(com.hillingar.server.dao.interfaces.UserDao,com.hillingar.server.SessionUtility) at parameter index 1 

を取得後、注釈を付けていない場合、私は

 Mar 29, 2013 9:52:04 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
    SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: The class com.hillingar.server.dao.interfaces.UserDao is an interface and cannot be instantiated. 
    SEVERE: Missing dependency for constructor public com.hillingar.server.SessionUtility(com.hillingar.server.dao.interfaces.UserDao) at parameter index 0 

を取得私のServletContextListenerです

01あなたがフィールドとしての代わりに、コンストラクタの一部として `UserDao`と` SessionUtility`を注入した場合

これは私のUserServiceのある

package com.hillingar.server.rest; 

import com.google.inject.Inject; 
import com.google.inject.Singleton; 
import java.util.List; 

import javax.servlet.http.HttpServletRequest; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.GET; 
import javax.ws.rs.POST; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Context; 
import javax.ws.rs.core.SecurityContext; 

import com.hillingar.server.SessionUtility; 
import com.hillingar.server.dao.interfaces.UserDao; 
import com.hillingar.server.model.User; 
import com.hillingar.server.model.dto.AuthenticationResponse; 

@Path("/user") 
@Produces("application/json") 
@Consumes({"application/xml","application/json"}) 
@Singleton // <-- Added Singleton here 
public class UserService { 

    private UserDao userDao; 
    private SessionUtility sessionManager; 

     /* 
      Error if I annotate with @InjectParam... 

      Mar 29, 2013 9:52:04 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
      SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
      SEVERE: The class com.hillingar.server.dao.interfaces.UserDao is an interface and cannot be instantiated. 
      SEVERE: Missing dependency for constructor public com.hillingar.server.SessionUtility(com.hillingar.server.dao.interfaces.UserDao) at parameter index 0 

      Error If I don't annotate at all... 
      Mar 29, 2013 9:54:59 PM com.sun.jersey.spi.inject.Errors processErrorMessages 
      SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
       SEVERE: Missing dependency for constructor public com.hillingar.server.rest.UserService(com.hillingar.server.dao.interfaces.UserDao,com.hillingar.server.SessionUtility) at parameter index 0 
       SEVERE: Missing dependency for constructor public com.hillingar.server.rest.UserService(com.hillingar.server.dao.interfaces.UserDao,com.hillingar.server.SessionUtility) at parameter index 1 

      (both output Initiating Jersey application, version 'Jersey: 1.13 06/29/2012 05:14 PM') 
     */ 
    @Inject 
    public UserService(UserDao userDao, SessionUtility sessionManager) { 
     this.userDao = userDao; 
       this.sessionManager = sessionManager; 
    } 

    @GET 
    public List<User> test(@Context HttpServletRequest hsr) { 
       // USER DAO IS ALWAYS NULL - CONSTRUCTOR INJECTION NOT WORKING 
     User loggedInUser = userDao.findBySessionId(hsr.getSession().getId()); 
       ... 
     return users; 
    } 

} 
+0

へのServletContextListenerを変更し、何が起こりますか? – condit

+0

コンストラクタをパラメータなし(かつ@Injectを削除)に変更し、UserDaoとSessionUtilityの両方に対してgetterとsetter(@Injectを持つセッタ)を作成しました。それでもどちらも呼ばれることはありません。 –

+0

今朝私は2つのことを変更した後、期待通りに動作し始めました.1)サーブレットコンテキストリスナを変更してGuiceServletContextListenerを拡張し、getInjectorメソッドをオーバーライドしました。2)インジェクタ内でserve( "/ rest/*" ).with(GuiceContainer.class);そして今度はそれが働いた! –

答えて

5

package com.hillingar.server; 

import java.util.logging.Logger; 

import javax.servlet.ServletContextEvent; 

import com.google.inject.Guice; 
import com.google.inject.Injector; 
import com.google.inject.Singleton; 
import com.google.inject.servlet.GuiceServletContextListener; 
import com.hillingar.server.dao.jdbcImpl.UserJdbc; 
import com.hillingar.server.dao.interfaces.UserDao; 
import com.hillingar.server.rest.UserService; 
import com.sun.jersey.guice.JerseyServletModule; 
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer; 
import com.sun.jersey.spi.container.servlet.ServletContainer; 

// (1) Extend GuiceServletContextListener 
public class ServletContextListener extends GuiceServletContextListener { 

    Logger logger = Logger.getLogger(this.getClass().getName()); 

    // (1) Override getInjector 
    @Override 
    protected Injector getInjector() { 
     return Guice.createInjector(new JerseyServletModule() { 
      @Override 
      protected void configureServlets() { 
       bind(SecurityFilter.class).in(Singleton.class); 
       bind(UserService.class);// .in(Singleton.class); 
       bind(ServletContainer.class).in(Singleton.class); 

       // (2) Change to using the GuiceContainer 
       serve("/rest/*").with(GuiceContainer.class); // <<<<--- 

       bind(UserDao.class).to(UserJdbc.class); 
       bind(SessionUtility.class); 
      } 
     }); 
    } 
} 
関連する問題