首页 文章

无法加载ApplicationContext(Spring Boot)

提问于
浏览
1

我正试图在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 .

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

1 回答

  • 0

    输出中的以下行:

    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>
    

相关问题