【问题标题】:Failed to load ApplicationContext (Spring Boot)无法加载 ApplicationContext (Spring Boot)
【发布时间】:2018-10-12 21:11:03
【问题描述】:

我正在尝试按照 Spring 网站上的示例测试我的应用程序。

这些是我的依赖项:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile("org.springframework.boot:spring-boot-starter-security")
    compile('org.springframework.boot:spring-boot-starter-web')
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity4")
    runtime('org.springframework.boot:spring-boot-devtools')
    runtime('com.h2database:h2')
    runtime('mysql:mysql-connector-java')
    compileOnly('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')
}

这是测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ProductControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testHomePage() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk());
    }

}

我得到错误:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

java.lang.IllegalStateException: 无法加载 ApplicationContext

知道是什么原因造成的吗?我没有任何特定的配置文件,只有安全配置和 webconfig。

网络配置:

    public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**", "/css/**", "/fragments/**")
                .addResourceLocations("classpath:/static/images/", "classpath:/static/css/", "classpath:/fragments/");
    }
}

【问题讨论】:

  • 请发布IllegalStateException 的根本原因/堆栈跟踪。原因在于第一个异常发生,ISE 是多个异常链接的结果。
  • @Antot 更新主帖
  • 看来您需要将javax.xml.bind:jaxb-api 添加到依赖项列表中。
  • 听起来您可能在 JDK 9 或更高版本上运行?这是有意的,还是切换到 JDK 8 适合您?

标签: java spring spring-boot spring-test spring-test-mvc


【解决方案1】:

输出中的以下行:

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

告诉我们找不到类javax.xml.bind.JAXBException。

正如@Antot 提到的,将以下依赖项添加到您的 graddle 应该可以解决问题:

compile('javax.xml.bind:jaxb-api:2.3.0')

对于那些使用 Maven,请使用以下内容:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

【讨论】:

    猜你喜欢
    • 2020-09-26
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多