The application I am working on has an XML-based Spring configuration(framework version: 4.1.7.RELEASE).
Because I want to instantiate certain beans provided a profile is specified, I have added the following to my 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
is a property set in the Maven pom.xml, based on the Maven profile that is selected:
<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>
I have filtering enabled in my maven-war
plugin, which works as expected, because the placeholder is replaced with the appropriate value based on the Maven profile chosen. Therefore, the part involving Maven is cleared and works fine.
However, when I try to inject an Environment instance, the active profile list is empty:
@Autowired private Environment env;
...
log.info(env.getActiveProfiles().toString());
The servletConfigInitParams
appears in the list of property sources for the Environment
, but it seems that my init-param
is simply ignored.
I have managed to set the Spring profile by providing actual system properties in the two maven profiles and running with those set, but that is not a viable solution for the production environment, nor would it be to provide those through command line.
Can anyone tell what I'm doing wrong?