Spring boot 如何解决自动配置数据源失败的问题:';spring.datasource.url';未指定,并且无法自动配置嵌入式数据源
我正在尝试在EC2中运行SpringBoot应用程序,我已经在RDS中托管了mysql。当我在本地数据库中尝试时,它工作得很好,但当我切换到Rds时,应用程序就给了我错误Spring boot 如何解决自动配置数据源失败的问题:';spring.datasource.url';未指定,并且无法自动配置嵌入式数据源,spring-boot,amazon-ec2,spring-data,amazon-rds,Spring Boot,Amazon Ec2,Spring Data,Amazon Rds,我正在尝试在EC2中运行SpringBoot应用程序,我已经在RDS中托管了mysql。当我在本地数据库中尝试时,它工作得很好,但当我切换到Rds时,应用程序就给了我错误 *************************** APPLICATION FAILED TO START *************************** Description: Failed to auto-configure a DataSource: 'spring.datasource.url' is
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not
specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
我在谷歌上搜索并尝试在应用程序中添加一些配置,但它对我不起作用
在这里,我发布了应用程序提到的类
AppUserDAO
@Repository
@Transactional
public class AppUserDAO {
@Autowired
private EntityManager entityManager;
public AppUser findUserAccount(String userName) {
try {
String sql = "Select e from " + AppUser.class.getName() + " e " //
+ " Where e.userName = :userName ";
Query query = entityManager.createQuery(sql, AppUser.class);
query.setParameter("userName", userName);
return (AppUser) query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
}
批准道
@Repository
@Transactional
public class AppRoleDAO {
@Autowired
private EntityManager entityManager;
public List<String> getRoleNames(Long userId) {
String sql = "Select ur.appRole.roleName from " + UserRole.class.getName() + " ur " //
+ " where ur.appUser.userId = :userId ";
Query query = this.entityManager.createQuery(sql, String.class);
query.setParameter("userId", userId);
return query.getResultList();
}
}
pom.xml
<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>org.o7planning</groupId>
<artifactId>SpringBootSecurityJPA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootSecurityJPA</name>
<description>Spring Boot +Spring Security + JPA + Remember
Me</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.o7planning.sbsecurity.SpringBootSecurityJpaApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
4.0.0
org.o7planning
SpringBootSecurityJPA
0.0.1-快照
罐子
SpringBootSecurityJPA
Spring Boot+Spring Security+JPA+记住
我
org.springframework.boot
spring启动程序父级
2.0.0.1版本
UTF-8
UTF-8
1.8
org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
弹簧启动安全
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
mysql
mysql连接器java
运行时
org.springframework.boot
弹簧起动试验
测试
org.springframework.security
弹簧安全性试验
测试
org.springframework.boot
springbootmaven插件
org.o7planning.sbsecurity.springbootsecurityjpaa应用程序
重新包装
请帮助我解决此问题,尝试在DAO中按构造函数而不是字段注入EntityManager依赖项。 此外,您还可以在主Spring Boot应用程序类中初始化bean,如下所示:
@Bean
public EntityManager entityManager() {
return new EntityManager();
}
试试这个
@PersistenceContext
private EntityManager entityManager;
编辑
经过相关编辑
无法自动配置数据源:“spring.DataSource.url”未指定,无法自动配置嵌入式数据源
原因:无法确定合适的驱动程序类别
您必须在application.properties中提到mysql的驱动程序
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
(or)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
为什么要排除数据源配置?如果没有数据源,将不会有
EntityManager工厂
,因此也不会有EntityManager
。遇到同样的问题您可以在应用程序属性中更新MySql驱动程序吗
@PersistenceContext
private EntityManager entityManager;
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
(or)
spring.datasource.driverClassName=com.mysql.jdbc.Driver