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.
Related
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")
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
I am currently working with Heritrix and I have a standard installation (this one: http://builds.archive.org/maven2/org/archive/heritrix/heritrix/3.2.0/) and it works fine.
But now I want to write and add my own extensions e.g. change the priority of urls which should be crawled or just a simple extractor. I can inspect the java code of an existing extractor but how can I add it to the crawler?
I tried to export my java test project to a jar file and put this file in the lib folder of Heritrix (where the other libraries are). Furthermore I added a bean to my job's cxml file.
But after starting I got this error: 2014-11-07T19:51:40.296Z SEVERE Could not instantiate bean class [myModule.TestClass]: No default constructor found; nested exception is java.lang.NoSuchMethodException: myModule.TestClass.(); Can't create bean 'myModule.TestClass#0'
It is just the extractorHTML renamed and in a new project and exported to a jar file.
Any idea what is wrong? I read all the documentations but there are only explanations how to write extensions and not how to add it?
Greetings and thank you :-)
I think the issue is the class loader requires a default constructor (A constructor - which takes no arguments) Add a default constructor
public YourClass() { }
and the required getters and setters for setting the member variables.
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.
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.