在springMVC中使用AOP拦截controller

SpringMVC+AOP注意点

  • 使用spring aop 注意事项

1)jar包是否完整:aopalliance-1.0.jar、asm-3.3.1.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar

2)在spring容器初始化配置xml(这个配置文件与springmvc的配置xml要区别开来)中加入aop的命名空间

        xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/aop

            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"

3)如果使用注解那么就得开启 扫描<context:component-scan base-package="com.ly.controller">

  使用aop必须在xml中配置开启代理<aop:aspectj-autoproxy proxy-target-class="true"/>

+++++++++++++++++++++++++++++下面是完整的spring-mvc.xml配置++++++++++++++++++++++++++++++++++++++++++
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd

   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-4.1.xsd">
<context:component-scan base-package="com.ly"/>
<!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
====================================================================================================================================


spring mvc 在controller层定义切面 拦截 注意事项。(在mvc的xml文件中必需定义开启<aop:aspectj-autoproxy>  cglib代理,因为controller被jdk代理了,不是通过cglib的字节码来完成的)

至于为何在mvc的xml中配置:原因如下
   在  web.xml 中 springMVC的核心分发器是在web容器启动时就初始化的,所以在spring-mvc.xml中也要单独配置<aop:aspectj-autoproxy proxy-target-class="true"/>
    <servlet>
          <servlet-name>DispatcherServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value> WEB-INF/config/spring-mvc.xml</param-value>  <!--通过mvc的配置文件来初始化springmvc的核心请求分发器-->
          </init-param>
          <load-on-startup>1</load-on-startup>
     </servlet>
     <!-- URL映射 -->
     <servlet-mapping>
         <servlet-name>DispatcherServlet</servlet-name>
         <url-pattern>/</url-pattern>
     </servlet-mapping>
+++++++++++++++++++++++++++++下面是完整的spring-mvc.xml配置++++++++++++++++++++++++++++++++++++++++++
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop  
              http://www.springframework.org/schema/aop/spring-aop-4.1.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="com.ly.controller">
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                   <property name="supportedMediaTypes">
                       <list>
                           <value>text/html; charset=UTF-8</value>
                           <value>application/json;charset=UTF-8</value>
                       </list>
                   </property>
            </bean>
</mvc:message-converters>
</mvc:annotation-driven>

<!--视图名称解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
 p:order="100" p:viewClass="org.springframework.web.servlet.view.JstlView"
 p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
</beans>

====================================================================================================================================

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值