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")
Related
I've tried setting a class loader in the init() function like so:
classLoader = Thread.currentThread().getContextClassLoader();
I subsequently try the following in my OnScheduled method:
InputStream in = classLoader.getResourceAsStream(filename);
My Java project has a file at src/main/resources/foo/bar. Regardless of whether I pass "foo/bar" or "/foo/bar" as "fileName" in the above code, classLoader.getResourceAsStream() returns null. I have verified that the file is inside the NAR archive, at META_INF/bundled_dependencies/foo/bar.
What's the best way of accessing the file's contents from my code?
MyProcessor.class.getResourceAsStream(...);
Is the way to go here. You want to use the classloader associated with your customer processor or controller service, not the current running thread.
I want to use resources from static directory and this directory may have another subdirectories.
In spring boot application.properties file I am using below key to refer outside directory:C:/custResources/. I am able to refer resources from this directory but not from the subdirectoriese e.g. C:/custResources/1, C:/custResources/2 etc.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/C:/custResources/
Looks like I am doing something wrong.
Below property is sufficient to add the whole directory and its folder as static location.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:/C:/resources
I am able to access video placed inside C:/resources/1 as localhost:8080/1/sample.mp4 and so on.
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 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.
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.