Announcement Announcement Module
Collapse
No announcement yet.
Inject file content Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inject file content

    Hello,

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


    Oliver

  • #2
    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>

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment

        Working...
        X