自动装配任务发送给Spring TaskExecutor [英] Autowiring Tasks sent to Spring TaskExecutor

查看:248
本文介绍了自动装配任务发送给Spring TaskExecutor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何拥有一个实现Runnable并自动连线到springs TaskExecutor的类?

How can you have a class that implements Runnable and submitted to springs TaskExecutor autowired?

例如,我有一个任务:

public class MyTask implements Runnable {

    @Autowired private MyRepository myRepository;

    @Override
    public void run() {
        myRepository.doSomething();
    }
}

还有一个将任务发送到spring TaskExecutor的服务:

And a service that sends a task to the spring TaskExecutor:

@Service
public class MyService {

    @Autowired private TaskExecutor taskExecutor;

    public void someMethod() {

        MyTask myTask = new MyTask();
        taskExecutor.execute(myTask);

    }

}

我知道这些字段没有自动接线,因为MyTask正在使用新的MyTask()实例化.但是,我该如何解决?我应该可以访问Spring的ApplicationContext并通过它创建bean吗?您将如何在Web应用程序环境中做到这一点?

I know the fields aren't being autowired because MyTask is getting instantiated using new MyTask(). However, how do I get around this? Should I be getting access to Spring's ApplicationContext and create the bean through it? How would you do this in a web application environment?

谢谢!

推荐答案

至少有两种使用Spring的好方法.首先, @Configurable 注释.使用此方法意味着对AspectJ的依赖,但是它将允许您注入不受Spring管理的bean(即您正在使用new运算符).这将涉及使用@Configurable注释MyTask,并按照链接中的说明在Spring配置中添加几行.

There are at least two good ways to do this using Spring. First, the @Configurable annotation. Using this means a dependency on AspectJ, but it will allow you to inject beans that aren't managed by Spring (i.e. you're using the new operator). This would involve annotating MyTask with @Configurable, and adding a couple of lines to your Spring configuration as mentioned in the link.

@Configurable
public class MyTask implements Runnable { ... }

@Service
public class MyService {
   @Autowired private TaskExecutor taskExecutor;

   public void someMethod() {

     // AspectJ would jump in here and inject MyTask transparently
     MyTask myTask = new MyTask();
     taskExecutor.execute(myTask);

}

}

第二种方法涉及使用Spring的ServiceLocatorFactoryBean功能来创建原型bean.最好在

The second approach would involve using the ServiceLocatorFactoryBean feature of Spring to create prototype beans. This is best explained in the JavaDoc, but in this circumstance you would inject a TaskFactory into your @Service annotated class, just like any other bean and then do something like so:

@Service
public class MyService {
  @Autowired private TaskExecutor taskExecutor;
  @Autowired private MyRepository myRepository;
  @Autowired private TaskFactory taskFactory;


public void someMethod() {
    MyTask myTask = taskFactory.getTask("myTask")
    taskExecutor.execute(myTask);
}

}

MyTask将已经随您的存储库一起注入,因为您可以在XML映射中对其进行配置.我每天都使用这两种方法,但是我倾向于使用第二种方法,因为第二种方法更易于阅读,并通过确保开发人员不会做无法轻易测试的事情来帮助他们保持诚实.随便的观察者都可以清除.

MyTask would already be injected with your repository, as you could configure this in your XML mapping. I use both of these approaches on a daily basis, but I tend to favor the second one as its easier to read and helps keep developers honest by ensuring that they don't do things that aren't easily testable, and frankly, its more clear to the casual observer.

这篇关于自动装配任务发送给Spring TaskExecutor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆