0

私はGuiceを初めて使用し、すでに立ち往生しています:)

GuiceConfig、OfyFactoryのクラスをコピーし、Motomapiaプロジェクト(参照可能)からOfyを少し変更して、サンプルとして使用しました。

GuiceServletContextListenerこのように作成しました

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());
    }
}

このリスナーをweb.xmlに追加しました

<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は次のようになります

@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にはGuiceアノテーションがまったくありません...

public class Ofy extends ObjectifyWrapper<Ofy, OfyFactory>
{
    // bunch of helper methods here
}

そして最後に、注入されたフィールドを使用しようとしているサーブレットをテストすると、次のようになります

public class TestServlet extends HttpServlet
{
    @Inject Ofy ofy;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        ofy.save(new Pizza());
    }
}

Ofyofyは常にnullです。注入されることはありません。また、OfyFactoryがインスタンス化されることはなく、コンストラクターが呼び出されることもないため、注入されません。

私が間違っていることを指摘していただけませんか?なぜ私のシングルトンは作成されないのですか?

どうもありがとう。

4

1 に答える 1

4

TestServletweb.xmlファイルで定義する代わりに、web.xmlからそのマッピングを削除し、configureServlets()メソッドに次の行を追加してみてください。

serve("/test").with(TestServlet.class);

クラスに注釈を付けるか、を追加して、TestServletとしてバインドする必要がある場合もあります。Singleton@Singleton

bind(TestServlet.class).in(Singleton.class);

モジュールの1つに接続します。

何が起こっているのかというと、Guiceは実際にサーブレットを作成していないため、Ofyオブジェクトを挿入できません。serve(...).with(...)Guiceは、バインディングを使用して作成するように指示された場合にのみサーブレットを作成します。web.xmlで定義されているサーブレットは、Guiceの制御の範囲外です。

于 2012-02-24T03:58:35.627 に答える