2015-03-19 30 views
0

设置: - 拥有一个包含我的bean的Bean定义的多个配置类 - 我将从包含所有bean名称及其相应配置的数据库中提取一个字符串列表I类要实例动态使用getBean()动态创建Bean的正确方式

目前我会做就行了一个循环,然后调用传递beanName和包含bean定义配置类中的方法:

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

我会再使用返回的对象并使用反射来调用特定的Meth ods基于从数据库中提取的列表。

问:有没有适当的方法来做到这一点?因为对于我想创建的每个bean,我认为性能会受到影响。

+0

你能不能解释一下什么是用例呢? – 2015-03-19 11:31:58

+0

试图创建一个程序,我们可以动态地将算法添加到业务对象,让我们说可以创建一个记录之前,我们可以定义10个算法,在记录实际创建之前运行,这10个算法可以随时在应用程序根据需要,算法也可以被其他业务对象重用 – 2015-03-19 18:14:46

回答

0

您可以在这个帖子在春季使用4.1

我发现下面的例子 - 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())); 

} 
相关问题