Spring无法找到类路径资源

时间:2016-03-02 11:56:53

标签: spring maven classpath maven-shade-plugin

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

我使用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文件夹中,也位于我的classpath中。我用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));

这对我来说是假的!

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

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

我不知道为什么会这样。 我读到的有关这些问题的一切都没有用。

我的项目: CLICK TO OPEN THE IMAGE

一个小清单:   - src / main / resource在我的classpath中   - applicationContext.xml位于src / main / resource文件夹中   - 用maven mvn:clean包构建我的proect   - 使用maven-shade-plugin包含dependencys

希望有人能帮助我。谢谢

1 个答案:

答案 0 :(得分:0)

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

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;
}

}