[springboot bug] mac 文件读取灵异事件

简介: [springboot bug] mac 文件读取灵异事件

一开始是想尝试一下spring在过去的xml文件配置bean 的感觉,但是在测试 FileSystemXmlApplicationContext 的时候,反复确认文件路径没有问题,将 / -> \\ 也不起作用,后决定debug一下,发现根因。记录一下,方便springboot 的新人参考。(声明,开发我不会使用到这个方式创建 bean)

测试代码

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 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="bean1" class="com.example.show_bean.ApplicationContextTest.Bean1"/>
  <bean id="bean2" class="com.example.show_bean.ApplicationContextTest.Bean2">
    <property name="bean1" ref="bean1"/>
  </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 中传入路径,如果是'/' 开头会被截掉一个,导致路径不全。

解决方案

简单粗暴多加一个 /

目录
打赏
0
0
0
0
41
分享
相关文章
java springboot监听事件和处理事件
通过上述步骤,开发者可以在Spring Boot项目中轻松实现事件的发布和监听。事件机制不仅解耦了业务逻辑,还提高了系统的可维护性和扩展性。掌握这一技术,可以显著提升开发效率和代码质量。
97 33
java springboot监听事件和处理事件
通过上述步骤,开发者可以在Spring Boot项目中轻松实现事件的发布和监听。事件机制不仅解耦了业务逻辑,还提高了系统的可维护性和扩展性。掌握这一技术,可以显著提升开发效率和代码质量。
90 13
|
2月前
|
Java Spring Boot监听事件和处理事件
通过上述步骤,我们可以在Java Spring Boot应用中实现事件的发布和监听。事件驱动模型可以帮助我们实现组件间的松耦合,提升系统的可维护性和可扩展性。无论是处理业务逻辑还是系统事件,Spring Boot的事件机制都提供了强大的支持和灵活性。希望本文能为您的开发工作提供实用的指导和帮助。
112 15
Java Springboot监听事件和处理事件
通过这些内容的详细介绍和实例解析,希望能帮助您深入理解Spring Boot中的事件机制,并在实际开发中灵活应用,提高系统的可维护性和扩展性。
67 7
|
10月前
|
Spring Boot 监听 Redis Key 失效事件实现定时任务
Spring Boot 监听 Redis Key 失效事件实现定时任务
168 0
大事件后端项目34_登录优化----redis_SpringBoot集成redis
大事件后端项目34_登录优化----redis_SpringBoot集成redis
大事件后端项目34_登录优化----redis_SpringBoot集成redis
SpringBoot3 事件和监听器
SpringBoot3 事件和监听器
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等