2012-02-10 12 views
2

タイトルは私の問題を説明しています。コンストラクターがクラスを受け入れるGuice `Module`を挿入するには?

など。

public class EntryDAOModule extends AbstractModule { 

    @Override 
    protected void configure() { 
     bind(EntryDAO.class).to(EntryDTOMongoImpl.class); // what should this be?  
    } 
} 

示すように、何が.toへのパラメータである必要があり、以下に挙げる:

私はそうのような EntryDAOMongoImplクラスインスタンス化するにはどうすればよい
public class GenericDAOMongoImpl<T, K extends Serializable> extends BasicDAO<T, K> { 
    public GenericDAOMongoImpl(Class<T> entityClass) throws UnknownHostException { 
     super(entityClass, ConnectionManager.getDataStore()); 
    } 
} 

public class EntryDAOMongoImpl extends GenericDAOMongoImpl<EntryDTOMongoImpl, ObjectId> implements EntryDAO<EntryDTOMongoImpl> { 
    private static final Logger logger = Logger.getLogger(EntryDAOMongoImpl.class); 

    @Inject 
    public EntryDAOMongoImpl(Class<EntryDTOMongoImpl> entityClass) throws UnknownHostException { 
     super(entityClass); 
    } 
    ... 
} 

:あなたが行っている何

Injector injector = Guice.createInjector(new EntryDAOModule()); 
this.entryDAO = injector.getInstance(EntryDAO.class); // what should this be? 
+0

あなたはスーパーGenericDAOMongoImplの()コンストラクタにEntryDAOMongImplのコンストラクタからEntryDTOMongoImpl.classを供給しようとしている場合に役立ちます私に教えてください? –

+0

@johncarl - はいごめんなさい - 明確にするために 'GenericDAOMongoImpl'クラスを追加しました。 – wulfgarpro

+0

ああ、私は...その道で答えを見る –

答えて

2

をここに必要なのは工場を作ることです。補助注射を使用すると、ここでお手伝いできます。

あなたが見ることができる私のprevious post regarding assisted injection

が、ここにあなたのケースのための正確なソリューションです:

EntryDAOMongoImpl:

public class EntryDAOMongoImpl extends GenericDAOMongoImpl<EntryDTOMongoImpl, ObjectId> implements EntryDAO<EntryDTOMongoImpl> { 
    private static final Logger logger = Logger.getLogger(EntryDAOMongoImpl.class); 

    @Inject 
    public EntryDAOMongoImpl(@Assisted Class<EntryDTOMongoImpl> entityClass) throws UnknownHostException { 
     super(entityClass); 
    } 
    ... 
} 

工場:

public interface EntryDAOFactory { 
    public EntryDAOMongoImpl buildEntryDAO(Class<EntryDTOMongoImpl> entityClass); 
} 

モジュール:

public class EntryDAOModule extends AbstractModule { 

    @Override 
    protected void configure() { 
     //bind(EntryDAO.class).to(EntryDAOMongoImpl.class); // what should this be? 

     FactoryModuleBuilder factoryModuleBuilder = new FactoryModuleBuilder(); 
     install(factoryModuleBuilder.build(EntryDAOFactory.class)); 
    } 
} 

使用法:

Injector injector = Guice.createInjector(new EntryDAOModule()); 
EntryDAOFactory factory = injector.getInstance(EntryDAOFactory.class); 
this.entryDAO = factory.buildEntryDAO(entityClass); 

あなたはsingelton(IMOシングルトンの自然な使い方)としてEntryDAOMongoImplを使用しようとしているなら、あなたは自分のモジュール内でアシスト射出せずに、次の操作を行うことができます。

public class EntryDAOModule extends AbstractModule { 

    @Override 
    protected void configure() { 
     EtnryDTOMongoImpl dto = new EntryDTOMongoImpl(TargetEntry.class); //guessing here 
     bind(EntryDAO.class).toInstance(new EntryDAOMongoImpl(dto)); // singleton 
    } 
} 

はそれが

+0

これは働いた;私はDIとGuiceにもっと読まなければならないでしょう - これが目的を破るようですね?私は今、上記のイディオムに続くすべてのDTO用のファクトリを作成する必要があります。また、別のDTO実装を使用する場合は、2か所で変更する必要があります。 「EntryDAOFactory」と使用クラスに含まれています。 – wulfgarpro

+0

目的を一切破りません。非@ Assisted注入​​依存関係を同じコンストラクタに含めることができます。この技術は、Factoryの定型文のトンを軽減します。 –

+0

あなたの質問を考慮して、Iveは別の方法を追加しました。 –

関連する問題