Announcement Announcement Module
Collapse
No announcement yet.
Issue with Spring profiles and my properties files Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Issue with Spring profiles and my properties files

    Hello,

    I am trying to activate one set of properties files for one Spring profile and another set for another Spring profile as follows:

    Code:
    <beans profile="cloud">
        <context:property-placeholder location="classpath*:META-INF/spring/cloud/*.properties" />
    </beans>
    
    <beans profile="default">
        <context:property-placeholder location="classpath*:META-INF/spring/default/*.properties" />
    </beans>
    I have the corresponding and appropriate directory structure in my src/main/resources folder.

    I have a simple @Value("${application.url}") in one of my services and I systematically get the following error:

    Error creating bean with name 'mailerServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.Be anCreationException: Could not autowire field: private java.lang.String com.kadjoukor.service.MailerServiceImpl.websiteCon text; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'application.url' in string value "${application.url}"

    Note that I have tried adding a spring.profiles.active init-param to my web.xml. It doesn't make any difference...

    Could it matter that the above snippets of configuration are located at the bottom of the configuration file?

    Otherwise, I am not sure what I am getting wrong. Can anyone please provide advice?

    Regards,

    J.

  • #2
    As you mentioned, you've added a spring.profiles.active init-param to your web.xml, and it doesn't have any effect. This means that your application context is being loaded by the listener configured in web.xml rather than your dispatcher servlet. Can you confirm by adding this property spring.profiles.active to the startup script on the command line
    -Dspring.profiles.active=cloud
    . This should work as expected.

    Comment


    • #3
      Hi,
      I tried adding the following to my local STS/tomcat:
      Code:
      -Dspring.profiles.active=default
      in order to enable the default profile on my local machine. I still get the same error as before...
      Any other idea?
      J.

      Comment


      • #4
        default should be active by default .

        What is loading this context the ContextLoaderListener or the DispatcherServlet, how to enable the profile depends on which one is used? Also note that beans loaded in the ContextLoaderListener don't process beans in the DispatcherServlet and vice-versa.

        Comment


        • #5
          Hi Marten,

          Originally posted by Marten Deinum View Post
          default should be active by default .
          That's what I thought too. Any idea what could cause it not to be picked up by Spring?

          Originally posted by Marten Deinum View Post
          What is loading this context the ContextLoaderListener or the DispatcherServlet, how to enable the profile depends on which one is used? Also note that beans loaded in the ContextLoaderListener don't process beans in the DispatcherServlet and vice-versa.
          I actually have the following code snippet:
          Code:
          <beans profile="cloud">
              <context:property-placeholder location="classpath*:META-INF/spring/cloud/*.properties" />
          </beans>
          <beans profile="default">
              <context:property-placeholder location="classpath*:META-INF/spring/default/*.properties" />
          </beans>
          in both webmvc-config.xml (dispatcherServlet) and applicationContext.xml (contextLoaderListener) and I have tried to add the spring.profiles.active property both in the dispatcherServlet config (web.xml) and on the command line to no avail...

          Comment


          • #6
            Where in your web.xml... The init-param has no effect the ContextLoaderListener so only adding a context-param would be useful.

            To see what is happening I suggest you enable debug logging and see what happens with your properties.

            Comment


            • #7
              Yes I did add it as a context-param...

              Debugging output is as follows:
              Code:
              2012-12-18 14:20:18,311 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletConfigInitParams]
              2012-12-18 14:20:18,311 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'spring.profiles.active' in [servletContextInitParams]
              2012-12-18 14:20:18,311 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.profiles.active' in [servletContextInitParams] with type [String]
               and value 'default'
              and:
              Code:
              2012-12-18 14:20:22,376 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [environmentProperties]
              2012-12-18 14:20:22,376 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [servletConfigInitParams]
              2012-12-18 14:20:22,376 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [servletContextInitParams]
              2012-12-18 14:20:22,376 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [jndiProperties]
              2012-12-18 14:20:22,377 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [systemProperties]
              2012-12-18 14:20:22,377 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [systemEnvironment]
              2012-12-18 14:20:22,377 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'application.url' in any property source. Returning [null]
              2012-12-18 14:20:22,377 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Searching for key 'application.url' in [localProperties]
              2012-12-18 14:20:22,377 [localhost-startStop-1] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'application.url' in any property source. Returning [null]
              2012-12-18 14:20:22,395 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
              application.url being the problematic key...

              Comment


              • #8
                It appears that the property-placeholder is doing its work but apparently the property files cannot be loaded. Make sure that they are available in the location as you specified.

                The logging should tell you which files it tries to load those are INFO messages, try to see what logging is emitted from the PropertySourcesPlaceholderConfigurer.

                Comment


                • #9
                  Originally posted by Marten Deinum View Post
                  It appears that the property-placeholder is doing its work but apparently the property files cannot be loaded. Make sure that they are available in the location as you specified.

                  The logging should tell you which files it tries to load those are INFO messages, try to see what logging is emitted from the PropertySourcesPlaceholderConfigurer.
                  Code:
                  2012-12-18 15:10:43,207 [localhost-startStop-1] INFO  org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from file [/home/julien/system/workspace-sts-2.
                  9.2.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/kadjoukor/WEB-INF/classes/META-INF/spring/default/app-config.properties]
                  Rather a Spring profile issue... Don't you think?

                  Comment


                  • #10
                    No I don't think so... What does the logging further say?

                    Also as mentioned before beans in the ContextLoaderListener don't influence the ones in the DIspatcherServlet and vice-versa. So make sure that you are replacing properties in the same context as where the properties files are loaded.

                    Judging frmo the logging everything is loaded but I suspect you are replacing the properties in the wrong context (something like that).

                    Comment


                    • #11
                      Ummmh. What do you mean by "replacing the properties"?

                      Comment


                      • #12
                        about the logging:

                        Code:
                        2012-12-18 15:33:04,131 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
                        org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'passwordManagerServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework
                        .beans.factory.BeanCreationException: Could not autowire field: private com.kadjoukor.service.MailerService com.kadjoukor.service.PasswordManagerServiceImpl.mailerService; nested exception is org.spri
                        ngframework.beans.factory.BeanCreationException: Error creating bean with name 'mailerServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.Be
                        anCreationException: Could not autowire field: private java.lang.String com.kadjoukor.service.MailerServiceImpl.websiteContext; nested exception is java.lang.IllegalArgumentException: Could not resolv
                        e placeholder 'application.url' in string value "${application.url}"
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
                        	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
                        	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
                        	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
                        	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
                        	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
                        	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
                        	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
                        	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
                        	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
                        	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
                        	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
                        	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
                        	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
                        	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
                        	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
                        	at java.lang.Thread.run(Thread.java:662)
                        Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.kadjoukor.service.MailerService com.kadjoukor.service.PasswordManagerServiceImpl.mailerService
                        ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailerServiceImpl': Injection of autowired dependencies failed; nested exception is org.sp
                        ringframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.kadjoukor.service.MailerServiceImpl.websiteContext; nested exception is java.lang.IllegalArgum
                        entException: Could not resolve placeholder 'application.url' in string value "${application.url}"
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
                        	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
                        	... 23 more
                        Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailerServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframewo
                        rk.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.kadjoukor.service.MailerServiceImpl.websiteContext; nested exception is java.lang.IllegalArgumentExceptio
                        n: Could not resolve placeholder 'application.url' in string value "${application.url}"
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
                        	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
                        	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:873)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
                        	... 25 more
                        Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.kadjoukor.service.MailerServiceImpl.websiteContext; nested exception is java.
                        lang.IllegalArgumentException: Could not resolve placeholder 'application.url' in string value "${application.url}"
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
                        	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
                        	... 36 more
                        Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'application.url' in string value "${application.url}"
                        	at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
                        	at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
                        	at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:174)
                        	at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:143)
                        	at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:167)
                        	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:749)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
                        	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
                        	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
                        	... 38 more
                        18 déc. 2012 15:33:04 org.apache.catalina.core.StandardContext listenerStart

                        Comment


                        • #13
                          Just as I said .

                          If you have a bean in y our dispatcher servlet and the property-placeholder in the contextloaderlistener nothing will happen apart from an exception. Because beans in the parent (ContextLoaderListener) don't influence the beans in the child (DispatcherServlet) and vice versa.

                          Comment


                          • #14
                            I see what you mean Marten. Let me sum up about my config:

                            In webmvc-config.xml:

                            Code:
                            	<beans profile="cloud">
                            		<context:property-placeholder location="classpath*:META-INF/spring/cloud/*.properties" />
                            	</beans>
                            	
                            	<beans profile="default">
                            		<context:property-placeholder location="classpath*:META-INF/spring/default/*.properties" />
                            	</beans>
                            In applicationContext.xml:

                            Code:
                            	<beans profile="cloud">
                            		<context:property-placeholder location="classpath*:META-INF/spring/cloud/*.properties" />
                            	</beans>
                            	
                            	<beans profile="default">
                            		<context:property-placeholder location="classpath*:META-INF/spring/default/*.properties" />
                            	</beans>
                            In web.xml

                            Code:
                            	<context-param>
                            		<param-name>contextConfigLocation</param-name>
                            		<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
                            	</context-param>
                            	<context-param>
                             		<param-name>spring.profiles.active</param-name>
                            		<param-value>default</param-value>
                            	</context-param>
                            
                            <servlet>
                            		<servlet-name>kadjoukor</servlet-name>
                            		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                            		<init-param>
                            			<param-name>contextConfigLocation</param-name>
                            			<param-value>WEB-INF/spring/webmvc-config.xml</param-value>
                            		</init-param>
                            		<init-param>
                            			<param-name>spring.profiles.active</param-name>
                            			<param-value>default</param-value>
                            		</init-param>
                            		<load-on-startup>1</load-on-startup>
                            	</servlet>
                            Please note the duplication of the contextroperty-placeholder. This works fine if I don't use profiles...

                            Do you see my point?

                            Comment


                            • #15
                              The logging (loading etc.) indicates that everything is setup as expected and available (properties are loaded etc.). What would be interesting is to see if there is a problem with namespaces icm. profiles.

                              What happens if you use a PropertySourcesPlaceholderConfigurer instead of the namespace configuration (this is basically what is going to be setup for you).

                              Comment

                              Working...
                              X