英文:
How do I let spring boot to use my profile in default?
问题
我正在尝试运行一个Spring Boot应用程序,我理解我可以使用VM选项来设置我的配置文件,但是否有一种方法可以将我的配置文件设置为默认,以便我不需要处理VM选项?另外,我可以让 mvn install
命令在默认情况下运行我的配置文件吗?
以下是您的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文件中添加以下配置和在application.properties
文件中设置 spring.profiles.active=local
,但两者都没有起作用,仍然显示 Vault 令牌未设置错误。我调试了一下发现 BootstrapApplicationListener
包含一个空的活动配置文件列表。
我采纳了 Scott Frederick 的建议,并成功使用 mvn springboot:run
命令运行了本地配置文件。但是当我运行 mvn install
时,仍然出现相同的错误。
英文:
I am trying run a spring boot application and I understand that i can use vm option to set my profile, but is there a way that i can set my profile in default so that i don't need to deal with the vm option? also can i let mvn install
run my profile in default?
# 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
this is my pom file
<?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>
this is the error that i got
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.
I try this in my pom file
<profiles>
<profile>
<id>default</id>
<properties>
<spring.profiles.active>local</spring.profiles.active>
</properties>
</profile>
</profiles>
and
spring.profiles.active=local
in my application.properties file
both of them are not working. still giving me a vault token not been set error.
I debug into it and see that the BootstrapApplicationListener contains an empty list for active profile.
I take Scott Frederick's advice, and i was able to run my local profile with mvn springboot:run
. but when i run mvn install
, it fail with the same error.
答案1
得分: 0
Spring Boot Maven 插件文档描述如何执行此操作。
对于您的示例,您可以在 pom.xml
中进行配置:
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>local</profiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
还请注意,任何在配置文件之外的配置(即,位于第一个 ---
文档分隔符之上)都属于一个名为 default
的配置文件,当未指定配置文件时将使用该配置文件。
英文:
The Spring Boot Maven plugin documentation describes how to do this.
For your example, you could configure this in pom.xml
:
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<profiles>local</profiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Also note that any configuration that is outside of a profile document (that is, above the first ---
document separator) is part of a profile named default
and is used when no profile is specified.
答案2
得分: 0
如果我理解您的问题正确,您希望在启动Spring Boot应用程序时,即使没有使用Maven(例如java -jar my-app.jar
),也始终使用'local'配置文件吗?
如果是这样,您可以考虑实现EnvironmentPostProcessor
:
public class AddLocalProfileEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// 可能会在这里添加一些编程条件来决定是否包含此配置文件
application.setAdditionalProfile("local");
}
}
然后在spring.factories
中注册它并检查:
在src/main/resources
内创建一个名为META-INF
的文件夹,然后在其中创建一个名为spring.factories
的文件,并将以下内容放入该文件中:
org.springframework.boot.env.EnvironmentPostProcessor=\
com.yourapp.yourpackage.AddLocalProfileEnvironmentPostProcessor
如果您希望按特定顺序调用后处理器,还可以实现Ordered接口或使用@Order注解。
英文:
If I get your question right, you want the 'local' profile to be always used when you start the spring boot application even if you do it without maven, like java -jar my-app.jar
?
If so you could consider implementing EnvironmentPostProcessor
:
public class AddLocalProfileEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
// maybe some programmatic conditions here to decide whether to include this profile or not
application.setAdditionalProfile("local");
}
}
Now register it in spring.factories
and check:
Inside src/main/resources
create a folder META-INF
and inside file spring.factories
Place the following into this file:
org.springframework.boot.env.EnvironmentPostProcessor=
com.yourapp.yourpackage.AddLocalProfileEnvironmentPostProcessor
You can also implement the Ordered interface or use an @Order annotation if you want the post processors to be invoked in specific order.
答案3
得分: 0
感谢所有尝试帮助的人。所以我找到了解决方案。就是在我的引导中使用 spring.profiles.active=local
。
spring:
profiles:
active: local
cloud:
vault:
enabled: false
---
spring:
config:
activate:
on-profile: local
---
spring:
config:
activate:
on-profile: it
cloud:
vault:
enabled: true
再次感谢Mark Bramnik和Scott Frederick,非常感谢。
英文:
thanks for everyone who trying to help. so i find the solution. it is to use spring.profiles.active=local
in my bootstrap.
spring:
profiles:
active: local
cloud:
vault:
enabled: false
---
spring:
config:
activate:
on-profile: local
---
spring:
config:
activate:
on-profile: it
cloud:
vault:
enabled: true
again, thanks Mark Bramnik and Scott Frederick, appreciate it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论