我想将 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/