Java依赖注入-如何@Inject到可调用

Java依赖注入-如何@Inject到可调用,java,struts-1,Java,Struts 1,我有一个Struts/J2EE应用程序 我有一个类,它创建了一个可调用的 ExecutorService executor = Executors.newFixedThreadPool(NO_THREADS); List<Future<Long>> tripFutureList = new ArrayList<>(); for(Long tripId : tripIds) { Callable<Long> c

我有一个Struts/J2EE应用程序

我有一个类,它创建了一个可调用的

    ExecutorService executor = Executors.newFixedThreadPool(NO_THREADS);
    List<Future<Long>> tripFutureList = new ArrayList<>();
    for(Long tripId : tripIds) {
        Callable<Long> callable = new CallableTripAutoApprovalEscalation(tripId);
        Future<Long> future = executor.submit(callable);
        tripFutureList.add(future);
    }
    for(Future<Long> future : tripFutureList) {
        try {
            logger.fine("Processed trip auto approval escalation for trip: "+future.get());
        } catch (InterruptedException | ExecutionException e) {
            logger.severe("There was an error processing trip."+ e.getMessage());
        }
    }

    executor.shutdown();

您可以将其注入父容器并简单地传递该实例

//in your wrapping component
@Resource
private YourInjectableClass injectable;

//and then pass it as ctor arg

Callable<Long> callable = new CallableTripAutoApprovalEscalation(tripId, injectable);
Future<Long> future = executor.submit(callable);
//在包装组件中
@资源
私有YourInjectableClass可注射;
//然后将其作为ctor arg传递
Callable Callable=新CallableTripAutoApprovalEscalation(tripId,可注射);
未来=执行人提交(可调用);

您必须1)创建
可调用的
2)在其上进行注入3)提交。您要做的是'Callable Callable=new CallableTripAutoApprovalEscalation(tripId);`所以这里没有注射有效。@Antoniossss,谢谢你的评论,但我不明白你的意思?我确实使用
new
关键字创建了callable,然后尝试使用
@inject
注释将其注入。我不知道你的第三点是什么意思。是的,我知道我使用的
new
关键字会阻止它使用注入,但我的问题是如何避免这种情况。不,你不是-你通过
new
创建新实例,并期望它以某种方式工作@Inject不是一种可以自己开箱即用的神奇曼波巨无霸。它只是一个标记,您的容器使用它来检测需要注入的内容——在引擎盖下,它只是一个普通的java调用setter或通过注入设置字段。@Antoniossss,我知道。这是我的问题。我想你下面的答案会解决这个问题。
//in your wrapping component
@Resource
private YourInjectableClass injectable;

//and then pass it as ctor arg

Callable<Long> callable = new CallableTripAutoApprovalEscalation(tripId, injectable);
Future<Long> future = executor.submit(callable);