SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException(无法加载ApplicationContext)
笛里谁知壮士心,沙头空照征人骨。这篇文章主要讲述SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException:无法加载ApplicationContext相关的知识,希望能为你提供帮助。
我对Spring很新。我正在使用SpringBoot + Mybatis + mysql,当我为UserDAO进行单元测试时出现此异常:“java.lang.IllegalStateException:无法加载ApplicationContext”。我按照此链接设置我的测试环境:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-test-autoconfigure/。我搜索了很多,但仍然无法找到解决方案。有人可以给我一些建议吗?非常感谢提前:)
这是我的Mybatis配置文件:
<
?xml version="1.0" encoding="UTF-8"?>
<
!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<
settings>
<
!-- Globally enables or disables any caches configured in any mapper under this configuration -->
<
setting name="cacheEnabled" value="https://www.songbingjia.com/android/true"/>
<
!-- Sets the number of seconds the driver will wait for a response from the database -->
<
setting name="defaultStatementTimeout" value="https://www.songbingjia.com/android/3000"/>
<
!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
<
setting name="mapUnderscoreToCamelCase" value="https://www.songbingjia.com/android/true"/>
<
!-- Allows JDBC support for generated keys. A compatible driver is required.
This setting forces generated keys to be used if set to true,
as some drivers deny compatibility but still work -->
<
setting name="useGeneratedKeys" value="https://www.songbingjia.com/android/true"/>
<
/settings>
<
!-- Continue going here -->
<
/configuration>
这是我的pom.file:
<
?xml version="1.0" encoding="UTF-8"?>
<
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<
modelVersion>
4.0.0<
/modelVersion>
<
groupId>
com.jingjie<
/groupId>
<
artifactId>
forum_demo<
/artifactId>
<
version>
0.0.1-SNAPSHOT<
/version>
<
packaging>
jar<
/packaging>
<
name>
forum_demo<
/name>
<
description>
Demo project for Spring Boot<
/description>
<
parent>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-parent<
/artifactId>
<
version>
1.5.9.RELEASE<
/version>
<
relativePath/>
<
!-- lookup parent from repository -->
<
/parent>
<
properties>
<
project.build.sourceEncoding>
UTF-8<
/project.build.sourceEncoding>
<
project.reporting.outputEncoding>
UTF-8<
/project.reporting.outputEncoding>
<
java.version>
1.8<
/java.version>
<
/properties>
<
dependencies>
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-aop<
/artifactId>
<
/dependency>
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-freemarker<
/artifactId>
<
/dependency>
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-web<
/artifactId>
<
/dependency>
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-devtools<
/artifactId>
<
scope>
runtime<
/scope>
<
/dependency>
<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-starter-test<
/artifactId>
<
scope>
test<
/scope>
<
/dependency>
<
dependency>
<
groupId>
mysql<
/groupId>
<
artifactId>
mysql-connector-java<
/artifactId>
<
scope>
runtime<
/scope>
<
/dependency>
<
dependency>
<
groupId>
org.mybatis<
/groupId>
<
artifactId>
mybatis<
/artifactId>
<
version>
3.4.5<
/version>
<
/dependency>
<
dependency>
<
groupId>
org.mybatis.spring.boot<
/groupId>
<
artifactId>
mybatis-spring-boot-starter-test<
/artifactId>
<
version>
1.3.1<
/version>
<
/dependency>
<
dependency>
<
groupId>
org.springframework<
/groupId>
<
artifactId>
spring-jdbc<
/artifactId>
<
version>
5.0.2.RELEASE<
/version>
<
/dependency>
<
!--<
dependency>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-configuration-processor<
/artifactId>
<
optional>
true<
/optional>
<
/dependency>
-->
<
/dependencies>
<
build>
<
plugins>
<
plugin>
<
groupId>
org.springframework.boot<
/groupId>
<
artifactId>
spring-boot-maven-plugin<
/artifactId>
<
/plugin>
<
/plugins>
<
/build>
这是我的UserDao
@Mapper
public interface UserDao {static final String USER_TABLE = "user";
final String INSERT_FIELDS = " name, password, salt, head_url";
final String SELECT_FIELDS = " id, name, password, salt, head_url";
// insert a record into user table
@Insert({"Insert into ", USER_TABLE, " (" + INSERT_FIELDS + ") values " +
"(#{name}, #{password}, #{salt}, #{headUrl})"})
void addUser(User user);
// select a record according to a given user id
@Select({"select ", SELECT_FIELDS, " from ", USER_TABLE, " where id = #
{id}"})
User getUserViaId(int id);
// select a record according to a given user name
@Select({"select ", SELECT_FIELDS , " from ", USER_TABLE, " where name = #
{name}"})
User getUserViaName(String name);
// update a user's password accroding to a given user id
@Update({"update ", USER_TABLE, " set password = #{password} where id = #
{id}"})
void updatePassword(int id);
// delete a record accroding to a given user id
@Delete({"delete from ", USER_TABLE, " where id = #{id]"})
void deleteRecordViaId(int id);
}
这是我的applications.properties:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/forum_demo?
useUnicode=true&
amp;
characterEncoding=utf8&
amp;
useSSL=false
spring.datasource.username=root
spring.datasource.password=*****
mybatis.config-location=classpath:src/main/resources/mybatis-config.xml
这是我的Test类:
package com.jingjie.forum_demo.daotest;
import com.jingjie.forum_demo.dao.UserDao;
import com.jingjie.forum_demo.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.boot.test.autoconfigure.MybatisTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Random;
@RunWith(SpringRunner.class)
//@SpringBootApplication
@EnableAutoConfiguration
@MybatisTest
//@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Rplace.NONE)
//@Sql("/forum_demo.sql")
public class UserDaoTest {@Autowired
UserDao userDaoTest;
@Test
public void addUserTest() {Random random = new Random();
for (int i = 0;
i <
11;
i ++) {
// perform some test
}
}
}
这是测试失败信息:
java.lang.IllegalStateException: Failed to load ApplicationContextat org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.jingjie.forum_demo.daotest.UserDaoTest];
nested exception is java.lang.IllegalStateException: Unable to read meta-data for class org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:556)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:185)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 25 more
Caused by: java.lang.IllegalStateException: Unable to read meta-data for class org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:217)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationValue(AutoConfigurationSorter.java:198)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.readBefore(AutoConfigurationSorter.java:186)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.<
init>
(AutoConfigurationSorter.java:158)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClasses.<
init>
(AutoConfigurationSorter.java:115)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter.getInPriorityOrder(AutoConfigurationSorter.java:57)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.sort(AutoConfigurationImportSelector.java:241)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.selectImports(AutoConfigurationImportSelector.java:98)
at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:547)
... 38 more
Caused by: java.io.FileNotFoundException: class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.core.type.classreading.SimpleMetadataReader.<
init>
(SimpleMetadataReader.java:50)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:98)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:89)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:76)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:93)
at org.springframework.boot.autoconfigure.AutoConfigurationSorter$AutoConfigurationClass.getAnnotationMetadata(AutoConfigurationSorter.java:213)
... 46 more
Process finished with exit code 255
答案【SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException(无法加载ApplicationContext)】我已经解决了这个问题,我在pom.file中使用了错误的Mybatis包。这就是为什么由于代码是正确的,但它显示了这样的错误
推荐阅读
- Spring 5 MVC Angular 5 App投掷404错误
- 如何直接从Android应用程序发送电子邮件而不显示设备的默认电子邮件形式()
- 如何将动态数据传递到sendgrid webapp上设计的电子邮件模板( ( - | Sendgrid))
- Opencv(Jetmap或colormap为灰度,反向applyColorMap())
- Android工作室中的Opencv集成,用于应用程序开发
- 为什么@GetMapping方法在发送响应时返回请求参数()
- 在android studio中反序列化操作wcf的请求消息体时出错
- AutoMapper(4.2+)Profile是逻辑还是基础设施的一部分()
- 如何为Xamarin.Android AlarmManager设置准确的时间