Results 1 to 4 of 4

Thread: Inject file content

  1. #1
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default Inject file content

    Hello,

    is it possible to inject the content of a file to a setter that expects a string?


    Oliver

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    yes but you have to do somehow the translation. A property editor would work but since there is already one for String the easiest way is to use a FactoryBean:
    <bean ..>
    <property name="fileContent">
    <bean class="ReadFileFactoryBean">
    <property name="file" value="classpath:/some/classpath/to/file"/>
    </bean>
    </property>
    </bean>
    Even better, you can remove the factory bean with an exiting class and use the factory method instead:
    <bean ..>
    <property name="fileContent">
    <bean class="MyFileReader" factory-method="getFileContent">
    <constructor value="/path/to/file"/>
    </bean>
    </property>
    </bean>
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jul 2005
    Location
    Munich, Germany
    Posts
    153

    Default

    Quote Originally Posted by Costin Leau
    yes but you have to do somehow the translation. A property editor would work but since there is already one for String the easiest way is to use a FactoryBean:


    Even better, you can remove the factory bean with an exiting class and use the factory method instead:
    Thanks. I thought there must be something that is usable out of the box without writing a custom Factory.

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    If you already have a (static) method reading the file contents into a string (quite easy to implement), you can also use MethodInvokingFactoryBean to obtain the string and inject it into a property.

    Regards,
    Andreas

Posting Permissions

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