0

我正在处理的应用程序具有基于 XML 的 Spring 配置(框架版本:4.1.7.RELEASE)。

因为我想在指定配置文件的情况下实例化某些 bean,所以我将以下内容添加到我的web.xml

 <servlet>
        <servlet-name>my-app</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
<!-- This part is new -->
        <init-param>
            <param-name>spring.profiles.active</param-name>
            <param-value>${spring.profile}</param-value>
        </init-param>
<!-- End of newly-added part -->
        <load-on-startup>1</load-on-startup>
    </servlet>

spring.profile是在 Maven pom.xml 中设置的属性,基于所选的 Maven 配置文件:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profile>development</spring.profile>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profile>production</spring.profile>
        </properties>
    </profile>
</profiles>

我在我的maven-war插件中启用了过滤,它按预期工作,因为占位符被替换为基于所选 Maven 配置文件的适当值。因此,涉及 Maven 的部分被清除并且工作正常。

但是,当我尝试注入 Environment 实例时,活动配置文件列表为空:

@Autowired private Environment env;
...
log.info(env.getActiveProfiles().toString());

出现servletConfigInitParams在 的属性源列表中Environment,但似乎 myinit-param被忽略了。

我已经设法通过在两个 Maven 配置文件中提供实际系统属性并使用这些设置运行来设置 Spring 配置文件,但这对于生产环境来说不是一个可行的解决方案,也不会通过命令行提供这些配置文件。

谁能告诉我做错了什么?

4

0 回答 0