gpt4 book ai didi

java - ContextConfiguration注解异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:25:10 27 4
gpt4 key购买 nike

我有一个非常简单的 Spring Boot 项目,刚刚从 Spring Boot 1.2.5.RELEASE 升级到 Spring Boot 1.3.0.M5(然后依赖于 Spring 4.2.0.RELEASE),现在我的项目无法编译。

项目:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEncryptableProperties
public class MyApp extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}

编译失败的测试(我唯一的测试ATM):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {MyApp.class})
@DirtiesContext
public class MyDAO_DataTest {

@Autowired
MyDAO dao;

@Test
public void whenDoingAtest() throws Exception {
//...
}
}

当我尝试编译时,它在我的测试文件上失败,并显示:

org.springframework.core.annotation.AnnotationConfigurationException: In AnnotationAttributes for annotation [org.springframework.test.context.ContextConfiguration] declared on [class com.example.MyDAO_DataTest], attribute [locations] and its alias [value] are declared with values of [{}] and [{class com.example.MyApp}], but only one declaration is permitted.

我找到了feature that's the origin of the exception ,但我真的不明白我能做些什么。

更新我通过更改此行“修复”了该问题:

@SpringApplicationConfiguration(classes = {MyApp.class})

...对此:

@ContextConfiguration(classes = {MyApp.class},
loader = SpringApplicationContextLoader.class)

有效地解决这个问题并允许自己工作,但我不明白为什么我必须这样做。 @SpringApplicationConfiguration 被描述为 Similar to the standard ContextConfiguration but uses Spring Boot's SpringApplicationContextLoader ,那么这是怎么回事?

最佳答案

Spring Boot 1.3.0.M5 (which then relies on Spring 4.2.0.RELEASE)

不幸的是,这是不正确的:Spring Boot 1.3.0.M5 明确依赖于 Spring Framework 4.2.1,而不是 4.2.0。

您看到的异常已在 Spring Framework 4.2.1 中得到解决,特别是在以下问题中。

Spring Boot 1.3.0 M5 中对 @SpringApplicationConfiguration 进行的更改需要 Spring Framework 4.2.1。有关详细信息,请参阅以下问题。

因此,确保您运行的是 Spring Framework 4.2.1 应该可以解决您的问题。

问候,

山姆

关于java - ContextConfiguration注解异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32742344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com