当前位置: 首页 > 知识库问答 >
问题:

无法加载ApplicationContext(Spring Boot)

郝修为
2023-03-14
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/autocconfigure/orm/jpa/hibernatejpaconfiguration.class]中定义的名为“Entity ManagerFactory”的bean时出错:调用init方法失败;嵌套异常为java.lang.NoClassDefoundError:javax/xml/bind/jaxbException

java.lang.IllegalStateException:无法加载ApplicationContext

    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/");
    }
}

共有1个答案

董洲
2023-03-14

输出中的以下行:

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

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

正如@Antot所提到的,在你的graddle中添加以下依赖项应该可以解决这个问题:

compile('javax.xml.bind:jaxb-api:2.3.0')
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>
 类似资料: