今天使用spring mvc搭建了一个简单的demo,但是发现spring容器始终不创建bean,控制台也不打印任何错误,检查了许久的配置文件都没有发现错误。
然后我在springmvc-servlet.xml中又加入了一个拦截器,开始报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1': Cannot create inner bean 'com.youyuan.interceptors.AuthInterceptor#0' of type [com.youyuan.interceptors.AuthInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.youyuan.interceptors.AuthInterceptor] for bean with name 'com.youyuan.interceptors.AuthInterceptor#0' defined in ServletContext resource [/WEB-INF/springmvc-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.youyuan.interceptors.AuthInterceptor at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 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)
于是我意识到容器找不到class文件,重新检查了一下我的class路径,发现是放在了
demo/WebRoot/classes而tomcat插件默认检查的class路径是/WebRoot/WEB-INF/classes
eclipse默认是放在build/classes,我之前曾经修改过这个路径,但是中间少写了WEB-INF而我并没有发现这也造成我没有想到是class文件路径造成的问题,结果为这个问题检查了一天,做下记录,引以为戒!