我正在尝试运行一个spring Boot 应用程序,我知道我可以使用vm选项来设置我的配置文件,但是有没有一种方法可以让我在默认情况下设置我的配置文件,这样我就不需要处理vm选项?我也可以让mvn install
在默认情况下运行我的配置文件?
# my bootstrap
spring:
config:
activate:
on-profile: local
cloud:
vault:
enabled: false
---
spring:
config:
activate:
on-profile: local
---
spring:
config:
activate:
on-profile: it
cloud:
vault:
enabled: true
这是我的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>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Spring-vault-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-vault-demo</name>
<description>Spring-vault-demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencyManagement>
<dependencies>
</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>
<!-- my vault-->
<dependency>
<groupId>org.springframework.vault</groupId>
<artifactId>spring-vault-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-vault-config</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-vault-config</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<argline>Dspring.profiles.active=it</argline>
</systemPropertyVariables>
<skipTests>false</skipTests>
</configuration>
<executions>
<execution>
<id>it</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<phase>integration-test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是我得到的错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vaultPropertySourceLocator' defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapPropertySourceConfiguration.class]: Unsatisfied dependency expressed through method 'vaultPropertySourceLocator' parameter 0: Error creating bean with name 'vaultTemplate' defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Failed to instantiate [org.springframework.vault.core.VaultTemplate]: Factory method 'vaultTemplate' threw exception with message: Error creating bean with name 'vaultSessionManager' defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Unsatisfied dependency expressed through method 'vaultSessionManager' parameter 0: Error creating bean with name 'clientAuthentication' defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Failed to instantiate [org.springframework.vault.authentication.ClientAuthentication]: Factory method 'clientAuthentication' threw exception with message: Cannot create authentication mechanism for TOKEN. This method requires either a Token (spring.cloud.vault.token) or a token file at ~/.vault-token.
我在我的pom文件中尝试这个
<profiles>
<profile>
<id>default</id>
<properties>
<spring.profiles.active>local</spring.profiles.active>
</properties>
</profile>
</profiles>
和spring.profiles.active=local
在我的application.properties文件
两个都不好用。仍然给我一个保管库令牌未被设置错误。我调试它,看到BootstrapApplicationListener包含一个空的活动配置文件列表。
我接受了Scott Frederick的建议,我能够用mvn springboot:run
运行我的本地配置文件。但是当我运行mvn install
时,它失败了,出现了同样的错误。
2条答案
按热度按时间mwg9r5ms1#
Sping Boot Maven插件文档描述了如何做到这一点。
对于您的示例,您可以在
pom.xml
中进行配置:还要注意,任何配置文件之外的配置(即第一个
---
文档分隔符之上的配置)都是名为default
的配置文件的一部分,并且在未指定配置文件时使用。kq0g1dla2#
如果我没理解错的话,你希望在启动spring Boot 应用程序时总是使用“local”配置文件,即使你没有使用maven,比如
java -jar my-app.jar
?如果是这样,你可以考虑实现
EnvironmentPostProcessor
:现在在
spring.factories
中注册它并检查:在
src/main/resources
中创建一个文件夹META-INF
,并在文件spring.factories
中将以下内容放入该文件:如果希望以特定顺序调用后处理器,还可以实现Ordered接口或使用@Order注解。