我有一个非常简单的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:在[class com.example.MyDAO_DataTest]中声明的注解[org.springframework.test.context.ContextConfiguration]的AnnotationAttributes中,属性[locations]及其别名[value]声明为值[{}]和[{class com.example.MyApp}]的名称,但只允许一个声明。


我找到了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(然后依赖于Spring 4.2.0.RELEASE)


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

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


https://jira.spring.io/browse/SPR-13325
https://jira.spring.io/browse/SPR-13345


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


https://github.com/spring-projects/spring-boot/issues/3635


因此,确保您在Spring Framework 4.2.1上运行可以解决您的问题。

问候,

山姆

09-30 14:31
查看更多