External path Properties file in spring mvc - spring

1.spring-servlet.xml -
<context:property-placeholder location="local path of database properties file/database.properties"/>
if i put absolute path in above then i can access the file.
but my absolute path is in general.properties and
general.properties is in my class path
2.general.properties -
proPath=D:\\Propertiesfile
so how can i put database.properties file path in spring-servlet.xml
Thanks in advance.

You need to understand the Spring Resource abstraction. By default if a resource is not qualified with a handler prefix e.g classpath: file: etc Spring determines the type of resource to load based on the type of ApplicationContext used.
If its a ClassPathXmlApplicationContext it uses a classpath resource. If its a FileSystemXmlApplicationContext it uses a file system resource. If its a web application context it uses Servlet context resource. However you can force it to load a specific type irregardless of application context type by appending the handler prefix e.g classpath:database.properties which loads your file from the classpath
You can use <context:property-placeholder location="classpath:database.properties"/> if your database.properties is in the classpath
Or <context:property-placeholder location="database.properties"/> if your database.properties is in the root of the web application

Related

Import resource based on JNDI in Spring 4.x

I want to import a resource based on a JNDI entry.
My application-context.xml looks like:
<jee:jndi-lookup id="td.naccms.cods2.config.path"
jndi-name="td.naccms.cods2.config.path" expected-type="java.lang.String"
default-value="classpath:application-context-persistence.xml" />
<context:property-placeholder />
<import resource="${td.naccms.cods2.config.path}" />
And tomcat returns the following error:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [application-context-cods2-web.xml]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'td.naccms.cods2.config.path' in string value "${td.naccms.cods2.config.path}"
Note, I do not want to load the resources from a property files because the resourse to be imported is going to change based on the JNDI.
When using placeholders ${...} you can use a : to specify another value in case the expression doesn't resolve (this could even be another placeholder!).
So instead of hacking around with lookups yourself just pass the value using a :.
<import resource="${td.naccms.cods2.config.path:classpath:application-context-persistence.xml}" />
The Environment abstraction (used to do resolving of placeholders in newer Spring versions) also checks JNDI for the property next to the System Environment, Property files (loaded through #PropertySource) and in a web application the ServletContext.

Spring context:property-placeholder not loading properties

I have a Spring config file with one bean. The bean has 2 properties that are populated from a properties file. I am using the following config in my Spring file to copy the values in but it does not seem to be working.
<context:property-placeholder ignore-resource-not-found="true"
system-properties-mode="NEVER"
location="classpath:my.properties"/>
The weird thing is - this has worked before. Can anyone tell me why this would not be successful in copying the properties across?
I know the infomation given is scant. I'll add elaborate if needs be.
Try the classpath*: prefix. And try giving the relative path to the conf file, and make sure it is really on the classpath (note that WEB-INF is not on the classpath - the classpath of a webapp starts at WEB-INf/classes (and lib))

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.

how to inject webapp resource using relative path in spring

I have a bean which im trying to pass a relative path at web application startup. It works for an absolute path (running tomcat within eclipse) such as (C:/dev/workspace/project/src/main/webapp/WEB-INF/resource/)
However when I try to pass it a relative path such as WEB-INF/my_resource/ it says cannot find the location C:/dev/eclipse/WEB-INF/my_resource/ probably because tomcat is running within eclipse. How can I make this path relative so that it will be always picked up from whatever webcontainer is running it no matter the location of the webapp?
Ive read in place to use the servletconfig.getRelativePath(/) but neither know how to obtain the servlet config from within my bean or even if this is the right thing to do in Spring... Please help
The source code for my bean class and bean configuration xml can be found below
public class SuggestionIndexSearcher extends IndexSearcher {
private String indexSearcherType;
public SuggestionIndexSearcher(String type, String path){
super(path);
this.indexSearcherType = type;
}
...
}
The bean is defined in teh beans xml as...
<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
<constructor-arg index="0" value="KMSearcher" />
<constructor-arg index="1" value="WEB-INF/resource/keyword" />
</bean>
If your change your bean to take org.springframework.core.io.Resource (javadoc) instead of String, then Spring will automagically coerce your path into the appropriate type of Resource. When running inside a servlet container, Spring will generally pick ServletContextResource, in which the path becomes relative to the webapp root (so WEB-INF/my_resource/ should work as you expect).
How your code chooses to handle Resource depends what you want to do with it, obviously.
No change is required in your XML config, just keep passing the path string as before. See the Spring manual for a wider description of resources.
Change the argument type from String to Resource - spring will then do the conversion and give you the resource object which you can process. You should be using an XMLWebApplicationContext (which is the default if the context is being created by the ContextLoaderListener or DispatcherServlet).
Take a look at this page for more details.

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