【问题标题】:Reading Spring profiles from a @controller从@controller 读取 Spring 配置文件
【发布时间】:2018-11-28 11:51:26
【问题描述】:

我想将 maven 配置文件链接到 spring 配置文件。该项目构建在一个战争文件中。该应用程序是部署在 Weblogic Server 中的 Spring 应用程序,而不是 SpringBoot。

我有这个pom.xml 文件

<profile>
    <id>debug</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <spring.profiles.active>debug</spring.profiles.active>
    </properties>
</profile>
..

在应用程序中@Controller:

  @Autowired
  private Environment env;

  final List<String> activeProfiles = Arrays.asList(env.getActiveProfiles());

activeProfilesempty

我得到了同样的结果

<activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.to.activate>local</spring.profiles.to.activate>
            </properties>

【问题讨论】:

    标签: java spring maven spring-mvc


    【解决方案1】:

    您只是使用 Maven 配置文件定义 spring.profiles.active 属性值。该属性可用于 Maven 构建,例如用于资源过滤。

    该值仍然必须传递给运行 Spring Boot 的 JVM,例如JUnit 测试通常使用 Maven Surefire 插件运行,该插件分叉 JVM,如果您不使用 argLine 配置,则不会看到属性:

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <argLine>-Dspring.profiles.active=${spring.profiles.active}</argLine>
          </configuration>
        </plugin>
      </plugins>
    </build>
    

    对于任何用于启动 Spring Boot 应用程序的 Maven 插件,上述情况通常都是正确的。

    【讨论】:

    • 我已经尝试了建议的解决方案,结果相同:-(
    • @carlesxuriguera 您如何运行 Spring Boot 应用程序?请添加minimum, complete, verifiable example
    • 应用是Spring应用,不是SpringBoot,部署在Weblogic Server中。
    • @carlesxuriguera 那你为什么要标记你的帖子spring-boot?如果它被部署为 WAR,您需要将活动配置文件保存在某处,或者在 WebLogic 中的 WAR 中。在您的情况下,Maven 只是一个构建工具,它与运行您的 Spring 应用程序无关。
    猜你喜欢
    • 2013-06-08
    • 2015-08-06
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    相关资源
    最近更新 更多