如何解决网站兼容,长沙有哪些知名网站,hot插件 wordpress,洛阳尚贤网络科技有限公司一开始是想尝试一下spring在过去的xml文件配置bean 的感觉#xff0c;但是在测试 FileSystemXmlApplicationContext 的时候#xff0c;反复确认文件路径没有问题#xff0c;将 / - \\ 也不起作用#xff0c;后决定debug一下#xff0c;发现根因。记录一下#xff0c… 一开始是想尝试一下spring在过去的xml文件配置bean 的感觉但是在测试 FileSystemXmlApplicationContext 的时候反复确认文件路径没有问题将 / - \\ 也不起作用后决定debug一下发现根因。记录一下方便springboot 的新人参考。声明开发我不会使用到这个方式创建 bean 文章目录 测试代码运行结果debug解决方案 测试代码
package com.example.show_bean;import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class ApplicationContextTest {public static void main(String[] args) {testFileSystemXmlApplicationContext();}/*** 基于磁盘路径的 xml 配置文件来创建*/private static void testFileSystemXmlApplicationContext() {FileSystemXmlApplicationContext context new FileSystemXmlApplicationContext(/Users/xx/project/java/easy_spring_mvc/learn_spring/show_bean/src/test/resources/b01.xml);System.out.println(context.getBean(Bean2.class).getBean1());}/*** java 配置类来创建*/private static void testAnnotationConfigApplicationContext() {}public static class Bean1 {Bean1() {System.out.println( 1);}}public static class Bean2 {private Bean1 bean1;public Bean2() {System.out.println( 2.);}public void setBean1(Bean1 bean1) {this.bean1 bean1;}public Bean1 getBean1() {return bean1;}}}
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idbean1 classcom.example.show_bean.ApplicationContextTest.Bean1/bean idbean2 classcom.example.show_bean.ApplicationContextTest.Bean2property namebean1 refbean1//bean/beans文件
运行结果
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/Users/xx/project/java/easy_spring_mvc/learn_spring/Users/csjerry/project/java/easy_spring_mvc/learn_spring/show_bean/src/test/resources/b01.xml]; nested exception is java.io.FileNotFoundException: Users/xx/project/java/easy_spring_mvc/learn_spring/show_bean/src/test/resources/b01.xml at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342) at com.example.show_bean.ApplicationContextTest.main(ApplicationContextTest.java:10) Caused by: java.io.FileNotFoundException: Users/csjerry/project/java/easy_spring_mvc/learn_spring/show_bean/src/test/resources/b01.xml at org.springframework.core.io.FileSystemResource.getInputStream(FileSystemResource.java:189) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333) debug 原因 在 FileSystemXmlApplicationContext 中传入路径如果是/ 开头会被截掉一个导致路径不全。 解决方案
简单粗暴多加一个 /