我有spring boot maven java多模块结构。我的结构是:
product (parent pom module)
..product-data (child pom module)
..product-business (has dependency for product-data)
..product-rest (has dependency for product-business)
..product-entities (child pom module)
product-data 会将实体对象返回给 product-business,product-business 会将实体对象返回给 product-rest,product-rest 返回 json 对象。
产品数据运行良好。但是一旦我运行产品业务,我就会收到错误 “无法确定数据库类型 NONE 的嵌入式数据库驱动程序类” . Spring 在我的 product-business/src/main/resources/application.properties 文件下查找 spring.datasource.... 属性。如果我在这里定义所有属性,那么错误就会消失,我会从产品数据中获取数据。
但!!我已经在 product-data/src/main/resources/application.properties 文件下定义了属性。 为什么我必须在我的产品业务模块中复制相同的属性? 整个目的是分离层。 product-data 负责获取数据,它应该在自己的结构下找到 spring.datasource... 属性。为什么它也强制我复制业务模块中的属性?我确定我错过了一些东西。有人有任何线索吗?
我在 SO 上经历了许多类似的问题,但其中大多数都缺少属性,所以这并不能解决我的问题。我认为我的 pom 文件不是可疑文件,因为一旦我将属性从产品数据复制粘贴到产品业务,错误就会消失。但是如果你还想看我的 pom:
Parent product POM
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.owner</groupId>
<artifactId>product</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<nopeasti.version>1.0.0-SNAPSHOT</nopeasti.version>
</properties>
<dependencies>
</dependencies>
<modules>
<module>product-data</module>
<module>product-business</module>
<module>product-rest</module>
<module>product-entities</module>
</modules>
</project>
product-data POM
<project>
<artifactId>product-data</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.owner</groupId>
<artifactId>product</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.0.M6</version>
</plugin>
</plugins>
</build>
</project>
product-business POM
<project>
<artifactId>product-business</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.owner</groupId>
<artifactId>product</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.owner</groupId>
<artifactId>product-data</artifactId>
<scope>compile</scope>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.0.M6</version>
</plugin>
</plugins>
</build>
</project>
最佳答案
根据this ,
It is not advisable to put application.properties in a library because there might be a clash at runtime in the application that uses it (only one application.properties is ever loaded from the classpath)
为了解决这个问题并仍然保持解耦架构,我创建了另一个文件 数据属性 在 的资源文件夹下产品数据 模块并指定 @PropertySource 配置文件中的注释。这是的配置文件产品数据 模块。 spring.datasource 属性是在这个文件中定义的。那么其他 2 个模块中就不需要 spring.datasource 属性了。
@ComponentScan
@EnableAutoConfiguration
@SpringBootConfiguration
@PropertySource(value = "data.properties")
public class NopeastiDataConfig {
// no main needed here as this is library consumed by business layer
}
进一步阅读:Externalized Configuration
关于spring - Maven Multi Module坚持在业务模块中复制datasource application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47317674/