使用mvc:annotation-driven时遇到的No bean named 'cacheManager' is defined问题

在我打算运行一个用maven框架写好的一个Hello World的时候,但却始终报org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined和org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined

上述错误,然而我在项目里并没有用到cacheManager(缓存处理器)这个东西,所以百度了一会到Stack Overflow发现,原来是在使用tx命名空间时,idea默认会引入cache,所以突然想到我代码里的错误是我在导入mvn命名空间时的错误,然后我就去spring的配置文件里看,可没发现任何问题,又去百度mvc:annotation-driven这个东西,从拼写到使用都没有错误,可把我愁坏了,又无意中想看看他的xmlns:mvc地址和 xsi:schemaLocation地址,发现IDEA编译器自动生成的竟然是错的,一口老血喷了出来。

 

这个是自动生成的错误地址

xmlns:mvc="http://www.springframework.org/schema/cache"

xsi:schemaLocation=" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "

 然后改为正确的地址是:

xmlns:mvc="http://www.springframework.org/schema/mvn"

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

之后,项目顺利进行!

顺便说一下从网上看到比较易懂的关于mvc:annotation-driven的介绍:

如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,所有当有请求时候都没有匹配的处理请求类,就都去<mvc:default-servlet-handler/>即default servlet处理了。添加上<mvc:annotation-driven/>后,相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。总之没有相应的Controller就会被default servlet处理就ok了。

 

猜你喜欢

转载自blog.csdn.net/qq_37400312/article/details/75261605
今日推荐