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
Related
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
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");
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");
How to get the full path of file located in /src/reportpackage/daysheet.jasper in the spring mvc application?. How to get the string format of the path?
You need to understand that after the build your file would end up in classpath and what you would be loading through Spring resource loading mechanism is a classpath resource. You can use spring notation for it like this
appContext.getResource("classpath:reportpackage/daysheet.jasper");
where appContext is the spring application context which can be autowired in any Spring bean
How do I reference an external beans.xml file based on its relative location to where my jar file is deployed? This is what I have now, which doesn't work:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("../beans.xml");
I want it out of the classpath so I can change things without redeploying, but relative to the deployed location of the jar so that it will work no matter where I put the jar and the beans.xml in the file system, as long as they are correctly situated relative to each other.
This is Spring btw, if that helps.
Thanks!
An application context file must be on the classpath to allow ClassPathXmlApplicationContext to work. To use a relative path you would have to use FileSystemXmlApplicationContext.
ApplicationContext context = new FileSystemXmlApplicationContext("../beans.xml");
You can import the external beans configuration file in your spring-config.xml you can import the extends
<beans ...>
<import resource="file:/path/to/external/config.xml"/>
</beans>
The above Spring config will import /path/to/external/config.xml. Having an external file will allow you to configure beans without having to rebuilding your main war/jar.
In your Java code, you can load it as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
and the config.xml file should be in:
project-classpath/config.xml
More about loading external configuration files, you can find here