首页 > 解决方案 > 创建 bean 时出错;通过字段表示的不满足的依赖关系

问题描述

我有一个用于某些功能的中央库,现在我无法集成该库。该库是用 Spring Boot 编写的,包含一个类:com.common.Security。它是这样定义的:

package com.common;
....
@Service
@EnableConfigurationProperties(SecurityProperties.class)
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Security {
....
}

我尝试在另一个类中使用它:

package org.special;
import com.common.Security;
@Configuration
public class WebServiceConfig {

@Autowired
private Security security;
....
}

但我得到一些错误:

Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'WebServiceclient'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'webserviceClient': 
Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'webServiceTemplate' defined in class path resource [org/special/WebServiceConfig.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.ws.client.core.WebServiceTemplate]: Factory method 'webServiceTemplate' threw exception; 
nested exception is org.springframework.beans.factory.BeanCreationException:  
nested exception is org.springframework.beans.factory.support.ScopeNotActiveException: Error creating bean with name 'scopedTarget.Security': Scope 'request' is not active for the current thread; 
consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: 

我能做些什么来解决这个问题?

标签: javaspring-boot

解决方案


删除范围非常有帮助。我之前尝试过这个,但在那之后我的一些测试失败了。我没有看到,这是因为 application.yaml 中缺少测试设置。当要求范围时,它们不是必需的。


推荐阅读