使用Idea構建springmvc框架,出現no bean named 'cacheManager' is defined 錯誤

ripley_csdn發表於2017-09-25

使用Idea構建springmvc框架,出現no bean named ‘cacheManager’ is defined 錯誤。

不可否認IDEA確實是最好的java開發軟體,各種提示智慧的有點非人類,整合的git也很強大,git命令幾乎都不需要敲啦,介面也比較美觀。。。:

但也是由於提示太多啦也導致一堆的問題:
下面是我構建springmvc的步驟:其中就是由於一個不注意,產生一大波錯誤困擾我好久好久……

  1. 首先建立一個maven專案,在pom檔案中新增依賴的jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>me.seaOf</groupId>

  <artifactId>spring-mvc</artifactId>

  <packaging>war</packaging>

  <version>1.0-SNAPSHOT</version>

  <name>spring-mvc Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <dependencies>

    <!--Junit4測試包-->

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.12</version>

      <scope>test</scope>

    </dependency>



    <!--spring-ioc&di start-->

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-core</artifactId>

        <version>3.2.3.RELEASE</version>

      </dependency>

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-beans</artifactId>

        <version>3.2.3.RELEASE</version>

      </dependency>

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-context</artifactId>

        <version>3.2.3.RELEASE</version>

      </dependency>

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-context-support</artifactId>

        <version>3.2.3.RELEASE</version>

      </dependency>

      <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>spring-expression</artifactId>

        <version>3.2.3.RELEASE</version>

      </dependency>

    <!--spring-ioc&di end-->

    <!--spring AOP start-->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aop</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aspects</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

      <dependency>

        <groupId>aopalliance</groupId>

        <artifactId>aopalliance</artifactId>

        <version>1.0</version>

      </dependency>

      <dependency>

        <groupId>org.aspectj</groupId>

        <artifactId>aspectjrt</artifactId>

        <version>1.7.4</version>

      </dependency>

      <dependency>

        <groupId>org.aspectj</groupId>

        <artifactId>aspectjweaver</artifactId>

        <version>1.7.4</version>

      </dependency>

    <!--spring AOP end-->

    <!--sping jdbc整合 start-->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-jdbc</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-test</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

    <!-- spring 事務控制 -->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-tx</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

    <!--sping jdbc整合 end-->

    <!--spring -mvc start-->

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-web</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-webmvc</artifactId>

      <version>3.2.3.RELEASE</version>

    </dependency>



    <!--spring -mvc end-->

    <dependency>

      <groupId>commons-io</groupId>

      <artifactId>commons-io</artifactId>

      <version>2.0.1</version>

    </dependency>

    <dependency>

      <groupId>commons-logging</groupId>

      <artifactId>commons-logging</artifactId>

      <version>1.0.4</version>

    </dependency>

    <dependency>

      <groupId>commons-fileupload</groupId>

      <artifactId>commons-fileupload</artifactId>

      <version>1.2.2</version>



    </dependency>

    <dependency>

      <groupId>org.hibernate</groupId>

      <artifactId>hibernate-validator</artifactId>

      <version>4.1.0.Final</version>

    </dependency>

    <dependency>

      <groupId>org.codehaus.jackson</groupId>

      <artifactId>jackson-core-asl</artifactId>

      <version>1.9.13-atlassian-2</version>

      <type>pom</type>

    </dependency>

    <dependency>

      <groupId>org.codehaus.jackson</groupId>

      <artifactId>jackson-mapper-asl</artifactId>

      <version>1.9.13</version>

    </dependency>

    <dependency>

      <groupId>javax.servlet.jsp.jstl</groupId>

      <artifactId>jstl-api</artifactId>

      <version>1.2</version>

    </dependency>

    <dependency>

      <groupId>org.glassfish.web</groupId>

      <artifactId>jstl-impl</artifactId>

      <version>1.2</version>

    </dependency>

    <dependency>

      <groupId>org.slf4j</groupId>

      <artifactId>slf4j-api</artifactId>

      <version>1.5.8</version>

    </dependency>

    <dependency>

groupId>javax.inject/groupId>

      <artifactId>com.springsource.javax.inject</artifactId>

      <version>1.0.0</version>

    </dependency>

  </dependencies>

  <build>

    <finalName>spring-mvc</finalName>

  </build>

這個是所以的依賴,我這主要是出錯啦 為防止意外所以
以相關的包都匯入啦。
2. 配置springmvc-servlet.xml檔案,配置好包掃描,mvc註解,及檢視解釋配置
注意用黑框標註出來的部分
由於IDEA的自動補全功能非常強大,當你配置 <mvc:annotation-driven/>
後編譯器會幫你自動補全上面兩個配置檔案約束。這個時候如果你沒注意的就會爆出一個很莫名奇妙的錯:這裡寫圖片描述
- 上面主要爆這個錯:九月 25, 2017 8:40:23 上午 org.apache.catalina.core.ApplicationContext log
資訊: Initializing Spring FrameworkServlet ‘springmvc’
九月 25, 2017 8:40:23 上午 org.apache.catalina.core.ApplicationContext log
嚴重: StandardWrapper.Throwable
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
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329).getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)。

報錯原因是:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘cacheManager’ is defined

這裡主要是因為上面springmvc-servlet.xml配置檔案中由於IDEA自動配置啦cache,但配置檔案中又沒有指定快取的空間,JVM虛擬機器中也有個cacheManage,這就導致spring進行bean管理時掃描到兩個cache Manage 導致都不能正常載入,爆錯,而你在配置時明明沒有使用到快取,卻老是報一個快取的錯誤,實在很懵逼。

  1. 解決辦法
    這裡寫圖片描述

  2. 正確的配置檔案是這個樣子的


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="controller"/>
    <mvc:annotation-driven/>
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

相關文章