可调用中的 Guice DI

我有一个关于在并行环境中使用 Guice 的具体问题。


我有一个 runnable,它接受构造函数参数。


class task implements Runnable{

String dbname="";

task(String dbname)

{

this.dbname=dbname

}

@

Inject

private ICacheService cacheService

public void run(){

//logic here


}

bind(ICacheService).to(CacheServiceImpl.class);



}

问题:如何实例化 Task.


我不能使用 ExecutorService.submit(新的,因为这不是正确的方法并且违背了 guice 的目的。)


慕莱坞森
浏览 161回答 2
2回答

慕沐林林

我将创建一个创建任务的工厂,通过任务中的构造函数注入 ICacheService 并利用 javax.inject.Providerpublic class TaskFactory {&nbsp; &nbsp; private Provider<ICacheService> cacheServiceProvider;&nbsp; &nbsp; @Inject&nbsp; &nbsp; public TaskFactory(Provider<ICacheService> cacheServiceProvider) {&nbsp; &nbsp; &nbsp; &nbsp; this.cacheServiceProvider = cacheServiceProvider;&nbsp; &nbsp; }&nbsp; &nbsp; public task create(String dbname) {&nbsp; &nbsp; &nbsp; &nbsp; return new task(dbname, cacheServiceProvider.get());&nbsp; &nbsp; }}class task implements Runnable {&nbsp; &nbsp; private final String dbname;&nbsp; &nbsp; private final ICacheService cacheService;&nbsp; &nbsp; public task(String dbname, ICacheService cacheService) {&nbsp; &nbsp; &nbsp; &nbsp; this.dbname = dbname;&nbsp; &nbsp; &nbsp; &nbsp; this.cacheService = cacheService;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; }}完成此操作后,我将注入 TaskFactory,然后使用 dbName 调用 create 方法。你也可以使用Guice 的AssistedInject来做类似的事情。顺便说一句,类名应该以大写字母开头。

慕田峪7331174

经过一点谷歌搜索,我找到了解决方案。捆绑:public class DependecnyBinderModule extends AbstractModule {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void configure() {&nbsp; &nbsp; &nbsp; &nbsp; bind(CloudSync.class).to(AWSCloudSyncImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(IConfigurationPropertyInitializer.class).to(ConfigurationPropertyInitializerImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(ICloudClientProvider.class).to(CloudClientProvider.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(IQueryConnection.class).to(QueryConnectionImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(IWorkSpaceDescriptorInitialization.class).to(WorkSpaceDescriptorInitialize.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(ISubscribeQueue.class).to(SubScribeQueueImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(InitializeDataSource.class).to(InitializeDataSourceImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(InitiateQueueListening.class).to(InitiateQueueListeningImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(DataExtractor.class).to(DataExtractorImpl.class);&nbsp; &nbsp; &nbsp; &nbsp; bind(DataTransformer.class).in(Scopes.SINGLETON);&nbsp; &nbsp; &nbsp; &nbsp; bind(TaskDispatcher.class);&nbsp; &nbsp; &nbsp; &nbsp; **install(new FactoryModuleBuilder().implement(Runnable.class, MultiQueueConsumer.class).build(TaskCreatorFactory.class));**&nbsp; &nbsp; }&nbsp; &nbsp; @Provides&nbsp; &nbsp; @Singleton&nbsp; &nbsp; ThreadFactory providesThreadFactory() {&nbsp; &nbsp; &nbsp; &nbsp; return new ThreadFactoryBuilder().setDaemon(true).setNameFormat("RealTimeSync-%d")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setUncaughtExceptionHandler(new RealTimeSyncExceptionHandler()).build();&nbsp; &nbsp; }}您的 Task Dispatcher 将任务分派给 Executor。import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadFactory;import com.google.inject.Inject;/**&nbsp;* @author marwaha&nbsp;*&nbsp;*/public class TaskDispatcher {@Injectprivate ThreadFactory factory;@Injectprivate RuunableFactory factory2;&nbsp; &nbsp; private ExecutorService executor;&nbsp; &nbsp; public void dispatch(String name) {&nbsp; &nbsp; &nbsp; &nbsp; if(executor==null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; executor=Executors.newFixedThreadPool(5, factory);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; executor.submit(factory2.create(name));&nbsp; &nbsp; }}可运行工厂public interface RuunableFactory {&nbsp; &nbsp; Task create(String dbname);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java