eclipse Springframework 无法解析控制器上的配置类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30888454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:42:47  来源:igfitidea点击:

Springframework Failed to parse configuration class on a controller

javaeclipsespringmavenspring-mvc

提问by Mixmax

I am working on a j2ee project with eclipse, maven and spring. All is going quite fine except a weird and annoying behavior i can't resolve...

我正在使用 eclipse、maven 和 spring 开发一个 j2ee 项目。一切都很顺利,除了我无法解决的奇怪和烦人的行为......

Let's begin with the stacktrace :

让我们从堆栈跟踪开始:

[localhost-startStop-1] WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lttd.controller.FichiersController]; nested exception is
java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:172)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:82)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:77)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:587)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getMemberClasses(ConfigurationClassParser.java:749)
at org.springframework.context.annotation.ConfigurationClassParser.processMemberClasses(ConfigurationClassParser.java:313)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:228)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:186)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
... 18 more
[localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lttd.controller.FichiersController]; nested exception is java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:172)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:82)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:77)
    at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:587)
    at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getMemberClasses(ConfigurationClassParser.java:749)
    at org.springframework.context.annotation.ConfigurationClassParser.processMemberClasses(ConfigurationClassParser.java:313)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:245)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:228)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:186)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
    ... 18 more
juin 17, 2015 10:42:55 AM org.apache.catalina.core.StandardContext listenerStart
GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.lttd.controller.FichiersController]; nested exception is java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:172)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4760)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5184)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: class path resource [com/lttd/controller/FichiersController.class] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:82)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:77)
    at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:587)
    at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getMemberClasses(ConfigurationClassParser.java:749)
    at org.springframework.context.annotation.ConfigurationClassParser.processMemberClasses(ConfigurationClassParser.java:313)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:245)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:228)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:186)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
    ... 18 more 

After some search on google i found it can be related to a classpath problem, so there is my Build path (can't post pictures ...):

在谷歌上搜索后,我发现它可能与类路径问题有关,所以有我的构建路径(无法发布图片...):

In source folder build path : intranetLTTD/src/main/java Included : All Excluded : none

在源文件夹构建路径:intranetLTTD/src/main/java 包括:全部排除:无

and my project treeview :

和我的项目树视图:

Java Ressources - src/main/java -com.lttd.controller -FichiersController

Java 资源 - src/main/java -com.lttd.controller -FichiersController

So it don't seems to be a classpath issue.

所以它似乎不是类路径问题。

I also found on google that the "$1.class" means its an anonymous class... why not but i don't make it from my own, ( i don't even know how to make it for a controller ).

我还在谷歌上发现“$1.class”意味着它是一个匿名类......为什么不呢,但我不是从我自己的,(我什至不知道如何为控制器制作它)。

This error is happening when i make any change in my FichiersController ( like declare a variable ) but also when i make any change in a controller i created after my FichiersController, but not in the ones i created before.

当我在我的 FichiersController 中进行任何更改(例如声明一个变量)时,也会发生此错误,而且当我在我的 FichiersController 之后创建的控制器中进行任何更改时,也会发生此错误,但不会在我之前创建的控制器中发生更改。

I found a workaround to still be able to run the project ( and it's a weird workaround ) : after making a change, i have to change in one of my method of the FichiersController :

我找到了一个仍然能够运行项目的解决方法(这是一个奇怪的解决方法):进行更改后,我必须更改我的 FichiersController 方法之一:

TypeFichier fileType = TypeFichier.valueOf(fileTypeString);
switch(fileType)

to :

到 :

switch(TypeFichier.valueOf(fileTypeString))

Or reverse

或反转

switch(TypeFichier.valueOf(fileTypeString))

to

switch(fileType) 

And in my dispatcher-servlet.xml i have :

在我的 dispatcher-servlet.xml 我有:

<context:component-scan base-package="com.lttd" />

I hope i've gave all you need to help me find out whats happening, and how to resolve it. Don't hesitate to ask for further informations.

我希望我已经提供了您需要的一切帮助我找出发生了什么以及如何解决它。不要犹豫,询问更多信息。

Thanks for your help.

谢谢你的帮助。

回答by idobi

Maybe you solved it already but I just Had the same problem with strange $1.classand for me that was a problem with Eclipse's publishing so I just built my web project with maven and put manually the files into my tomcat folder.

也许你已经解决了它,但我只是遇到了同样的问题,$1.class对我来说,这是 Eclipse 发布的问题,所以我只是用 maven 构建了我的 web 项目并手动将文件放入我的 tomcat 文件夹中。

回答by Mixmax

EDIT : found the cause of the problem : <context:component-scan base-package="com.lttd" />was present in dispatcher-servlet.xml , but also in my spring-security.xml (due to the fact i was learning j2ee spring at the beginning of the project). That's why it created a FichierController$1. But i don't know why it happens with this class only.

编辑:找到问题的原因: <context:component-scan base-package="com.lttd" />存在于 dispatcher-servlet.xml 中,但也存在于我的 spring-security.xml 中(因为我在项目开始时正在学习 j2ee spring)。这就是为什么它创建了一个 FichierController$1。但我不知道为什么它只发生在这门课上。

After deleted the <context:component-scan base-package="com.lttd" />in spring-security.xml, i don't have the problem anymore.

<context:component-scan base-package="com.lttd" />在 spring-security.xml 中删除后,我不再有问题了。

EDIT: Finally this did not repaired the error, the error reappeared 3 days later...

编辑:最后这并没有修复错误,3天后错误再次出现......

Here is the method of the FichiersController i have to edit for a temporarily workaround :

这是 FichiersController 的方法,我必须编辑以临时解决方法:



    @Transactional
    @RequestMapping(value = "/admin/fileEditAjax*", method = RequestMethod.GET)
    public @ResponseBody FichierFormObject fileEditAjax(@RequestParam (value="fileName", required = true) String fileName,
                                                    @RequestParam( value="fileType", required = true)String fileTypeString){

        FichierFormObject fileFormObject = new FichierFormObject();
        TypeFichier fileType = TypeFichier.valueOf(fileTypeString);

        //If the error is triggered, i have to change this 'switch(fileType)' to TypeFichier.valueOf(fileTypeString) or reverse
        switch(fileType){
            case FICHE_CONNEXION:
            case DOCUMENT_CLIENT:
                //some processing
                break;

            case PROCEDURE:
                //some processing
                break;

            case MODULEM3:
                //some processing
                break;

            case PRODUIT_CONNEXE:
                //some processing
                break;

            default:
                break;
        }

        return fileFormObject;
    }

But i'm not sure the problem comes from my code...

但我不确定问题是否来自我的代码......

Hope this edit is the right place to add those information.

希望此编辑是添加这些信息的正确位置。