Skip to content

Instantly share code, notes, and snippets.

@martyychang
Last active April 26, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martyychang/6e9ffbe37af65a770c65663d08d39509 to your computer and use it in GitHub Desktop.
Save martyychang/6e9ffbe37af65a770c65663d08d39509 to your computer and use it in GitHub Desktop.
Unsure whether using `ObjectFactory` is the best way to get a new prototype bean.
@Service
public class IntegrationService {
@Autowired
private TaskExecutor integrationTaskExecutor;
@Autowired
private IntegrationTaskFactory integrationTaskFactory;
/**
* This is the method that calls a factory method to get a new QueueTask
* object, which is what I'm trying to make a bean.
*/
public void runQueueTask(Map<String, String> params) {
integrationTaskExecutor.execute(
integrationTaskFactory.getQueueTask(params));
}
}
@Component
public class IntegrationTaskFactory {
/**
* Is this how I should be getting new instances of a prototype bean?
* http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/ObjectFactory.html
*/
@Autowired
private ObjectFactory<QueueTask> queueTaskFactory;
public QueueTask getQueueTask(Map<String, String> params) {
// Get a new QueueTask object
QueueTask task = queueTaskFactory.getObject();
// Customize task based on params
...
// Return the object
return task;
}
}
@Component
@Scope("prototype")
public class QueueTask {
@Autowired
private AtomicInteger dataIdCounter;
@Autowired
private IntegrationTasksRepository integrationTasks;
@Autowired
private MongoOperations mongoOps;
@Autowired
private SquadRunItemsRepository squadRunItems;
@Autowired
private AmazonS3Service amazonS3Service;
@Override
public void run() {
// Do stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment