我有一个 Spring Boot 应用程序,我在其中使用“原型”bean。我知道可以通过构造函数参数注入参数,但我想避免这种方法,因为我有许多额外的配置参数,包括其他 Bean 和我的每个实例参数。
public class FooBar {
// singleton beans that are shared between multiple instances of this class
private FooRepository fooRepository;
private BarRepository barRepository;
// instance specific settings that are SPECIFIC to the instance of this class
private String fooParameter;
private String barParameter;
private String parameterX;
private String parameterY;
private String parameterZ;
//...
}
@Configuration
public class AppConfig {
/* @Bean definition of FooRepository, BarRepository etc */
@Bean
@Scope(value = "prototype")
public FooBar getFoobar(
FooRepository fooRepository, BarRepository barRepository,
String fooParameter, String barParameter, String parameterX /* ... */) {
// works, but I want to avoid something like this
return new new Foobar(fooRepository, barRepository,
fooParameter, barParameter, parameterX, /* ... */);
}
@Bean
@Scope(value = "prototype")
public FooBar getFoobar(HashMap<String, String> moreParameters) {
Foobar foobar = new Foobar();
// inject parameters without having to implement setter calls
// I want to inject BOTH Spring Beans here and some config parameters
return foobar;
}
}
有什么方法可以让 Spring 从其内部 Bean 集和我提供的参数中进行自动装配?我想避免完全设置调用。