Spring+Spring MVC+Mybatis整合配置AOP不生效的解决方案以及Bean初始化重复加载两次(疑难杂症)...

之前上班做spring+spring mvc +hibernate开发, 2年之久不做想复习一下aop的使用,结果配置遇见aop不生效,解决而记录!

 

先上代码直接看反例效果会明显:

首先看一下我的代码的包路径:

接下来看Spring-MVC的配置文件部分代码:

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4.  
    xmlns:context= "http://www.springframework.org/schema/context"
  5.  
    xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:tx= "http://www.springframework.org/schema/tx"
  6.  
    xmlns:aop= "http://www.springframework.org/schema/aop"
  7.  
    xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9.  
    http://www.springframework.org/schema/aop
  10.  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11.  
    http://www.springframework.org/schema/context
  12.  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  15.  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
  16.  
     
  17.  
    <context:component-scan base-package="cn.edu.neu" />
  18.  
     
  19.  
    ...............//省略其他配置
  20.  
     
  21.  
    </beans>

 

 

在看一下Spring的配置文件:

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4.  
    xmlns:context= "http://www.springframework.org/schema/context"
  5.  
    xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:tx= "http://www.springframework.org/schema/tx"
  6.  
    xmlns:aop= "http://www.springframework.org/schema/aop"
  7.  
    xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9.  
    http://www.springframework.org/schema/aop
  10.  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11.  
    http://www.springframework.org/schema/context
  12.  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  15.  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
  16.  
     
  17.  
     
  18.  
    <!-- 自动扫描
  19.  
    <context:component-scan base-package="cn.edu.neu" />
  20.  
    <!-- aop 日志管理 -->
  21.  
    <aop:aspectj-autoproxy expose-proxy="true" />
  22.  
    <aop:config proxy-target-class="true">
  23.  
    <aop:aspect id="aspectId" ref="aopLog"> <!--调用日志类 -->
  24.  
    <!-- cn.edu.neu.service.impl.UserServiceImpl -->
  25.  
    <aop:pointcut id="log"
  26.  
    expression= "execution(* cn.edu.neu.service.*.*(..))" /> <!--配置在log包下所有的类在调用之前都会被拦截 -->
  27.  
    <aop:before method="before" pointcut-ref="log"/> <!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法 -->
  28.  
    <aop:after method="after" pointcut-ref="log"/>
  29.  
    <!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法 -->
  30.  
    </aop:aspect>
  31.  
    </aop:config>
  32.  
     
  33.  
    </beans>


web.xml配置文件如下:

 

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.  
    xmlns= "http://java.sun.com/xml/ns/javaee"
  4.  
    xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5.  
    version= "3.0">
  6.  
    <display-name>Archetype Created Web Application </display-name>
  7.  
    <!-- 配置资源的加载顺序:context-param -> listener -> filter -> servlet -->
  8.  
     
  9.  
    <!-- Spring和mybatis的配置文件 -->
  10.  
    <context-param>
  11.  
    <param-name>contextConfigLocation </param-name>
  12.  
    <param-value>classpath:spring-mybatis.xml </param-value>
  13.  
    </context-param>
  14.  
     
  15.  
     
  16.  
    <!-- Spring监听器 -->
  17.  
    <listener>
  18.  
    <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
  19.  
    </listener>
  20.  
     
  21.  
     
  22.  
    <!-- 编码过滤器 -->
  23.  
    <filter>
  24.  
    <filter-name>encodingFilter </filter-name>
  25.  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
  26.  
    <async-supported>true </async-supported>
  27.  
    <init-param>
  28.  
    <param-name>encoding </param-name>
  29.  
    <param-value>UTF-8 </param-value>
  30.  
    </init-param>
  31.  
    </filter>
  32.  
    <filter-mapping>
  33.  
    <filter-name>encodingFilter </filter-name>
  34.  
    <url-pattern>/* </url-pattern>
  35.  
    </filter-mapping>
  36.  
     
  37.  
     
  38.  
    <!-- Spring MVC servlet -->
  39.  
    <servlet>
  40.  
    <servlet-name>SpringMVC </servlet-name>
  41.  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  42.  
    <!-- -->
  43.  
    <init-param>
  44.  
    <param-name>contextConfigLocation </param-name>
  45.  
    <param-value>classpath:spring-mvc.xml </param-value>
  46.  
    </init-param>
  47.  
    <load-on-startup>1 </load-on-startup>
  48.  
    <async-supported>true </async-supported>
  49.  
    </servlet>
  50.  
    <servlet-mapping>
  51.  
    <servlet-name>SpringMVC </servlet-name>
  52.  
    <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
  53.  
    <url-pattern>/ </url-pattern>
  54.  
    </servlet-mapping>
  55.  
     
  56.  
    <welcome-file-list>
  57.  
    <welcome-file>/index.jsp </welcome-file>
  58.  
    </welcome-file-list>
  59.  
     
  60.  
    </web-app>



 

以上的配置之后以为大功告成,结果一运行,恩?不好使?,接下来分析一下原因:

 

我们知道web.xml中加载顺序是:context-param -> listener -> filter -> servlet ,所以在加载完参数之后开始启动spring的监听器,监听器加载完成之后,就完成了bean加载到容器的过程。但是你可能到现在还没有注意的是,在上面springmvc和spring的配置文件都配置了<context:component-scan base-package="cn.edu.neu" />,所以导致在Spring的DispatcherServlet启动过程中又一次把springmvc.xml扫描到的组件(就是类)再一次添加到spring的容器中,此次会覆盖前面加载的,所以这个就是为什么spring aop不生效的原因!!!

 

到此是不是还是有点不明白呢?那就在具体说一下:因为在第一次spring加载组件的时候会给pointcut对象生成代理(aop实现原始是动态代理),放到容器中。但是第二次加载时候生成的对象没有代理,第二次覆盖了第一次存在代理的对象,因此导致aop不生效!

 

 

接下来说一下解决方案:

方案1:将spring和spring mvc的配置文件放到一个配置文件中,即可避免两次加载覆盖。

 

方案2:如果非要两个文件都配置扫描的代码的话,那就把需要生成代理的包路径在spring中配置扫描,确保spring mvc的扫描路径不包括“需要生成代理的路径”即可。(而spring mvc的controller组件扫描必要要在spring mvc中配置)

 

例如方案2代码如下:

 

spring的配置文件:

扫描非controller路径

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4.  
    xmlns:context= "http://www.springframework.org/schema/context"
  5.  
    xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:tx= "http://www.springframework.org/schema/tx"
  6.  
    xmlns:aop= "http://www.springframework.org/schema/aop"
  7.  
    xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9.  
    http://www.springframework.org/schema/aop
  10.  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11.  
    http://www.springframework.org/schema/context
  12.  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  15.  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
  16.  
     
  17.  
     
  18.  
     
  19.  
    <!-- 自动扫描 -->
  20.  
    <context:component-scan base-package="cn.edu.neu.dao,cn.edu.neu.service,cn.edu.neu.log" />
  21.  
     
  22.  
     
  23.  
     
  24.  
    <!-- aop 日志管理 -->
  25.  
    <aop:aspectj-autoproxy expose-proxy="true" />
  26.  
    <aop:config proxy-target-class="true">
  27.  
    <aop:aspect id="aspectId" ref="aopLog"> <!--调用日志类 -->
  28.  
    <!-- cn.edu.neu.service.impl.UserServiceImpl -->
  29.  
    <aop:pointcut id="log"
  30.  
    expression= "execution(* cn.edu.neu.service.*.*(..))" /> <!--配置在log包下所有的类在调用之前都会被拦截 -->
  31.  
     
  32.  
    <aop:before method="before" pointcut-ref="log"/> <!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法 -->
  33.  
    <aop:after method="after" pointcut-ref="log"/>
  34.  
    <!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法 -->
  35.  
    </aop:aspect>
  36.  
     
  37.  
    </aop:config>
  38.  
     
  39.  
    </beans>



 

spring-mvc配置文件:

spring mvc配置文件只负责扫描contrller的组件

 

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
  4.  
    xmlns:context= "http://www.springframework.org/schema/context"
  5.  
    xmlns:mvc= "http://www.springframework.org/schema/mvc" xmlns:tx= "http://www.springframework.org/schema/tx"
  6.  
    xmlns:aop= "http://www.springframework.org/schema/aop"
  7.  
    xsi:schemaLocation= "http://www.springframework.org/schema/beans
  8.  
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  9.  
    http://www.springframework.org/schema/aop
  10.  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11.  
    http://www.springframework.org/schema/context
  12.  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13.  
    http://www.springframework.org/schema/tx
  14.  
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  15.  
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
  16.  
     
  17.  
     
  18.  
     
  19.  
    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
  20.  
    <context:component-scan base-package="cn.edu.neu.controller" />
  21.  
    </beans>

 

 

 

核心结论:就是需要生成代理的类一定不要被重复覆盖加载(Spring bean的加载支持覆盖),否则就会aop不起作用!

 

补充:

你通过配置方案1或者方案2之后,你在eclipse中启动tomcat可能会发现还是加载两次!!!这个问题坑了我一上午,记录一下自己的心得吧!

得到下面结论的运行环境是:

1:Tomcat部署目录默认的webapps,而且webapps中部署了一个该验证的项目。

2:eclipse版本:Version: Mars.1 Release (4.5.1),tomcat版本:apache-tomcat-7.0.53-windows-x64

 

结论先行:

 

1:从新解压一个全新的tomcat,然后将项目放到webapps中启动发现只会加载一次,这是就可以确定原因了就是原来tomcat或eclipse的问题!接下来看如下分析:

 

2:只要你的tomcat配置过eclipse的话,当你使用eclipse启动tomcat之后,会在tomcat的server.xml中生成如下代码:

 

 <Context docBase="C:\install\tmp\apache-tomcat-7.0.53-windows-x641\apache-tomcat-7.0.53\webapps\springaop" path="/neuweb" reloadable="true" source="org.eclipse.jst.jee.server:springaop"/>

 

从此之后无论你是使用eclipse启动tomcat还是bin/startup.bat启动tomcat都会加载两次bean!(Context docBase只要给项目起别名了的话就会加载两次,但是如果当path的名字和docBase的最后的名字一样的话就不会触发加载两次了,一样的话其实就是起了一个和原来名字一样的别名与没起别名是一样的。)

 

即如下不如加载两次:

 

<Context docBase="C:\install\tmp\apache-tomcat-7.0.53-windows-x641\apache-tomcat-7.0.53\webapps\springaop" path="/springaop" reloadable="true" source="org.eclipse.jst.jee.server:springaop"/>

所以到此就可以确定原因了:就是因为项目有别名访问导致最终实现两次加载!

 

 

解决办法:删除Context代码之后,使用tomcat脚本启动tomcat,这时候bean就会加载一次!(如果使用eclipse启动tomcat的话还是会生成context,还是加载两次)

 

context docbase作用:就是你给的项目起一个访问的别名,例如:你有一个叫appweb的项目,这时候就可以使用localhost:8080/appweb访问该项目了,但是如果你还想使用localhost:8080/website路径访问appweb的话,就需要使用context docbase配置。具体配置不细说了,自行找资料学习!

 

 

 

 

 

本文转载:https://blog.csdn.net/dax1n/article/details/77131548

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值