Path to WebContentFile in Spring - spring

I'm using Spring 3 and would like to get the path to a folder I have created under the WebContent folder. I want to create a File instance there.

A real path to the file under webapp root can be obtained using ServletContext.getRealPath().
Note that this method may return null if the concept of real path has no sense in your deployment configuration (for example, application is deployed as an unpacked .war), in this case you can't create a file there.
An instance of ServletContext in Spring can be autowired.

Related

Need to get path to a folder in resources

I am using Spring boot application. I have this structure in my resource folder
resources
|__customers
|__retail
I have to pass the path to this folder to one of the beans for writing files.
The names of the files are dynamic so I cannot pass a predefined value to the file.
To do this I tried
#value(${classpath:resources/customer/retail} )
Resources resource;
// Also tried
ResourceLoader loader = new FileSystemResourceLoader();
ResourceUtils.getURL(filePathDump).getPath().getClass().getResource(
"indexingData/publication/analysis/dump"
);
// and several other options
but it shows throw file not found or resource not found Exception, Now I checked the way path are defined for each one of them and I am positive they were all specified correctly
I need a way to pass this path to a folder in resources. Please help
you can autowire org.springframework.core.io.ResourceLoader and then get file as follows:
resourceLoader.getResource("classpath:customer/retail/someFile.txt")

Externalizing configuration for Hibernate Search

I am running hibernate search with spring boot. I have written a working configuration for my application. How ever, i want to externalize my configuration and use ./config/hibernate.properties instead of src/main/resources/hibernate.properties. After copying my properties file to the desired location, i am getting and exception:
nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist
Anyone with any idea on how i should tell spring to read my configuration file?
Move your configuration to an src/main/resources/application.properties file and prepend spring.jpa.properties. everywhere, so hibernate.dialect will become spring.jpa.properties.hibernate.dialect, for example.
Then you can use Spring features to move your configuration wherever you want. To move it to ./config/application.properties I suppose you will have to add #PropertySource("./config/application.properties") to one of your #Configuration classes, or something similar.
I'm sure you can also keep the hibernate configuration in a separate file (separate from the rest of your application configuration).
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about externalizing configuration in Spring Boot.
For some reason, it seems hibernate-search will prevent application from starting as long as a hibernate.properties configuration file does not exist. After trying for a while without success, i found a work around for my problem.
First, i created an empty hibernate.properties file and place it under src/main/resources.
Secondly, i moved all hibernate-search configurations to application.properties as follows:
spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch
spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com
spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE
spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow
This way, the application will start and spring will get all configuration from the externalized configuration as documented here.

Spring Boot classpath: vs relative path

In Spring Boot, to access a resource, say myresource.json, I can use both classpath: or a relative path, like ./myresource.json
What is the difference? Which one should I use?
When you call getResource() on a specific application context, and the location path specified doesn't have a specific prefix like ./myresource.json, you will get back a Resource type that is appropriate to that particular application context.
If getResource() was executed against a ClassPathXmlApplicationContext instance it will return a ClassPathResource.If the same method was executed against a FileSystemXmlApplicationContext instance, you'd get back a FileSystemResource. For a WebApplicationContext, you'd get back a ServletContextResource, and so on.
As such, you can load resources in a fashion appropriate to the particular application context.
On the other hand, you may also force ClassPathResource to be used, regardless of the application context type, by specifying the special classpath: prefix.
See this doc

What is difference between class path , file system?

I know that:
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
loads context definition from an XML file located in the classpath, treating context definitions as classpath resources.
ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");
loads context definition from an XML file in the filesystem.
XmlWebApplicationContext
loads context definition from an XML file contained within a web application.
But, what does it exactly mean??
Thanks :)
ClassPathXmlApplicationContext will read files from your classpath. They must be in classes folder of your web application or in a jar in your libfolder.
FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.
XmlWebApplicationContext certainly can access to files contained in your web application, but this is not the most important thing. It implements WebApplicationContext and this means that it will detect ServletContextAware beans, register custom scopes (request, session, ...) among other things.
FileSystemXmlApplicationContext- You need to provide complete full path of xml bean
ClassPathXmlApplicationContext - In this case you DONOT need to set full path, as long as classpath is set
I think above opinion may have something wrong, FileSystemXmlApplicationContext can not access your whole file system, what it can only scan is your whole project folder.In order to prove my conclusion i make a example, first using ClasspathXmlApplicationContext and everything is normal, the second time i move beans.xml file to my desktop folder, so there is no beans.xml file in the project hirachy, and change ClassPathXmlApplicationContext to FileSytemXmlApplicationContext and something goes wrong, error trace below:
INFO: Loading XML bean definitions from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/crabime/Development/IdeaProjects/springInterview/Users/crabime/Desktop/beans.xml]; nested exception is java.io.FileNotFoundException: Users/crabime/Desktop/beans.xml (No such file or directory)
So FileSystemXmlApplicationContext can only detect the current project all folder. For example you make a directory which named config under the project root directory, and you can change your Main Class code like below:
ApplicationContext atx = new FileSystemXmlApplicationContext("/config/beans.xml");
And everything will ok again. So if all like sinuhepop said i think there should something need to be changed.

Spring OSGi classpath resource issue

I'm trying to deploy a spring based bundle in osgi (fuse esb).In spring context, I'm referring to a db4o file which is inside resources folder. As per my understanding, a maven project will make sure that any file available under resources folder will be available in project classpath. I've kept the file under resources/META-INF/spring/repo/test.db4o.
Here's the entry in spring context.
<bean id="objectContainer" class="org.springmodules.db4o.ObjectContainerFactoryBean">
<property name="databaseFile" value="classpath:META-INF/spring/repo/test.db4o" />
</bean>
Once I install and try to start the application, I'm getting the following exception.
java.io.FileNotFoundException: OSGi resource[classpath:META-INF/spring/repo/test.db4o|bnd.id=258|bnd.sym=taxonomydaoimplbundle] cannot be resolved to absolute file path because it does not reside in the file system: bundle://258.0:1/META-INF/spring/repo/test.db4o
I've tried different combinations, but OSGi doesn't seem to recognize this file. Any pointer will be appreciated.
-Thanks
I found the issue finally. ObjectContainerFactoryBean is relying on OSGiResourceBundle to load the resource as a file object. Though OSGiResourceBundle exposes a method called getFile(), it doesn't work as intended in an OSGi environment. It always expects a file protocol whereas the resource returned as an URI has a protocol "bundle".Hence, the exception is being thrown. The workaround is to use a inputstream or getUrl. Since I didn't have the source code of ObjectContainerFactoryBean, I had to extend this class to provide my own implementation which loads the file as an inputstream.

Resources