我正在尝试在SpringPrgramm2中访问Bean.xml,而SpringProgramm1项目中存在Beans.xml。两者都是java项目。
请在下面找到我的代码:
Eclipse中的SpringProramm1 java项目: (只有Beans.xml出现在src文件夹中)
<?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.xsd">
<bean id="textEditor" class="com.surajhome.practice.spring.TextEditor">
<property name="spellChecker">
<bean id="spellChecker" class="com.surajhome.practice.spring.SpellChecker"/>
</property>
</bean>
</beans>
SpringPrgoramm 2 java项目:
下面的MainApp.java代码无法从另一个java项目访问beans.xml。
package com.surajhome.practice.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext appContext=new ClassPathXmlApplicationContext("classpath:SpringProgram1/Beans.xml");
TextEditor obj1 = (TextEditor) appContext.getBean("textEditor");
obj1.spellCheck();
}
}
我们如何在SpringProgramm2项目中访问此Beans.xml文件?
例外:
Apr 21, 2018 7:17:24 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@694f9431: startup date [Sat Apr 21 19:17:24 IST 2018]; root of context hierarchy
Apr 21, 2018 7:17:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [SpringProgram1/Beans.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [SpringProgram1/Beans.xml]; nested exception is java.io.FileNotFoundException: class path resource [SpringProgram1/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:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:258)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:621)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:142)
at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:85)
at com.surajhome.practice.spring.MainApp.main(MainApp.java:11)
Caused by: java.io.FileNotFoundException: class path resource [SpringProgram1/Beans.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
答案 0 :(得分:0)
首先,我认为你的项目结构没有凝聚力。 SpringProgramm2项目中的类在SpringProgramm1项目XML中配置是没有意义的,因为这些类属于SpringProgramm2项目。 所以,我举了一个例子,说明你的项目如何使用Maven更具凝聚力:
在我的示例中,SpringProgramm2将依赖于SpringProgramm1:
<dependency>
<groupId>br.edu.claudivan</groupId>
<artifactId>SpringPrgramm1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
在SpringProgramm1中,您可以保留bean的配置。在SpringProgramm2中,您保持MainApp.java类加载上下文XML,如下所示:
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/Beans.xml");
TextEditor obj1 = (TextEditor) context.getBean("textEditor");
obj1.spellChecker();
}
}
我将示例代码放在my github上。
希望这有帮助!