2016-03-02 60 views
0

我多次搜索了我的问题。其中有些是好的,但不是我的问题的解决方案。从现在开始,我花了几个小时来解决我的问题。春季找不到课程路径资源

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

现在让我解释一下我的问题: 当我尝试启动我的插件(FYI:推杆入主程序/ 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 /主/资源在我的类路径 - applicationContext.xml的是在src /主/资源文件夹 - 构建我proect使用Maven MVN :清洁包 - 使用maven-shade-plugin包含依赖项

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

+0

应该是'getClassLoader()的getResourceAsStream( “/ applicationContext.xml中”)'。注意斜杠。 – Tunaki

+0

比输出结果是真的 –

+0

所以它的工作,祝贺! – Tunaki

回答

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

}