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.
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.
So my question(s):
- a) Am I fundamentally wrong in my understanding of
@Componentannotation? Does it NOT makeMyTaskinto a spring-managed singleton? - b) Is there some other annotation I should use so that spring detects
@Autowiredand injects the property? - c) Is spring autowiring not meant for non-singleton containers/classes like
MyTask?
Update # 1 - These don't work:
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
}
}
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.