Announcement Announcement Module
Collapse
No announcement yet.
Initialising an object with a string parameter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Initialising an object with a string parameter

    Hi,

    I have a Constants interface with various Strings like home directory ,lib directory etc information placed in variables like

    public interface ConstantsIF {
    public static final String HOME="C:/";
    }


    one other class file works like this

    DependentClass {
    public DependentClass(String x) {
    // do some thing with x here
    }
    }

    if my x has to be a value like

    ConstantsIF.HOME+"/config/config.xml";

    then how do I pass that in the
    <constructor-arg type="java.lang.String">
    <value>??????????????</value>
    </constructor-arg>



    Can somebody help me with this please

    Regards,
    Chetan
    Last edited by chetan; May 31st, 2006, 10:12 AM.

  • #2
    Have a look at Spring's "FieldRetrievingFactoryBean" as described here:
    http://static.springframework.org/sp...s.html#d0e2331

    Or, if you are using Spring 2.0, you can use the new "util" namespace, like so:
    <util:constant id="homeConstant" staticField="ConstantsIF.HOME"/>

    Comment


    • #3
      Dear Mark,

      I had a look at the first url mentioned,
      I am able to get the value in the interface.but how do I concatenate this value with the one that I am supposed to pass.

      if myield is the value obtained from the ConstantsIF using the
      FieldRetrieving factory bean

      then how do i achieve this concatenation effect

      myField+"/config/config.xml"

      such the spring application context understands the full path is of the form
      C:/config/config.xml

      Regards,
      Chetan

      Comment


      • #4
        Actually, for providing the "home" value as a prefix, I would consider using the PropertyPlaceholderConfigurer. That way the value for your home directory is stored in a properties file rather than hardcoded in the "ConstantsIF". You might consider trying something like this:
        Code:
          <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:/example/constants.properties"/>
          </bean>
          
          <bean id="testBean" class="example.TestBean">
            <property name="filename" value="${home.dir}/rest/of/path"/>
          </bean>
        Then in the constants.properties file, simply provide:
        Code:
        home.dir=/home
        The TestBean instance's 'filename' property would then have a value of: "/home/rest/of/path"

        Comment

        Working...
        X