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

qvtsj1bj  于 2023-05-28  发布在  Spring
关注(0)|答案(2)|浏览(279)

我正在尝试运行一个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时,它失败了,出现了同样的错误。

mwg9r5ms

mwg9r5ms1#

Sping 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的配置文件的一部分,并且在未指定配置文件时使用。

kq0g1dla

kq0g1dla2#

如果我没理解错的话,你希望在启动spring Boot 应用程序时总是使用“local”配置文件,即使你没有使用maven,比如java -jar my-app.jar
如果是这样,你可以考虑实现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");
    }
}

现在在spring.factories中注册它并检查:
src/main/resources中创建一个文件夹META-INF,并在文件spring.factories中将以下内容放入该文件:

org.springframework.boot.env.EnvironmentPostProcessor=
  com.yourapp.yourpackage.AddLocalProfileEnvironmentPostProcessor

如果希望以特定顺序调用后处理器,还可以实现Ordered接口或使用@Order注解。

相关问题