Last active
April 26, 2016 12:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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