ClassPathXmlApplicationContext - BeanDefinitionStoreException: IOException parsing XML document - java

I have some unit tests that load an application context from an XML file:
#BeforeClass
public static void setUp() throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("test-application-context.xml");
}
This throws an exception:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test-application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [test-application-context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:542)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
My unit test class is in the folder:
myproj/myproj-configuration-dal/src/test/java/com/mypatterns/api/data/impl/LogTest.java
The resource file is in:
myproj/myproj-configuration-dal/src/test/resources/test-application-context.xml
I tried moving the xml file around and, also the entire resouce folder, to the following locations: the src folder, the folder where LogTest is, to the main folder (where the non-test sources are) and so on.
Where exactly is this supposed to be? Where is ClassPathXmlApplicationContext looking for the resources?
Thanks,
Serban

It turns out that replacing ClassPathXmlApplicationContext with FileSystemXmlApplicationContext makes the job much easier. You can use this one and you won't have this problem.

Related

Spring/NetBeans - java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist

Whenever I try to run my java project, I get file not found exception saying that beans.xml cannot be found. I am using NetBeans and have read that I might need to set my classpath to the correct directory or the project will not run correctly (Eclipse does this automatically(?)). Running this same program with same library works with Eclipse. I don't know if my problem is Spring related or classpath related (but I assume it's a classpath issue).
MainApp.java
package hello;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloWorld hello = (HelloWorld) context.getBean("helloWorld");
hello.getMessage();
}
}
HelloWorld.java
package hello;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Message: " + message);
}
}
beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "helloWorld" class = "hello.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
ST
run:
Mar 20, 2017 11:05:04 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#4ee285c 6: startup date [Mon Mar 20 23:05:04 EDT 2017]; root of context hierarchy
Mar 20, 2017 11:05:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at hello.MainApp.main(MainApp.java:8)
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 13 more
/home/john/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
File and Project structures
Thanks for the help.
SOLUTION: In NetBeans the beans.xml type file you are using to instantiate your beans must contain it's path. In my case, I had to type out:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/hello/beans.xml");
Instead of just:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Your beans.xml is in hello package so you should refer it using:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("hello/beans.xml");
P.S.: A better practice would be to place this in resources directory.
Also found out if on windows, double backslashes (\) should be used.
so this:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/hello/beans.xml")
would be like this:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("\\hello\\beans.xml")
ResourceLoader have four kind of method to load resource
(classpath,http,file,none(depending on applicationContext)).
You can learn more about it

How can I confirm that ClassPathXmlApplicationContext finds a file?

We have an app that does not run in a web container so I am trying to start Spring up. In the "main" function we have this:
public static void main(String[] args) throws InterruptedException, IOException, AlreadyAliveException, InvalidTopologyException, AuthorizationException {
// starting up spring...
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");
DataSourceTransactionManager dstm = applicationContext.getBean("markiscool", DataSourceTransactionManager.class);
dstm.toString();
}
And my applicationContext.xml contains:
<bean id="markiscool" class="blah.entities.LocationEntity" />
The app, on startup, logs this:
[INFO] ClassPathXmlApplicationContext - Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext#295cd6e5:
startup date
[Thu Dec 17 10:28:28 CST 2015]; root of context hierarchy
Exception in thread "main"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'markiscool' is defined
I have tried putting garbage in the xml file but it doesn't fail so it must not be finding the file. Also, the file is on the classpath:
I have also stuck the file in about every place I can think of. It does not load. Help!
Try to use FileSystemXmlApplicationContext:
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("/conf/applicationContext.xml");
Instead of:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");
Try this:
Right click -> properties -> Deployment Assembly
Put an entry: conf -> /
try
new ClassPathXmlApplicationContext("applicationContext.xml");
1) Put applicationContext.xml into the config folder
2) Tell Maven to include applicationContext.xml in the jar it creates
3) Use the function ClassPathXmlApplicationContext to pull in the file
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:conf/applicationContext.xml");
Note: FileSystemXmlApplicationContext will not work if you are trying to access a file inside of a jar. For this reason I recommend using ClassPathXmlApplicationContext because it works whether you are running the project in eclipse or via the jar

java.io.FileNotFoundException: class path resource

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
public static void main(String args[]){
Resource res= new ClassPathResource("E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(res);
Employee s=(Employee)factory.getBean("e");
}
}
Above is my program and the error show is:
Oct 13, 2015 8:42:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at contructor.Test.main(Test.java:21)
Caused by: java.io.FileNotFoundException: class path resource [E:/JAVA/Springs Netbean Projects/Employee/src/contructor/applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
What could be the possible problem? please help as i am new to springs
I am facing same problem, how can i solve it?
You are trying to get a reference to a file as a classpath resource but the path you have given is not a path of a file inside the classpath.
Instead of harcoding the full path to the file, use a path that is relative to the root source of the project. If src is the root source directory, use this:
Resource res = new ClassPathResource("/contructor/applicationContext.xml");
Because it is ClassPathResource I think contructor/applicationContext.xml is enough

How to get path in spring to access configuration file

I have written a Spring program where the following classes, interfaces and xml files are in different packages. I use Eclipse Kepler.
SelectClient.java
package com.rajeev.spring.action;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.rajeev.spring.DAOI.Select;
/**
* #author rajeev
*
*
*/
public class SelectClient {
/**
* #param args
*/
public static void main(String[] args) {
String path=System.getProperty("user.dir");
System.out.println(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
Resource resource=new ClassPathResource(path+"/src/com/rajeev/spring/DAOImpl/SelectCfg.xml");
XmlBeanFactory beanFactory=new XmlBeanFactory(resource);
Object object=beanFactory.getBean("sb");
Select select=(Select)object;
System.out.println("emp name is:"+select.fetchName(101));
}
}
The problem is that when I execute the SelectClient.java, it is giving following error
E:\javahyd\eclipse\Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml
Jan 17, 2014 11:41:43 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:78)
at org.springframework.beans.factory.xml.XmlBeanFactory.(XmlBeanFactory.java:66)
at com.rajeev.spring.action.SelectClient.main(SelectClient.java:26)
Caused by: java.io.FileNotFoundException: class path resource [E:/javahyd/eclipse/Spring_DataSource_Object_Inject/src/com/rajeev/spring/DAOImpl/SelectCfg.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 4 more
The above exception, is due to the path. When I use the same path in run(windows+r) the particular file it is opening. I dont want to keep my configuration file out of any packages.
ClassPathResource is used to refer the path of the resource in the classpath. The path argument passed in the ClassPathResource refers to the absolute path within the class path.So, use
Resource resource=new ClassPathResource("com/rajeev/spring/DAOImpl/SelectCfg.xml");
Try to use FileSystemResource to mention absolute path in the filesystem
Use FileSystemResource instead of ClassPathResource resource and give complete path e.g Resource resource=new FileSystemResource("C://path/to//your//config.xml");

Using Spring Security in an OSGi Application without SpringDM

It's about half a day I'm trying to find a way to migrate my implementations on SpringSecurity to the context of OSGi(equinox) bundles without switching to SpringDM.
Currently we have two projects:
1. I have an implementation of Spring Security based on some xml configuration files to handle authentication and authorization.
2. On the other hand, we have a huge OGSi bundled project structure with about 200 bundles which need to be integrated with a security bundle(the one described above)
As the first step to create mySpringBasedSecurityBundle I need to run this method after loading mySecurityBundle to access the security configuration xml-file located : com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml
which prepared me Spring-DataSource.xml and Spring-Security.xml as following:
private void loadApplicationContext()
{
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL);
new ThreadLocal<Object>();
setApplicationContext(new ClassPathXmlApplicationContext(SPRING_CONTEXT_ADDRESS));
}
But unfortunately this Exception occurred:
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:126)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.myComp.backend.appsecurity.spring.appSecurityManager.loadApplicationContext(appSecurityManager.java:233)
at com.myComp.backend.appsecurity.spring.appSecurityManager.internalInitialize(appSecurityManager.java:106)
at com.myComp.BaseModuleManager.initialize(BaseModuleManager.java:511)
at com.myComp.BaseModuleManager.initialize(BaseModuleManager.java:1)
at com.myComp.backend.BaseBackendManager.initializeSubBackendManagers(BaseBackendManager.java:643)
at com.myComp.backend.BaseBackendManager.prepareSubBackendManagers(BaseBackendManager.java:885)
at com.myComp.backend.BackendManager.internalStart(BackendManager.java:127)
at com.myComp.BaseModuleManager.start(BaseModuleManager.java:574)
at com.myComp.BaseModuleManager.start(BaseModuleManager.java:1)
at com.myComp.application.BaseApplicationStub.startBackendManager(BaseApplicationStub.java:2407)
at com.myComp.Application.frameworkEvent(Application.java:72)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:874)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)Caused by: java.io.FileNotFoundException: class path resource [com/myComp/backend/appsecurity/spring/resources/Spring-Context.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
.. 26 more
as much as I search the web, the only recommendation for this issue ends to the application on SpringDM, but it is not acceptable for our ProjectManager to switch to SpringDM and to be honest I have no idea about SpringDM.
Would you please help me resolve this issue using Spring Core functionalities.
Thanks alot
Moein
You don't actually need Spring DM. It simply provides a bridge between OSGi and Spring, with some niceties like loading all your context files properly in an OSGi environment. You can do this yourself as well, but you have to compensate for the classloading issues, which is the problem you are having.
Try this to fix your classloading issues.
ApplicationContext ctx = new ClassPathXmlApplicationContext(myCtxPath)
{
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader)
{
super.initBeanDefinitionReader(reader);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.setBeanClassLoader(getClassLoader());
}
}
BTW, the second line of your method serves no purpose.

Categories