0

I have a Spring application where I want to take my static resources from a simple file system (D:/.../..).

Could I do something like

mvc:resources mapping="/resources/**" location="d:/../.. .css, ...js" 

Or if there is any other way I could achieve this.

However, I need to do this only in Spring configuration file.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Abhi
  • 93
  • 2
  • 4
  • 14

3 Answers3

0

Yes

You can read files from file system like

Resource resource = appContext.getResource("file:c:\\testing.txt");.

Please refer full example from MkYong here. And if you are creating web application then you should load appContext like:

@Autowired
private ApplicationContext appContext;
Abhendra Singh
  • 1,959
  • 4
  • 26
  • 46
  • I don't have to use bean, actually I need to add .css into my jsp. And this .css is residing in filesystem. I need to do it from applicationContext.xml only and use key in jsp file directly. – Abhi Mar 27 '15 at 11:19
  • Hi, In spring configuration file I have given, context:Property-placeholder location="file:D:\resources\application.properties" I've STATIC_RESOURCES_JS_CSS_URL in my application.properties. But, it is not getting resolved in mvc:resources mapping="/resources/**" location="#{STATIC_RESOURCES_JS_CSS_URL}" How to do that. Any Idea?? – Abhi Mar 27 '15 at 11:30
  • try this ` `. We need to provide path to dir for files. Not complete path of file. One more thing either pass `/` or `\\` to provide path. Reference: http://stackoverflow.com/questions/5456635/spring-serving-static-resources-outside-context-root – Abhendra Singh Mar 27 '15 at 11:46
0

You can use PropertyPlaceholderConfigurer to read your properties file from either classpath or WEB-INF or from file system adding below configuration to your applicationContext.xml file:

A. File in Classpath:

<bean id="dbProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:db.properties" />
</bean>

The keys defined in the db.properties file can be read using ${key name}.

Example: Consider, your properties file contain

 jdbc.url=jdbc:mysql://localhost:3306/dbName

then you can read this property using below command

<property name="url" value="${jdbc.url}" />

B. File in WEB-INF:

When your properties file in under WEB-INF directory, then you just add below code snippet under dbProperties bean & it'll starts looking your properties file in the WEB-INF directory of the application.

<property name="location" value="WEB-INF/db.properties" />

C. File on File System:

When you want to read file from file system then just add below code in dbProperties bean.

<property name="location" value="file:///D:/database/db.properties" />

NOTE: You can either use file:/// or file: in case C.

Hope this solves your problem.

OO7
  • 2,785
  • 1
  • 21
  • 33
0
Resource res=new ClassPathResource("Yourfile");

you can read the file as it is.

gustavohenke
  • 40,997
  • 14
  • 121
  • 129