Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring XML配置无法检索活动配置文件_Spring_Spring Mvc_Web.xml_Spring Profiles - Fatal编程技术网

Spring XML配置无法检索活动配置文件

Spring XML配置无法检索活动配置文件,spring,spring-mvc,web.xml,spring-profiles,Spring,Spring Mvc,Web.xml,Spring Profiles,我正在使用的应用程序具有基于XML的Spring配置(框架版本:4.1.7.RELEASE) 因为我想在指定概要文件的情况下实例化某些bean,所以我将以下内容添加到我的web.xml: <servlet> <servlet-name>my-app</servlet-name> <servlet-class> org.springframework.web.servlet.Dispatche

我正在使用的应用程序具有基于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>
我在我的
maven-war
插件中启用了过滤功能,该插件可以正常工作,因为占位符将根据所选的maven配置文件替换为适当的值。因此,涉及Maven的部分已清除,工作正常

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

@Autowired private Environment env;
...
log.info(env.getActiveProfiles().toString());
servletConfigInitParams
出现在
环境的属性源列表中,但似乎我的
init param
被忽略了

我通过在两个maven概要文件中提供实际的系统属性并使用这些属性集运行,成功地设置了Spring概要文件,但这对于生产环境来说不是一个可行的解决方案,也不能通过命令行提供这些属性


有人知道我做错了什么吗?

列表是空的还是
env
?您是将其注入到sprig管理的bean中,还是将其注入到您自己用
new
创建的东西中?另外,在每个环境中创建不同的war文件是一件坏事,您基本上是将未经测试的代码投入生产,因为它是一个不同的工件。两者都不是空的。列表是空的,NPE评论是我的错误。当然,我正在注入
env
的bean是Spring管理的。由于设置不同(不同的服务器技术、不同的数据库要求),生产工件需要与开发工件稍有不同。但是,这是在发布周期中测试的工件:功能测试、系统测试、集成测试、用户验收测试等等,所以不用担心,构建并不那么容易跳转到生产环境。这个bean是在
DispatcherServlet
中还是
ContextLoaderListener
的一部分,或者偶然地两者都是…我认为它只是
ContextLoaderListener
的一部分,在对应于
contextConfigLocation
上下文参数的xml中指定,该参数将无法访问
DispatcherServlet
上设置的环境。因此它什么都不知道。如果它是全局参数而不是
init参数
则将其定义为
context参数
,如
contextConfigLocation
@Autowired private Environment env;
...
log.info(env.getActiveProfiles().toString());