Java Scope#u原型bean在启动时被实例化,但由于它可以';自动线
我想使用Java Scope#u原型bean在启动时被实例化,但由于它可以';自动线,java,spring,spring-boot,Java,Spring,Spring Boot,我想使用ObjectFactory创建一个带有一些自定义参数的原型bean。在我需要豆子的地方,我有以下几点: private final ObjectProvider<Installation> installationProvider; public void test() { Installation installation = installationProvider.getObject("url"); } 但当我启动应用程序时,会出现以下异常:
ObjectFactory
创建一个带有一些自定义参数的原型bean。在我需要豆子的地方,我有以下几点:
private final ObjectProvider<Installation> installationProvider;
public void test() {
Installation installation = installationProvider.getObject("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
scope\u原型的bean不应该在运行时自动连接,因为我想在运行时使用不同的参数创建bean。我发现了问题。我有一个单独的服务,这是我刚才为测试而创建的(忘记了):
由于此服务是在启动时连接的,显然它还将尝试自动连接依赖bean。我已经设置了一个类似于您的示例项目,它可以按预期工作。每次调用installationProvider.getObject(“url”)都会返回installationbean的一个新实例
您确定您的应用程序没有在另一个类中通过@Autowire访问bean吗
***************************
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.
@Service
public class TestSomething {
private final Installation installation;
@Autowired
public TestSomething(Installation installation) {
this.installation = installation;
}
}