我是一名对 Spring 平台比较新的开发者。我正在实现基于 XML 的 Spring 配置文件。
我在 web.xml 中声明了一个配置文件,如下所示:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
我希望在我的Java类中获取活动配置文件的值。
这是我的第一次尝试,但由于一些问题而无法运行。
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
String profiles[] = ctx.getEnvironment().getActiveProfiles();
System.out.println(profiles[0]);
它显示了一个 null 值。有没有什么办法在 Java 类中激活配置文件?