【发布时间】:2019-03-07 10:00:05
【问题描述】:
我在执行测试时遇到了这个异常:
UnsatisfiedDependencyException:创建名称为“net.gencat.transversal.espaidoc.mongo.GridFSTest”的 bean 时出错:通过字段“resourceProperties”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“net.gencat.transversal.espaidoc.ResourcesConfigProperties”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。
所以,我认为信息非常清楚:ResourcesConfigProperties is not satisfied。
我的测试:
RunWith(SpringRunner.class)
@SpringBootTest()
public class GridFSTest {
@Autowired
private GridFsTemplate gridFsTemplate;
@Autowired
private ResourcesConfigProperties resourceProperties;
public URL getHugeResource() {
try {
return Paths
.get(this.resourceProperties.getHuge())
.toUri()
.toURL();
} catch (MalformedURLException e) {
return null;
}
}
@Test
public void storeHugeFile() throws IOException {
URL resource = this.getHugeResource();
this.gridFsTemplate.store(
resource.openStream(),
resource.getPath(),
"mime"
);
}
}
而ResourcesConfigProperties 是:
@ConfigurationProperties(prefix = "files")
public class ResourcesConfigProperties {
private String huge;
/**
* @return the huge
*/
public String getHuge() {
return huge;
}
/**
* @param huge the huge to set
*/
public void setHuge(String huge) {
this.huge = huge;
}
}
进入我的src/test/resources 我有我的application.properties 文件:
files.huge: /home/jcabre/Downloads/1GB.zip
有什么想法吗?
编辑
主 Spring 启动应用程序:
@SpringBootApplication(
//scanBasePackages = { "cat.gencat.ctti.canigo.arch.web.rs" },
exclude = JmxAutoConfiguration.class
)
@EnableConfigurationProperties({
ApiProperties.class,
FileStoreProperties.class
})
@Import(RedisConfiguration.class)
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
【问题讨论】:
-
您的主 Spring 引导应用程序是什么样的?
-
我刚刚编辑了帖子。
-
将
@Component添加到ResourcesConfigProperties,因为@ConfigurationProperties不导入任何创建bean 的spring 注释。 -
你需要将
@Configuration添加到ResourcesConfigProperties类,否则它不会在spring容器中创建这个类的bean,@Autowired将不起作用。
标签: spring-boot spring-boot-test