5

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

Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98

4 Answers4

2

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?

maggu
  • 1,201
  • 9
  • 9
  • The problem happens with the filter definition web.xml. Is looking for applicationContext under WEB-INF. – Andrea Girardi Apr 10 '13 at 21:23
  • Ok, I moved applicationContext.xml under WEB-INF/classes ant it's working fine, but I'm not able to include any file in my applicationContext. I absolutely need to include ehcache.xml and it's not possibile to embedded it under applicationContext. – Andrea Girardi Apr 11 '13 at 08:48
  • That should be straight forward. Add ehcache xml in same location where application context xml is placed. see an example representing this ./ehcache.xml org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory – maggu Apr 11 '13 at 09:17
1

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.

cRx
  • 39
  • 2
1

use

context = new FileSystemXmlApplicationContext(fileContext);

instead of

context = new ClassPathXmlApplicationContext(fileContext);
subodh
  • 6,136
  • 12
  • 51
  • 73
  • Same problem, it's starting to read from an absolute url (the same of Eclipse) and I'm not able to reach the project folder. – Andrea Girardi Apr 11 '13 at 08:22
0

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

Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98