我正在按照以下指南开发模块化Spring Boot应用程序:https://www.section.io/engineering-education/multi-module-architecture-with-spring-boot-and-maven/
However this article does not speak about application.properties files for different modules. I've included this in myroot application.propertiesfile:
spring.datasource.url = jdbc:mysql://localhost:3306/spring
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username = root
spring.datasource.password =
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
- 项目结构**
这里的根是一个spring boot应用程序,数据、服务和控制器是maven模块。
- 根pom.xml文件**
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dbiz</groupId>
<artifactId>esuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>esuite</name>
<packaging>pom</packaging>
<description>eSuite project for Spring Boot</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
<modules>
<module>data</module>
<module>service</module>
<module>controller</module>
</modules>
</project>
- 模块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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>controller</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dbiz</groupId>
<artifactId>esuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.dbiz</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.dbiz</groupId>
<artifactId>data</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
其他模块pom.xml文件与此类似。
- 堆栈跟踪**
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.1)
2023-01-10T11:14:27.773+05:00 INFO 13664 --- [ main] com.dbiz.esuite.EsuiteApplication : Starting EsuiteApplication using Java 19.0.1 with PID 13664 (D:\eclipse-workspace\esuite\target\classes started by Dell in D:\eclipse-workspace\esuite)
2023-01-10T11:14:27.782+05:00 INFO 13664 --- [ main] com.dbiz.esuite.EsuiteApplication : No active profile set, falling back to 1 default profile: "default"
2023-01-10T11:14:31.986+05:00 INFO 13664 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-10T11:14:32.210+05:00 INFO 13664 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33 ms. Found 0 JPA repository interfaces.
2023-01-10T11:14:38.609+05:00 INFO 13664 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-01-10T11:14:38.647+05:00 INFO 13664 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-01-10T11:14:38.647+05:00 INFO 13664 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.4]
2023-01-10T11:14:39.413+05:00 INFO 13664 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-01-10T11:14:39.416+05:00 INFO 13664 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 11128 ms
2023-01-10T11:14:39.553+05:00 WARN 13664 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2023-01-10T11:14:39.558+05:00 INFO 13664 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2023-01-10T11:14:45.176+05:00 INFO 13664 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-01-10T11:14:45.236+05:00 ERROR 13664 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
我已经尝试了所有的解决方案,我可以找到Stackoverflow和其他论坛,但没有一个是为我工作。任何提示或建议将不胜感激。
谢谢!
- 编辑**
Made changes to the application.properties file by using spring.datasource.hikari.url
New Application.properties file
spring.datasource.hikari.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.idle-timeout=180000
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.pool-name=Mysql-spring.datasource.hikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=5000
spring.datasource.hikari.jdbc-url=jdbc:mysql://localhost:3306/spring
spring.datasource.hikari.username=root
spring.datasource.hikari.password=
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
- 结果**仍然得到相同的错误。
1条答案
按热度按时间j0pj023g1#
JPA(Java persistence API)是一个对象关系Map(ORM)工具的Java规范,“spring-boot-starter-data-jpa”依赖项使ORM能够在springboot框架的上下文中实现。
spring Boot 应用程序的JPA自动配置特性尝试使用JPA数据源建立数据库连接。JPA数据源bean需要数据库驱动程序才能连接到数据库。
数据库驱动程序应作为pom.xml文件中的依赖项提供。对于Oracle、SQL Server、MySql、DB2、Postgres、MongoDB等外部数据库,需要数据库JDBC连接属性来建立连接。
您需要配置数据库驱动程序和JDBC连接属性来修复此异常-〉Failed to configure a DataSource:未指定“url”属性,无法配置嵌入式数据源。原因:无法确定合适的驱动程序类。
application.properties
application.yaml
通过编程:试试这个,我用这个构建了你的演示项目并成功运行了它。