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");
Related
I'm working with Play framework 1.4.3 and my problem is that i created a new folder inside in the controllers folder and in this new folder added new controllers class and I did the same with a folder of the views but when in my html , i tried to declared in the href of a tag ancla the route , this not it found my controller created, only found the controller for default "Application.java".
Example:
This is my folder hierarchy
I can use a controller inside a folder in the controllers folder with Play Framework 1.4.3?
This is my MyServicesControlle:
package controllers.myServices;
import play.mvc.Controller;
public class MyServicesController extends Controller {
public static void index() {
render();
}
}
when i write the whole path in a tag ancla:
my services
throws this exception:
19:11:34,868 ERROR ~
#73mb1o6aa
Internal Server Error (500) for request GET /application/index
Oops: PatternSyntaxException
An unexpected error occured caused by exception PatternSyntaxException: group redeclaration controller; use ({=name}...) for group reassignments
play.exceptions.UnexpectedException: Unexpected Error
at play.Invoker$Invocation.onException(Invoker.java:245)
at play.Invoker$Invocation.run(Invoker.java:307)
at Invocation.HTTP Request(Play!)
Caused by: jregex.PatternSyntaxException: group redeclaration controller; use ({=name}...) for group reassignments
at jregex.Term.makeTree(jregex/Term.java:299)
at jregex.Term.makeTree(jregex/Term.java:219)
at jregex.Term.makeTree(jregex/Term.java:206)
at jregex.Pattern.compile(jregex/Pattern.java:164)
at jregex.Pattern.<init>(jregex/Pattern.java:150)
at jregex.Pattern.<init>(jregex/Pattern.java:108)
at play.mvc.Router$Route.compute(Router.java:828)
at play.mvc.Router.getRoute(Router.java:142)
at play.mvc.Router.appendRoute(Router.java:126)
at play.mvc.Router.parse(Router.java:208)
at play.mvc.Router.parse(Router.java:173)
at play.mvc.Router.load(Router.java:53)
at play.mvc.Router.detectChanges(Router.java:232)
... 1 more
first it was necessary to reference the folder where the controller was located, followed by the name of the controller and then the action.
Following the previous example:
my services
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
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.
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
The actual question is: Is there a way to get XmlWebApplicationContext to load resources using paths relative to the context location? For clarity's sake, let's say "context location" is the location of the first file specified via setConfigLocation() method.
Detailed explanation is below:
I'm using Spring MVC in web tier and Spring IOC in mid tier. Appropriate contexts are defined hierarchically as described in Spring Documentation: web stuff is defined in my-servlet.xml and services et al are defined in services.xml that's loaded via ContextLoaderListener. Mid tier can be deployed either together with web tier (e.g. the whole thing runs within ServletContainer) or separately (in which case services.xml is replaced by remote-services.xml defining remote stubs). The whole setup works perfectly except for the following problem:
I have certain resources (additional XML files, what have you) located in the same folder as services.xml that need to be accessible by said services. Those resources are specified as dependencies in services.xml using relative paths. When mid tier is deployed standalone that works fine, but not when it's deployed within servlet container. In the latter case mid tier context gets instantiated as XmlWebApplicationContext which loads all resources based of servlet context root meaning I have to prefix everything with /WEB-INF/ which I'd really like to avoid. Using PropertyPlaceholderConfigurer presents a similar problem as well.
I know I can work around this somewhat by having resources load from classpath, but that's not ideal either - for standalone deployment it means I need to add configuration folder to classpath and for web deployment it means everything has to be copied under WEB-INF/classes.
Any ideas?
I've ended up extending Spring's XmlWebApplicationContext to allow relative resource paths. This does what I want, that is allows me to use the same context.xml file no matter whether it's deployed as part of web app or standalone.
For anyone interested source is available below. It's published using SOV (Stack Overflow Voting) license :-) which means you're free to do whatever you want with it as long as you upvote this answer :-)
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
/**
* Extends Spring's default web application context to allow relative
* resource paths. Resources without explicitly specified URL protocol
* and / or leading slash are loaded relative to the first location
* from getConfigLocations().
*/
public class SpringApplicationContext extends XmlWebApplicationContext {
#Override
protected Resource getResourceByPath(String path) {
path = StringUtils.cleanPath(path);
if (path.startsWith("/") || (path.indexOf(':')>0)) {
return super.getResourceByPath(path);
}
try {
return super.getResourceByPath(getConfigLocations()[0])
.createRelative(path);
} catch (IOException E) {
// failed to create relative resource - default to standard implementation
return super.getResourceByPath(path);
}
} // getResourceByPath()
}
I agree that is rather annoying. I get around this by doing what you suggest, which is putting my spring config on the classpath, so even though I still use fully-qualified imports, they work under any environment.
I'm not sure why your classpath config needs to be that complex, though. The files can just under your java source folder alongside the java files, so they get handled the same.
Strange. Your solution does not work for me. Here is mine:
package dmp.springframework.web.context;
import java.io.IOException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class RelativeResourceXmlWebApplicationContext extends XmlWebApplicationContext {
#Override
protected Resource getResourceByPath(String path) {
path = StringUtils.cleanPath(path);
if (path.startsWith("/") || (path.contains(":"))) {
return super.getResourceByPath(path);
}
try {
String newFilename = super.getResourceByPath(getConfigLocations()[0]).getFile().getParentFile().getAbsolutePath();
newFilename = newFilename + "/" + path;
return new FileSystemResource(newFilename);
} catch (IOException E) {
// failed to create relative resource - default to standard implementation
return super.getResourceByPath(path);
}
} // getResourceByPath()
}