Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Issue with Spring profiles and my properties files

  1. #1
    Join Date
    Nov 2007
    Posts
    177

    Default 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. #2
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    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.

  3. #3
    Join Date
    Nov 2007
    Posts
    177

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Nov 2007
    Posts
    177

    Default

    Hi Marten,

    Quote 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?

    Quote 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...

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Nov 2007
    Posts
    177

    Default

    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...

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Nov 2007
    Posts
    177

    Default

    Quote 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?

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •