Spring @autowired不适用于非单个容器吗? [英] Is Spring @autowired not meant for non-singleton containers?

查看:281
本文介绍了Spring @autowired不适用于非单个容器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现RunnableMyTask类,并且在任何给定时刻可以实例化许多这样的对象.我想将某些属性自动关联到MyTask类.

I have a MyTask class which implements Runnable and there can be many such objects instantiated at any given moment. There are certain properties that I would like to autowire into MyTask class.

但是我认为,如果我用@Component标记MyTask,那么它将成为弹簧管理的单例正确吗?那不是我想要的,我需要由TaskExecutor运行的此类的许多独立实例.

But I think that if I mark MyTask with @Component then it will become a spring-managed singleton correct? That's not what I want, I need many independent instances of this class to be run by a TaskExecutor.

所以我的问题是

  • a)我对@Component注释的理解从根本上来说是错误的吗?它不会使MyTask变成弹簧管理的单例吗?
  • b)我还应该使用其他注释,以便spring检测@Autowired并注入属性吗?
  • c)弹簧自动接线是否不适用于非单个容器/类,例如MyTask?
  • a) Am I fundamentally wrong in my understanding of @Component annotation? Does it NOT make MyTask into a spring-managed singleton?
  • b) Is there some other annotation I should use so that spring detects @Autowired and injects the property?
  • c) Is spring autowiring not meant for non-singleton containers/classes like MyTask?

更新#1-这些无效:

public class MyTask implements Runnable { // I want this class to be non-singleton
    @Autowired
    public SomeSpecialSpringConfiguredConnectionClass blah; // this is the singleton bean that should be injected
    @Override
    public void run() {
        // BLAH IS NULL, this shouldn't be NULL, that is not what I want
        // which makes sense considering Spring never knew it had to work
        // on this class
    }
}

@Component
public class MyTask implements Runnable { // I want this class to be non-singleton
    @Autowired
    public SomeSpecialSpringConfiguredConnectionClass blah; // this is the singleton bean that should be injected
    @Override
    public void run() {
        // this works BUT now MyTask is singleton :(
    }
}

@Component
@Scope("prototype")
public class MyTask implements Runnable { // I want this class to be non-singleton
    @Autowired
    public SomeSpecialSpringConfiguredConnectionClass blah; // this is the singleton bean that should be injected
    @Override
    public void run() {
        // BLAH IS NULL, again ... this shouldn't be NULL, that is not what I want
    }
}


更新#2-在等待更多有关如何以简便方法进行操作的建议时,我正在研究:Using AspectJ to dependency inject domain objects with Spring作为替代方法.


Update # 2 - While waiting for some more suggestions on how to do it the easy way, I'm looking into: Using AspectJ to dependency inject domain objects with Spring as an alternative.

推荐答案

首先,使用@Component声明并由spring组件扫描拾取的bean默认情况下将成为受spring管理的 singleton .

first, beans declared with @Component and picked up by spring component scan will become a spring-managed singleton by default.

我不知道您如何使用MyTask,但是在您遇到的情况下使用AspectJ实在是太过节制了,并且将MyTask声明为弹簧管理的bean并没有多大意义.另一种方法是:

I have no idea how you use MyTask, but it is overkilled to use AspectJ in your situation, and it does not make much sense to declare MyTask as a spring-managed bean. another way of doing this will be:

  1. 将MyTask定义为纯Java类,并添加一个构造函数以初始化依赖项blah

  1. define MyTask as a plain java class and add a constructor to initialize the dependency blah

autowire blah ,并在每次要执行任务时实例化MyTask对象,如下所示:

autowire blah in where you use MyTask, and instantiate a MyTask object every time you want to execute a task as follow:

//autowire the dependency of MyTask in another spring bean with default singleton scope
@Autowired private SomeSpecialSpringConfiguredConnectionClass blah
//create task and wire the blah yourself
executor.submit(new MyTask(blah))

这篇关于Spring @autowired不适用于非单个容器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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