【发布时间】:2020-10-30 12:24:23
【问题描述】:
我想使用ObjectFactory 创建一个带有一些自定义参数的原型bean。在我需要 bean 的地方,我有以下内容:
private final ObjectProvider<Installation> installationProvider;
public void test() {
Installation installation = installationProvider.getObject("url");
}
我已经这样配置了:
@Configuration
public class MyConfiguration {
@Bean
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public Installation createInstallation(String url) {
return new Installation(url);
}
}
但是当我启动我的应用程序时,我得到了以下异常:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method createInstallation in com.mycompany.MyConfiguration required a bean of type 'java.lang.String' that could not be found.
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
鉴于这是在启动时发生的,似乎 Spring 试图在启动时自动装配这个 bean。但是,我的理解是,SCOPE_PROTOTYPE 范围的 bean 不应该在运行时自动装配,因为我想在运行时使用不同的参数创建 bean。
【问题讨论】:
标签: java spring spring-boot