Rob*_*_UK 5 java shared-libraries autowired maven spring-boot
我希望创建一个Spring库项目以在内部团队之间共享。
在一个非常基本的概念级别上,该库会将消息事件发送到队列,我的计划是在多个Spring Boot微服务以相同方式发送消息的团队内部对此进行标准化。
我在图书馆项目中的pom看起来像这样
<artifactId>my-library</artifactId>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
etc...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.16.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我在图书馆项目中有一项服务看起来像这样
public class EventService {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
public void sendAuditEvent(AuditMessage auditMessage){
Set<ConstraintViolation<AuditMessage>> violations = validator.validate(auditMessage);
if(!isEmpty(violations)){
log.error("Unable to send audit message");
violations.stream().forEach( v-> log.error(v.getMessage()));
}
log.info("Found {} violations", violations.size());
// etc blah blah
return;
}
}
Run Code Online (Sandbox Code Playgroud)
将库导入另一个项目时,我的想法是可以自动装配EventService。通过将其添加到pom中,然后
@ComponentScan({"my.library.package.eventlibrary.service"})
如何防止Spring版本锁定?如果该库2.1.5.RELEASE
今天使用的是春季,并且导入该库的项目使用的是其他版本,那么我是否不会遇到潜在的Maven冲突?
也可以说导入库的项目使用较低版本的hibernate api,而库具有6.0.16.Final
。如何防止项目使用库类路径中的较新项目?
为了进一步阐明我的问题,有没有一种方法可以将库中的依赖项与使用它的项目分开。
您可以尝试排除您的库可能给使用它的项目带来的所有传递依赖项。
为此,您应该在dependencyManagement部分中替换为 ,并使用spring-boot-starter-parent
库需要使用的所有依赖项的范围,以及将由项目准确使用的、将与库一起使用的所有依赖项的范围。spring-boot-dependencies
provided
例如,您的库的 pom.xml 可能如下所示:
<!-- ... -->
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>0.1.0</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<spring-boot.version>2.1.5.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
然后,您将能够在不同的项目中使用您的库,例如使用旧的 Spring Boot:
<!-- ... -->
<groupId>com.example</groupId>
<artifactId>old-project</artifactId>
<version>0.13.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath/>
</parent>
<!-- ... -->
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>library</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
所以这个项目将使用hibernate-validator:5.3.6.Final
它的spring-boot-starter-web
.
重要提示 - 您的库的代码应该与此版本的 Spring Boot “兼容”。换句话说,您应该使用您感兴趣的不同版本的 Spring Boot 来测试您的库。
以我的项目为例。
归档时间: |
|
查看次数: |
279 次 |
最近记录: |