当我执行Spring应用程序时,我遇到了错误。 我有两个类ConsoleWriter和FileWriter,它们的bean会被自动发现并通过添加组件扫描自动注入这些类所在的包。
我收到意外的输出文件未找到,即找不到beans1.xml。
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [com/jalaj/programming/testprog/beans1.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/jalaj/programming/testprog/beans1.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:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.programming.testprog.test.main(test.java:14)
Caused by: java.io.FileNotFoundException: class path resource [com/jalaj/programming/testprog/beans1.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
以下是我的相同代码
public interface LogWriter { //having one method write
public void write(String test);
}
@Component
public class ConsoleWriter implements LogWriter{
public void write(String test){ //method write prints data into console
System.out.println("Write to Console q"+test);
}
}
@Component
public class FileWriter implements LogWriter {
public void write(String test) { //method write prints data into console
System.out.println("Write to file"+test);
}
}
@Component
public class Logger {
@Inject
private ConsoleWriter consoleWriter;
@Inject
private FileWriter fileWriter;
public ConsoleWriter getConsoleWriter() {
return consoleWriter;
}
public void setConsoleWriter(ConsoleWriter consoleWriter) {
this.consoleWriter = consoleWriter;
}
public FileWriter getFileWriter() {
return fileWriter;
}
//use this Annotation on top of a setter or DataMember
public void setFileWriter(FileWriter fileWriter) {
this.fileWriter = fileWriter;
}
public void writeFile(String text){
if(fileWriter!=null)
fileWriter.write(text);
}
public void writeConsole(String text){
if(consoleWriter!=null)
consoleWriter.write(text);
}
@PostConstruct
public void init(){
System.out.println("Initialization of beans done");
}
@PreDestroy
public void destroy(){
System.out.println("Destruction of beans done");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="com.test.programming.testprog">
</context:component-scan>
</beans>
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/com/jalaj/programming/testprog/beans1.xml");
Logger logger=(Logger)context.getBean("logger");
logger.writeConsole("Hello Jalaj ");
logger.writeFile("Hello ishu ");
((AbstractApplicationContext) context).close();
}
}
任何人都可以指导我如何解决/调试此问题。
答案 0 :(得分:0)
尝试将beans1.xml放在src / main / resources下并以这种方式加载文件:
ApplicationContext context = new ClassPathXmlApplicationContext(“beans1.xml”);
答案 1 :(得分:0)
(从我对问题的评论中复制,以便将来的读者更容易看到)
您的软件包不是com.jalaj.programming.testprog
,而是com.test.programing.testprog
。尝试更改代码中的路径以反映该路径。