をインスタンス化されることはありません:)Guice3シングルトンは、私はGuiceのに新たなんだと、すでに立ち往生GAEプロジェクトで
私はかなりのクラスGuiceConfig、OfyFactoryと少しのようにそれを使用して(あなたが閲覧することができた)Motomapia projectからOfyを変更をコピーサンプル。
は、私はこの
@Singleton
public class OfyFactory extends ObjectifyFactory
{
Injector injector;
@Inject
public OfyFactory(Injector injector)
{
this.injector = injector;
register(Pizza.class);
register(Ingredient.class);
}
@Override
public <T> T construct(Class<T> type)
{
return injector.getInstance(type);
}
@Override
public Ofy begin()
{
return new Ofy(super.begin());
}
}
Ofyが持っていないよう
<web-app>
<listener>
<listener-class>com.mine.courierApp.server.GuiceConfig</listener-class>
</listener>
<!-- GUICE -->
<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>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<!-- My test servlet -->
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.mine.courierApp.server.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
OfyFactoryに見える私のweb.xmlにこのリスナーを追加しました。この
public class GuiceConfig extends GuiceServletContextListener
{
static class CourierServletModule extends ServletModule
{
@Override
protected void configureServlets()
{
filter("/*").through(AsyncCacheFilter.class);
}
}
public static class CourierModule extends AbstractModule
{
@Override
protected void configure()
{
// External things that don't have Guice annotations
bind(AsyncCacheFilter.class).in(Singleton.class);
}
@Provides
@RequestScoped
Ofy provideOfy(OfyFactory fact)
{
return fact.begin();
}
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector()
{
return Guice.createInjector(new CourierServletModule(), new CourierModule());
}
}
のように見えるGuiceServletContextListener
を作成しましたGuiceの注釈はすべて...
public class TestServlet extends HttpServlet
{
@Inject Ofy ofy;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ofy.save(new Pizza());
}
}
Ofy ofyは常にnullであるように私は、注入されたフィールドを使用しようとしている
public class Ofy extends ObjectifyWrapper<Ofy, OfyFactory>
{
// bunch of helper methods here
}
そして最後にテストサーブレットが見えます。決して注射されません。 OfyFactoryはインスタンス化されないため、コンストラクタは呼び出されません。
私が間違っていることを指摘してください。なぜ私のシングルトンは作られていないのですか?
ありがとうございます。
あなたは絶対に正しいです。この質問を掲載した後、私はあなたが書いたことを正確に行いました。今私はカスタムプロバイダでエラーが発生している、com.google.inject.OutOfScopeException:スコープオブジェクトにアクセスすることはできません。現在、HTTPサーブレットリクエストに含まれていないか、com.google.inject.servlet.GuiceFilterをこのリクエストのサーブレットフィルタとして適用するのを忘れている可能性があります。これは、プロバイダから '@ RequestScoped'を削除した場合に機能します。何が問題なの? – expert
'TestServlet'はシングルトンでなければならないので、' Ofy'オブジェクトが要求ごとにスコープされる必要がある場合、 '@ RequestScoped'の代わりに' Provider 'を' TestServlet'に注入する必要があります。 http://code.google.com/p/google-guice/wiki/ServletModuleのドキュメントでは、これについて非常によく説明しています。 –
これは助けになりました!ドキュメントを読んでいないと私には恥じらいます:) – expert