I packed the spring bean factory config file into a jar file. And then I use the jar in my project. It is reported that "can not find the config file...".
I works without jar if I unpack those into "class" dir.
Any help will be much appreciated!
I packed the spring bean factory config file into a jar file. And then I use the jar in my project. It is reported that "can not find the config file...".
I works without jar if I unpack those into "class" dir.
Any help will be much appreciated!
It Seems that you access your configuration via a filename. This cannot work within a jar. In a jar you have to locate the configuration via the classpath.
Maybe ClassPathXmlApplicationContext can be helpful for you.
Hope that helps,
Andreas
the usual questions: is the jar on the classpath?
Can you show us some code where you're trying to access it? how about your build, what does the internal structure of the jar look like?
Yes, the jar is in the classpath. Because the found factory class is in my jar.
The sample code is:
private final URL url = this.getClass().getResource("oracle_spatial_Contex t.xml");
// String[] files = {url.getPath()};
String[] files = {url.getFile()};
ac = new FileSystemXmlApplicationContext(files);
Maybe the problem is FileSystemXmlApplicationContext. I will try to use ClassPathXmlApplicationContext.
Thanks!
but though I try ClassPathXmlApplicationContext, it was also reported the FileNotFoundException Exception:
I give my code here(oracle_spatial_Context.xml is in the same package as this class.):
URL url = this.getClass().getResource("oracle_spatial_Contex t.xml");
String[] files = {url.getPath()};
ac = new ClassPathXmlApplicationContext(files);
Exception:
Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from URL [file:/E:/LXJ/webapp02/WEB-INF/lib/cluster_oracle_spatial-1.0.jar!/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml]; nested exception is java.io.FileNotFoundException: E:\LXJ\webapp02\WEB-INF\lib\cluster_oracle_spatial-1.0.jar!\com\efn\Engine\FriendChain\ClusterOracleS patial\Util\oracle_spatial_Context.xml
at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:144)
at org.springframework.beans.factory.support.Abstract BeanDefinitionReader.loadBeanDefinitions(AbstractB eanDefinitionReader.java:83)
at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:101)
at org.springframework.context.support.AbstractXmlApp licationContext.loadBeanDefinitions(AbstractXmlApp licationContext.java:69)
at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:87)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:262)
at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:80)
Is there directory structure inside the Jar?
http://www.springframework.org/docs/...hResource.html
ClassPathXmlApplicationContext("com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ");
Does something like that work?
yes, there is a directory structure inside the jar.
I use your given method, but it was still error:
Caused by: org.springframework.beans.factory.BeanDefinitionSt oreException: IOException parsing XML document from class path resource [com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ]; nested exception is java.io.FileNotFoundException: Could not open class path resource [com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml ]
After digging around in the spring src a little, maybe some of these might work?
From the ContextSingletonBeanFactoryLocator:
Code:ClassPathXmlApplicationContext("classpath*:oracle_spatial_Context.xml ");
Inside AdvisorAutoProxyCreatorTests you see:
So maybe try it with leading slashCode:/** * Return a bean factory with attributes and EnterpriseServices configured. */ protected BeanFactory getBeanFactory() throws IOException { return new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreator.xml"); }
And inside SingletonBeanFactoryLocatorTests you see:Code:ClassPathXmlApplicationContext("/com/efn/Engine/FriendChain/ClusterOracleSpatial/Util/oracle_spatial_Context.xml");
so spring's loaders look pretty flexible about know how to dig around in the classpathCode:public void testBaseBeanFactoryDefs() { // Just test the base BeanFactory/AppContext defs we are going to work with // in other tests. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "/org/springframework/beans/factory/access/beans*.xml"); }
Actually it is the default behaviour of the java ClassLoader#getResourceXXX methods. So it is no speciality of spring (though efficiently instrumentedOriginally Posted by khagel
).
Regards,
Andreas
try to put the xml file in the root of the jar file and then use
Code:ClassPathXmlApplicationContext("oracle_spatial_context.xml");