[英]@profile annotation with maven profile
您好我正在嘗試根據活動配置文件切換數據庫,所以我使用 maven 配置文件來過濾數據庫依賴項
這是我的pom文件
<?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>2.4.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>couhbase</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>couhbase</spring.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-couchbase</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.couchbase.client</groupId>
<artifactId>java-client</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
這里我們有兩個配置文件,當配置文件是 couhbase 它應該連接 couchbase 數據庫,如果它是 dev 那么它應該連接到 MongoDB
這是我的屬性文件
spring.couchbase.bootstrap-hosts=127.0.0.1
spring.couchbase.bucket.name=config
spring.couchbase.bucket.password=
spring.profiles.include=@spring.profiles.active@
並在 maven 構建項目時設置配置文件名稱
因此,如果配置文件是 couhbase,則可以毫無問題地與 couchbase 連接,但是當我嘗試將配置文件更改為 dev 時,在構建項目時遇到問題
這是問題
它試圖編譯擴展 AbstractReactiveCouchbaseConfiguration 以設置連接參數的 ReactiveCouchbaseConfiguration class,這是 class
@Configuration
@Profile("couhbase")
@EnableReactiveCouchbaseRepositories("com.baeldung.couchbase.domain.repository")
public class ReactiveCouchbaseConfiguration extends AbstractReactiveCouchbaseConfiguration {
private CouchbaseProperties couchbaseProperties;
@Value("${spring.profiles.active}")
private String activeProfile;
@Autowired
private Environment env;
public ReactiveCouchbaseConfiguration(CouchbaseProperties couchbaseProperties) {
//System.out.println( env.getActiveProfiles());
this.couchbaseProperties = couchbaseProperties;
}
@Override
protected List<String> getBootstrapHosts() {
return couchbaseProperties.getBootstrapHosts();
}
@Override
protected String getBucketName() {
return couchbaseProperties.getBucketName();
}
@Override
protected String getBucketPassword() {
return couchbaseProperties.getBucketPassword();
}
@Override
public CouchbaseEnvironment couchbaseEnvironment() {
return DefaultCouchbaseEnvironment
.builder()
.bootstrapHttpDirectPort(couchbaseProperties.getPort())
.build();
}
}
所以我試圖使用配置文件注釋來避免這個 class 但它仍然編譯這個 class 當我們將配置文件從 couhbase 更改為 dev 時,有沒有辦法避免這個 class?
使用 @Profile 只是意味着如果聲明的配置文件與活動配置文件不匹配,您的 bean 將在運行時被忽略。 class 仍然需要編譯。 你為什么要如此努力地分離構建目標中的依賴關系? 恕我直言,投資回報率是負數。 您只在工件上節省了一些 MB。
最簡單的選擇是正常添加依賴項,而不是在 maven 配置文件中討價還價。 我會推薦這個。
另一種選擇是使用彈簧自動配置和@ConditionalOnClass。 你可以玩這個。
最復雜的選擇是將所有代碼(包括配置 bean)移動到單獨的模塊/jar 中,並將其添加到相應的 maven 配置文件中。 然后,再次使用 springs 自動配置選項或配置文件來切換配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.