Announcement Announcement Module
Collapse
No announcement yet.
Simple way to pull/push environment-specific "properties" into my Controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple way to pull/push environment-specific "properties" into my Controller

    I am pretty sure there is a super-simple solution to this problem, but I have yet to find one.

    Here is the situation. I need to construct a call-back URL to pass out with my JSON data. This URL needs to have the correct hostname in it, so if I am running in my Eclipse environment the url woud be http://localhost:8080/some_stuff, but in my Prod environment it would be http://myserver.com/some_stuff.

    The most straight-forward solution would be to use the headers, grab the "Host", and plug it in. The problem is I can't over-ride the showJson() method in my controller due to the problem described in this post.

    In the meantime, I need a solution today, so I thought: What if I just have some sort of custom properties file and push/pull in the host value from that? I thought this would be simple, but so far it has not been.

    The problem is that the controllers are all defined through annotations, and I haven't found a way to "push" a property value in via annotations. What makes it more complicated is that the values of a spring bean are called "properties", so Google finds WAY too much non-related results.

    My current attempt has been to "pull" in the value using System.getProperty(), but the PropertyPlaceholderConfigurer I setup doesn't seem to add the properties in the custom properties file to System.properties.

    Here is my config for loading the property:
    Code:
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        	<property name="locations">
        		<value>classpath*:app.properties</value>
        	</property>
        </bean>
    I put app.properties in the "Servers/SpringSource tc Server Developer Edition v2.1-config", which also has the catalina.properties as well as a bunch of other Tomcat-related stuff. I figure its getting loaded since ignoreResourceNotFound defaults to "false" and I am not seeing any errors/warnings. In the Controller method I tried this:
    Code:
    String host = System.getProperty("url.host");
    But host is null after this line.

    I am sure I am missing something here. Not sure what, thought.

  • #2
    You can obtain this kind of information via the servlet API. For example, the HttpServletRequest (which Spring MVC will pass to any handler method that declares it as a parameter) can tell you various parts of the request URL, including the host, etc.
    Last edited by Andrew Swan; Jul 14th, 2011, 08:48 PM. Reason: Clarified how to get the HTTP request

    Comment


    • #3
      Thanks Andrew, but if you refer the the original posting, you'll see I am unable to use the HttpServletRequest.

      The problem is that I need to push in the showJson method to modify it, but as soon as I modify the signature of showJson, adding HttpServletRequest, the Roo Shell things I removed the push-in and re-inserts the original showJson back into the ITD. This causes an ambiguous method error, since now you have two methods in the controller mapped to the same RequestMapping.

      So, how can I gain access to HttpServletRequest if I can't add it to my method signature? I have a JIRA story added to make changes to how Roo Shell matches methods in the ITDs with methods in the Controller, to rely on the method name rather than the full signature, but I need to have a solution today. And since the hostname part of the URL will differ from test to Prod, and the hostname isn't necessarily the actual hostname on the server anyway, I thought maybe a property was the best answer. I know I can modify Tomcat's startup scripts, add a System property to the command line there, but that seems sloppy. I am sure Spring has come up with a better way, I am just having a hard time finding it do to the commonality of the terms "property" and "controller".

      Comment


      • #4
        Here is how I do it :

        in applicationContext.xml, I add this line :
        Code:
        <util:properties id="fooProperties" location="file:c:/var/foo.properties"/>
        which loads the props from the foo.properties file

        and then, I use the @Value annotion to inject the values :
        Code:
        @Value("#{fooProperties['mail.from']}")
        private String mailFrom;
        So I can have a different foo.properties in different environments.

        Comment


        • #5
          Wester, this is exactly what I was looking for!

          Comment

          Working...
          X