【问题标题】:Spring cannot find class path resourceSpring找不到类路径资源
【发布时间】:2016-06-15 06:22:57
【问题描述】:

我多次搜索我的问题。其中一些很好,但不是我的问题的解决方案。从现在开始,我花了几个小时来解决我的问题。

我使用 Maven 在 Eclipse 中启动了一个项目。 之后,我添加了 spring 和 hibernate 作为依赖项。 这个项目不是一个普通的静态 void main() 项目。 它是一个插件,我可以在其他正在运行的程序中实现它。

现在让我解释一下我的问题: 当我尝试启动我的插件时(仅供参考:将其放入主程序的 /plugins 文件夹中),我得到一个 I/O 异常:

org.bukkit.plugin.InvalidPluginException: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException:
class path resource [applicationContext.xml] cannot be opened because it does not exist

applicationContext.xml 在我的 src/main/resources 文件夹和我的类路径中。我用winRar检查了它。在构建过程之后,applicationContext.xml 位于根目录中。 CLICK TO OPEN THE IMAGE

我还使用 apache maven-shade-plugin 来包含我的所有依赖项。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <mainClass>com.lostforce.core.LostForceCore</mainClass>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

可以找到applicationContext.xml!我检查了它

System.out.println("Is null: " + (getClassLoader().getResourceAsStream("applicationContext.xml") == null));

这对我来说返回 false!

我使用以下代码加载 applicationContext.xml:

context = new ClassPathXmlApplicationContext("applicationContext.xml");

我不知道为什么会这样。 我读到的所有关于这个问题的东西都不起作用。

我的项目: CLICK TO OPEN THE IMAGE

一个小清单: - src/main/resource 在我的类路径中 - applicationContext.xml 在 src/main/resource 文件夹中 - 使用 maven mvn 构建我的项目:clean package - 使用 maven-shade-plugin 包含依赖项

希望任何人都可以帮助我。谢谢

【问题讨论】:

  • 应该是getClassLoader().getResourceAsStream("/applicationContext.xml")。注意斜线。
  • 比输出为真
  • 成功了,恭喜!
  • 没有? :D 当输出为真时,则找不到文件
  • 编辑:当我使用 public static void main(...) 然后它工作。那么问题就出现了,我用它作为插件吗?

标签: spring maven classpath maven-shade-plugin


【解决方案1】:

我解决了这个问题! 因为我的项目是一个插件,所以我必须定义另一个类加载器。所以我创建了这个启动方法:

public class SpringBootstrap {

private final ClassLoader classLoader = SpringBootstrap.class.getClassLoader();

public ApplicationContext startupSpring() {
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml") {
        protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
            super.initBeanDefinitionReader(reader);
            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
            reader.setBeanClassLoader(classLoader);
            setClassLoader(classLoader);
        }
    };
    return context;
}

}

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 2014-10-20
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2010-12-08
    • 2011-12-15
    相关资源
    最近更新 更多