0

Setup : - have a Several Configuration Class containing Bean definitions for my beans - I will fetched a List of String from a database containing all the bean names and their corresponding Configuration class I want to instantiate dynamically

currently I will do a loop on the list and then call a method passing the beanName and the Configuration Class containing the bean definition:

private Object getBean(String beanName, Class configurationClass) {
    Object bean = null;
    AbstractApplicationContext context = new AnnotationConfigApplicationContext(
            configurationClass);
    bean = context.getBean(beanName);
    return bean;
}

I would then use the returned object and used Reflections to invoke specific Methods based on a list I fetched from a database.

Question : Is there a proper way to do this ? because for every bean I want to create , I think performance will be affected.

Mithun
  • 7,747
  • 6
  • 52
  • 68
  • Could you explain a little what is the use case for this? – M. Deinum Mar 19 '15 at 11:31
  • trying to create a program where we can dynamically add algorithms to business objects , lets say before a record can be created , we can define 10 algorithms that would run before the record will be actually created, those 10 algorithms can be changed anytime on the application as per needed , and the algorithms can also be re used by other business objects – Dennis Mariano Mar 19 '15 at 18:14

1 Answers1

0

You can use this in spring 4.1

I found the below example in this post - Spring MVC: How to return image in @ResponseBody?

public ResponseEntity<InputStreamResource> downloadUserAvatarImage(@PathVariable Long userId) {
GridFSDBFile gridFsFile = fileService.findUserAccountAvatarById(userId);

return ResponseEntity.ok()
        .contentLength(gridFsFile.getLength())
        .contentType(MediaType.parseMediaType(gridFsFile.getContentType()))
        .body(new InputStreamResource(gridFsFile.getInputStream()));

}
Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15