org.springframework.beans.factory.xml.ResourceEntityResolver resolveSchemaEntity

再idea 上可以正常运行但是打包成jar之后就包这个错误
6月 26, 2023 11:15:34 下午 org.springframework.beans.factory.xml.ResourceEntityResolver resolveSchemaEntity
WARNING: DTD/XSD XML entity [http://www.springframework.org/schema/beans/spring-beans.xsd] not found, falling back to remote https resolution

再META-INF\spring.schemas 里面把这个加上去 http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans.xsd
https://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool.xsd
就可以了

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:

    针对该问题,可以尝试以下解决方案:

    1. 将所有的XML资源文件打包到JAR包中

    可以使用Maven或Gradle等构建工具,将src/main/resources目录下的所有XML资源文件打包到JAR包中。具体操作可以参考构建工具的文档或教程。

    1. 在JAR包中显式指定XML资源文件的位置

    在Spring Boot的application.properties或application.yml文件中,添加以下配置:

    spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

    此配置会将XML资源文件的位置显式指定为src/main/resources目录以及其他可能的资源目录,从而避免了在运行JAR包时出现资源文件找不到的问题。

    1. 使用ClassPathResource对象获取XML资源文件

    在代码中使用ClassPathResource对象获取XML资源文件,然后注入到需要使用的对象中。具体代码实现可以参考下面的示例:

    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    
    public class MyBean {
        private Resource resource;
    
        public void setResource(Resource resource) {
            this.resource = resource;
        }
    
        public void getResource() {
            try {
                System.out.println(resource.getFile().getAbsolutePath());
                // do something with the resource
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    // 在配置文件中注入资源文件
    <bean id="myBean" class="com.example.MyBean">
        <property name="resource">
            <bean class="org.springframework.core.io.ClassPathResource">
                <constructor-arg value="file.xml"/>
            </bean>
        </property>
    </bean>
    

    这样就可以在代码中获取XML资源文件,并避免了运行JAR包时出现资源文件找不到的问题。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^