我想在我的 Spring MVC 应用程序中执行以下操作:
- 从我的 @Service
List<String> list
中的数据库获取一些数据(它将返回MainService
) . - 使用这个
list
作为其他@ServiceConfigFactory
的构造函数参数我目前在MainService
中@Autowired . - 从
ConfigFactory
触发一个方法获得将添加到我的 @Controller 类中的 ModelAndView 的最终结果。
我知道这样是可能的:
ConfigFactory类:
@Service
public class ConfigFactory(){
public void init(List<String> list){
//Use list to initialize ConfigFactory
}
public Result getResult(){
//Do some business logic
return result;
}
}
MainService类:
@Service
public class MainService {
@Autowired ConfigFactory configFactory;
public Result method(){
//Get list from database;
configFactory.init(list);
Result result = configFactory.getResult();
//Create a Result that will be later added to controller ModelAndView.
}
}
但是感觉不太好。所以我被困在这里了。有谁知道如何正确实现这一点?
最佳答案
为什么不将列表作为参数传递给 getResult() 方法? 我想您从 init() 方法中的列表中获取值,并初始化 ConfigFactory 中的一些属性?不要这样做,当多个用户尝试执行相同的操作时,您可能会遇到问题。
关于java - Spring中具有动态参数值的 Autowiring 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17569953/