Announcement Announcement Module
Collapse
No announcement yet.
Referencing a file via classpath in spring context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Referencing a file via classpath in spring context

    Maybe I'm being dense, but I'm trying to reference a file using the classpath: notation in the context file, and it's not working for me.

    If I use the full file path, it works. But since this will be part of a webapp, I wanted to use the classpath notation. So am I going about this the wrong way, is there another way?

    Code:
    	<!-- the database configuration setup -->
    	<bean id="dbSetup" class="com.blackcore.photokitchen.setup.database.DatabaseSetupMySql" init-method="init">
    		<property name="dataSource"><ref bean="dataSource"/></property>
    		<property name="dropTablesSqlFile"><value>classpath&#58;com/blackcore/photokitchen/database/mysql/drop-tables.sql</value></property>
    		<property name="createTablesSqlFile"><value>classpath&#58;com/blackcore/photokitchen/database/mysql/create-tables.sql</value></property>
    	</bean>
    [/code]

  • #2
    This should work ok.

    Is your bean code like:
    public void setDropTablesSqlFile(InputStream is) {
    ...
    }

    Comment


    • #3
      No, the argument is of java.io.File.. the spring javadocs seem to indicate using InputStreamResource as a last resort. Although to think about it, using inputstream would be a cleaner way to pass in the drop table sql rather than a file. I'll try it.

      But is there any reason why passing it in as a File argument doesnt work?

      public void setDropTablesSqlFile(File f) {
      ...
      }

      Comment


      • #4
        Re: Referencing a file via classpath in spring context

        Originally posted by timster
        Maybe I'm being dense, but I'm trying to reference a file using the classpath: notation in the context file, and it's not working for me.

        If I use the full file path, it works. But since this will be part of a webapp, I wanted to use the classpath notation. So am I going about this the wrong way, is there another way?

        Code:
        	<!-- the database configuration setup -->
        	<bean id="dbSetup" class="com.blackcore.photokitchen.setup.database.DatabaseSetupMySql" init-method="init">
        		<property name="dataSource"><ref bean="dataSource"/></property>
        		<property name="dropTablesSqlFile"><value>classpath&#58;com/blackcore/photokitchen/database/mysql/drop-tables.sql</value></property>
        		<property name="createTablesSqlFile"><value>classpath&#58;com/blackcore/photokitchen/database/mysql/create-tables.sql</value></property>
        	</bean>
        [/code]
        Only Spring's Resource type actually knows how to interpret the 'classpath:' notation. When you feed a string (in this case with 'classpath:' as a prefix) to a property of type File, all that is going to happen is that default FileEditor PropertyEditor that Spring registers is going to create a new File object for you (using the constructor which takes a String), and feed it in as the value for that property. The 'classpath:' which just be part of the filename...

        If you do not mind having a Spring dependency in your code, then your dropTablesSqlFile property could be changed to be of type Resource, and using the 'classpath:' prefix would work.

        Colin

        Comment


        • #5
          Thanks for the explaination, Colin. Your suggestion is certainly doable.

          I'm trying to avoid the need to add spring dependencies if possible. (Although my app probably wouldnt be even half as good as it is now without Spring).

          Is there another way around it. Like mabye defining the file as a resource somehow in the context, then the resource could pass the file in to the argument?

          The reason I ask is that I would imagine this to be a common scenario, that developers would want to fetch file resources in the context from the classpath, instead of using hard-coded directories.

          Comment


          • #6
            It's going to be quite a bit more verbose, but you could define a bean which is a ClassPathResource. Then, define a MethodInvokingFactoryBean which calls the getFile() method on that resource. Then just set this MethodInvokingFactoryBean as the File property value. Like I said, quite a bit more verbose...

            Comment


            • #7
              another way to do

              In case if you want to pass a java.io.File to a bean,

              I think we can just define a nested bean
              <bean class="java.io.File">
              <constructor-arg.... .>
              </bean>


              Can we that and without modifiy any java source code??

              Comment

              Working...
              X