让Spring Boot默认使用我的配置文件如何配置?

huangapple go评论55阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;3.1.0&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;Spring-vault-demo&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;Spring-vault-demo&lt;/name&gt;
&lt;description&gt;Spring-vault-demo&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;17&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencyManagement&gt;
&lt;dependencies&gt;
&lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!--  my vault--&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.vault&lt;/groupId&gt;
&lt;artifactId&gt;spring-vault-core&lt;/artifactId&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-starter-vault-config&lt;/artifactId&gt;
&lt;version&gt;4.0.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-starter-bootstrap&lt;/artifactId&gt;
&lt;version&gt;4.0.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-vault-config&lt;/artifactId&gt;
&lt;version&gt;4.0.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;systemPropertyVariables&gt;
&lt;argline&gt;Dspring.profiles.active=it&lt;/argline&gt;
&lt;/systemPropertyVariables&gt;
&lt;skipTests&gt;false&lt;/skipTests&gt;
&lt;/configuration&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;it&lt;/id&gt;
&lt;goals&gt;
&lt;goal&gt;integration-test&lt;/goal&gt;
&lt;goal&gt;verify&lt;/goal&gt;
&lt;/goals&gt;
&lt;phase&gt;integration-test&lt;/phase&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

this is the error that i got

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;vaultPropertySourceLocator&#39; defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapPropertySourceConfiguration.class]: Unsatisfied dependency expressed through method &#39;vaultPropertySourceLocator&#39; parameter 0: Error creating bean with name &#39;vaultTemplate&#39; defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Failed to instantiate [org.springframework.vault.core.VaultTemplate]: Factory method &#39;vaultTemplate&#39; threw exception with message: Error creating bean with name &#39;vaultSessionManager&#39; defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Unsatisfied dependency expressed through method &#39;vaultSessionManager&#39; parameter 0: Error creating bean with name &#39;clientAuthentication&#39; defined in class path resource [org/springframework/cloud/vault/config/VaultBootstrapConfiguration.class]: Failed to instantiate [org.springframework.vault.authentication.ClientAuthentication]: Factory method &#39;clientAuthentication&#39; 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

&lt;profiles&gt;
&lt;profile&gt;
&lt;id&gt;default&lt;/id&gt;
&lt;properties&gt;
&lt;spring.profiles.active&gt;local&lt;/spring.profiles.active&gt;
&lt;/properties&gt;
&lt;/profile&gt;
&lt;/profiles&gt;

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:

&lt;project&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;profiles&gt;local&lt;/profiles&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

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(&quot;local&quot;);
    }
}

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

huangapple
  • 本文由 发表于 2023年5月28日 02:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定