Results 1 to 4 of 4

Thread: Initialising an object with a string parameter

  1. #1
    Join Date
    May 2006
    Posts
    5

    Default 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 at 09:12 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    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"/>

  3. #3
    Join Date
    May 2006
    Posts
    5

    Default

    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

  4. #4
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,844

    Default

    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"

Posting Permissions

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