Failed to load ApplicationContext 除错1

在尝试运行Spring Boot JPA测试代码时遇到了'Failed to load ApplicationContext'错误。经过排查,发现错误与JDK版本、POM配置以及数据源设置有关。通过将JDK版本从13降低到12-8,排除DataSourceAutoConfiguration,以及调整@SpringBootApplication注解,成功解决了问题,测试通过。

application.properties
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://139.23.01.23:3306/card
spring.datasource.username=AAA
spring.datasource.password=BBB@
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

POM.XML

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

spring-boot-starter-parent
org.springframework.boot
2.1.9.RELEASE

org.example
22springbootjpa
1.0-SNAPSHOT

<properties>
    <maven.compiler.source>13</maven.compiler.source>
    <maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- mysql -->
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!-- druid连报泡-->
    <dependency>
    <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.0</version>
    </dependency>

    ~~<dependency> 可以不用
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>~~ 

</dependencies>

关于测试代码报错

package mm;

import mm.App;
import mm.entry.User;
import mm.entry.UserDao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

//@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
//@RunWith(SpringRunner.class)

//报错情况:application.properties Failed to load ApplicationContext
// 上述两注解更改无效,后改JDK版本从13 改到12–8 ,一切正常,测试通过

@RunWith(SpringJUnit4ClassRunner.class)

@SpringBootTest(classes = App.class )
public class mtest {
@Autowired
private UserDao muser;

@Test
public void testadd()
{
User ent = new User();
ent.setName(“anejijsk09”);
ent.setPhone(“13956565”);
ent.setEmail(“163.com”);

this.muser.save(ent);

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值