【发布时间】:2018-09-09 09:54:44
【问题描述】:
我有一个实用方法,它将字符串作为输入参数并给我一个与输入对应的对象。我需要从 Spring 引导请求映射方法中调用此实用程序方法。
现在我的问题是以下两种方法的优缺点是什么?
- 将实用程序方法设为静态并调用它。
- 将方法制作为原型 bean 并调用该 bean。
方法 1 的示例代码:
**//SourceSystem can change at runtime**
public static FixedLengthReport fixedLengthReport(String sourceSystem) {
return new TdctFixedLengthReport(sourceSystem, dao);
}
方法 2 的示例代码:
@Bean
@Scope(value = "prototype")
**//SourceSystem can change at runtime**
public FixedLengthReport fixedLengthReport(String sourceSystem) {
return new TdctFixedLengthReport(sourceSystem, dao);
}
PS:从其他帖子收集的样本。
【问题讨论】:
标签: java spring spring-boot static-methods spring-bean