Eclipse Spring Dynamic web project classpath find xml config - java

I'm new with Spring and Hibernate. I did a project from book Prospring4
But I did not a simple java application, but Dynamic web project (for future). I wrote all code and created a app-context-annotation.xml with beans. In book I have a code to load it:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:META-INF/spring/app-context-annotation.xml");
In my project I have a standart path - WebContent/META-INF and I add folder with file where. But always I get a error:
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [META-INF/spring/app-context-annotation.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/app-context-annotation.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/app-context-annotation.xml] cannot be opened because it does not exist
I read many forum answers but I really don't understand WHY it happens!
I tried to convert my project to Maven project, but got this error again.
PLZ tell me what I'm doing wrong?
EDIT.
Ok. Now this is my project structure, but result is the same exception.
enter image description here

Is the Webcontent folder on your classpath? I mean, when you build your project you see app-context-annotation.xml into the classes folder? If not you should add the Webcontent folder to the build path or move the xml to another folder. p.e: src/main/java

Related

XML not exist when it does

I am new to Intellij Idea and trying to follow some tutorials. I tried to load an XML into "new ClassPathXmlApplicationContext()". However, there is an error which stated FileNotFoundException.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [vehicle_beans.xml]; nested exception is java.io.FileNotFoundException:
class path resource [vehicle_beans.xml] cannot be opened because it does not exist
This is my structure:
Here is my code:
XML File location is problem here. As you are using Maven for project, create another folder at level of java named "resources" and put your XML file inside it.
src/main/resources
Resources folder should contain all the config related files to make them available for application to consume. Otherwise you have to pass the full file path to your file reader.
please try this FileSystemXmlApplicationContext instead of ClassPathXmlApplicationContext
private static final ApplicationContext ac =
new FileSystemXmlApplicationContext(
"src/main/java/org/example/vehicle_beans.xml"
);
hope it will work for you

ClassPathXmlApplicationContext error, Spring framework

I have encountered a problem when it comes to the Springs framework, which leads to that the communication between the server and the database does not work.
The project that I created is a Spring project, then refactored to Maven.
At this line in the code:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("projectName/spring.xml");
I get this error:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [projectName/spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [projectName/spring.xml] cannot be opened because it does not exist
But it does exist. And I've tried solutions for this problem such as writing ClassPathXmlApplicationContext("spring.xml") instead. This doesn't help however, since then Spring automatically looks in the folder src/main/resources. This doesn't work for me since my project structure doesn't allow me to add this folder and put a XML-file in it. If I try to create this folder, then it is automatically put inside the Java-resources folder, and Eclipse won't allow me to put XML in there.
This is how my project looks:
enter image description here
Is there a way for me to declare where Spring should look for this spring.xml-file?
The ClassPathXmlApplicationContext assumes that the file is on your classpath (Javy describes how to do load a resource from your classpath).
If you want to load the configuration from your file system (as you're doing), you might want to consider using FileSystemXmlApplicationContext instead. Using this mechanism to load your context you can pass a file system location as you're currently doing.
new ClassPathXmlApplicationContext(this.getClass().getResource("/spring.xml").getPath())
try the code above
hope that helped
Spring doesn't look at the src/main/resources, it looks at the classpath.
If you write projectName/spring.xml you need to have this file in bin/projectName/spring.xml or build/projectName/spring.xml. Where bin or build your build folder.
If you build a jar, this file should be in the jar!projectName/spring.xml.
For the web-application this file should be in the WEB-INF/classes/projectName/spring.xml.
If you add src/main/resources at the class path, then content of this folder will be in the build folder. Maven adds src/main/resources at the class path automatically.
Sometimes you should rebuild (clean) your project in the IDE to have such files in the build folder.
Use "FileSystemXmlApplicationContext" as
ApplicationContext context = new FileSystemXmlApplicationContext("spring.xml");

Loading a jar's context.xml from classpath

I'm trying to load an application context which is inside a jar as a plugin. I use this to load the context:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");
When I load the jar through pom.xml, it works fine.
Then I add it directly in the classpath using eclipse instead of maven to avoid to compile every time (ultimate goal is the shared lib folder in tomcat, not working too). Now spring is unable to find it and return a default context (no exception)
I checked that it's correctly insert in the classpath using:
InputStream in1 = this.getClass().getClassLoader().getResourceAsStream("my-context.xml");
It works.
I checked logs. Using the pom.xml, spring is correctly searching in the jar
Searching directory [...target\classes\META-INF\maven\x.y.z] for files matching pattern [...\x.y.z/target/classes/**/my-context.xml]
Searching directory [...ehealth.poc.module1] for files matching pattern [D:/JRB/Projects/Vivates/workspaces/default/extcom/ehealth.poc.module1/target/classes/**/ecm-context.xml]
...
Resolved location pattern [classpath*:**/my-context.xml] to resources [file [...\target\classes\my-context.xml]]
Loading XML bean definitions from file [...\target\classes\my-context.xml]
...
In the second case, nothing in the log about my jar.
Why spring does not have the same behavior when I use maven or directly the classpath? I maven doing something else than simple adding dependencies location in the classpath?
Finally, we found the solution on eclipse.
The problem comes from the ** in
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**my-context.xml");
It looks like ** doesn't scan the .jar files. Setting the direct path is working :
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:my-context.xml");

applicationContext classpath not found

For some reason (a shiro filter) I saved my application context file in WEB-INF folder. Everything works when I run tomcat but, when I try to get an application context from a controller using :
context = new ClassPathXmlApplicationContext(fileContext);
I receive always this exception:
IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
It seems that under ecplise I'm not able to include WEB-INF under classpath. I took a look to a lot questions here in stackoverflow but I didn't find yet a solution.
If I move the applicationContext.xml file under src/main/java folder, I'm able to get the context but, the shiro filder defined into web.xml file is not able to see the shiro bean defined under applicationContext file (I double checked and the bean is correctly worked). How can I tell to web.xml to get content from src/main/java? Or, how can I reach the applicationContext.xml
WEB-INF is not in your CLASSPATH. WEB-INF/classes is. So why dont you put it in a source folder and package the application?
Do not create an instance of ApplicationContext in your controller. The spring DispatcherServlet already creates one for you. All you need to do is access all bean declarations in you application context file using #Autowired.
use
context = new FileSystemXmlApplicationContext(fileContext);
instead of
context = new ClassPathXmlApplicationContext(fileContext);
Problem has been solved moving all configuration file under WEB-INF/classes and adding the prefix classpath:
<import resource="classpath:spring-data.xml"/>
thanks to all for the help! I really appreciate that!
cheers, Andrea

Current / default directory in vaadin project

I am working on reports. Now I have a jrxml file which I have to load so that I can parse it. However, the problem is, I want to load the file with its name directly
String path = "myreport.jrxml";
But I am unable to find that where should I place that file in my project so that the above path is valid. In simple java project, we just place our desired file in our project folder where src folder is placed, but it is not working in vaadin project. So where should I place my file.
Thanks.
Are you deploying your project to a web server? Then you would need to find out what is the default working directory of that web server.
You could use:
JRXmlLoader.load("/myreport.jrxml");
or if you want to test if the resource is found and take another tries you could load it as a resource and pass it load method from JRXmlLoader as follows:
JRXmlLoader.load(getClass().getResourceAsStream("/myreport.jrxml"));

Categories