我有一个名为SpringPoc.jar的jar,我使用eclipse作为runnable jar导出
在罐子里面我有2个班级 一个是public class NormalServlet {
public void executeScheduler() {
ApplicationContext context =
new ClassPathXmlApplicationContext("com/serv/Beans.xml");
CallMeth obj = (CallMeth) context.getBean("callMeth");
obj.test();
}}
第二课是主要应用
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/serv/Beans.xml");
CallMeth obj = (CallMeth) context.getBean("callMeth");
obj.test();
}}
我在Beans.xml中定义了一个bean
<bean id="callMeth" class="com.poc.CallMeth"/>
然后我在eclipse中创建了一个带有测试类的简单java项目 它调用jar并尝试执行上面的类NormalServlet
public class Test {
public static void main(String[] args) {
try {
URL[] classLoaderUrls = new URL[]{new URL("file:\\D:\\MyWorkspace\\Scheduler\\src\\com\\callit\\SpringPoc.jar")};
URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
Class classToLoad = Class.forName ("com.serv.NormalServlet", true, urlClassLoader);
Method method = classToLoad.getDeclaredMethod ("executeScheduler");
Object instance = classToLoad.newInstance ();
Object result = method.invoke (instance);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我运行测试类
时我收到以下错误
Sep 09, 2016 11:48:30 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c50507: startup date [Fri Sep 09 11:48:30 IST 2016]; root of context hierarchy
Sep 09, 2016 11:48:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/serv/Beans.xml]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.callit.Scheduler.callMe(Scheduler.java:40)
at com.callit.Scheduler.main(Scheduler.java:15)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/serv/Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/serv/Beans.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
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:540)
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)
at com.serv.NormalServlet.executeScheduler(NormalServlet.java:17)
... 6 more
Caused by: java.io.FileNotFoundException: class path resource [com/serv/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:329)
... 19 more
如果我从cmd运行相同的jar 作为java -jar SprrinPoc.jar 调用MainApp类并且Beans.xml加载正常
问题是当我从测试类
外部调用Normalservlet时它为什么不加载非常感谢
答案 0 :(得分:0)
只要依赖jar文件包含xxx.xml,它也应该在您的宿主项目类路径中可用。这将在classpath
中的任何位置搜索applicationcontext.xmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:**/applicationContext.xml");