java - 从 @controller 读取 Spring 配置文件

标签 java spring maven spring-mvc

我想将 Maven 配置文件链接到 Spring 配置文件。该项目是在 war 文件中构建的。该应用程序是部署在 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());

但是activeProfiles

我使用得到了相同的结果

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

最佳答案

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

该值仍然必须传递给运行 Spring Boot 的 JVM,例如JUnit 测试通常使用 Maven Surefire 插件运行,该插件会 fork 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 插件,上述情况通常都是正确的。

关于java - 从 @controller 读取 Spring 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518840/

相关文章:

java - 用Java阻塞帧切换

java - 如何在 Spring XML 配置中设置 LocalDateTime

Spring MVC 与 Spring Integration HTTP : how to pass the queryString avoiding the "?" encoding with "%3F" ?

maven - maven 构建中的未知主机异常 nexus.codehaus.org

Java 日历在 MS Windows 中为 `America/Santiago` 区域返回错误的时间

java - 与 Java 集成的 Flash Builder

spring - 使用 Bcrypt 加密 InMemoryAuthentication 密码

java - 使用 Spring JPA 和 Hibernate 更新数据库分区上的记录时出错

maven - oozie3.3.2执行mkdistro.sh命令时生成失败

git - 如何在gitlab中管理多个maven项目