spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)
首先截取了网上的一张配置execution的图片

我在项目中关于aop的配置:如果拦截controller的方法,需要在spring-mvc.xml文件中加入(如果在spring.xml中加入则无法拦截controller层方法)
使用 and not 来排除对某些方法的拦截
<!--let spring know to use cglib not jdk -->
<aop:aspectj-autoproxy proxy-target-class="true" /> <bean id="loginAOP" class="com.shop.aop.LoginAOP" />
<aop:config>
<aop:aspect id="aspect" ref="loginAOP">
<aop:pointcut expression="execution(* com.shop.controller..*.*(..)) and not execution(* com.shop.controller.LoginController.*(..))"
id="controller" />
<aop:before method="beforeExec" pointcut-ref="controller" />
</aop:aspect>
</aop:config>
<!--let spring know to use cglib not jdk有了这个Spring就能够自动扫描被@Aspect标注的切面了 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
其实普通的xml配置不需要上面这行(如果使用基于AspectJ的就需要)
<bean id="loginAOP" class="com.shop.aop.LoginAOP" />
<aop:config>
<aop:aspect id="aspect" ref="loginAOP">
<aop:pointcut expression="execution(* com.shop.controller..*.*(..))"
id="controller" />
<aop:before method="beforeExec" pointcut-ref="controller" />
</aop:aspect>
</aop:config>
下面的是截取自 http://blog.csdn.net/udbnny/article/details/5870076 的一部分(博主没有说能不能转)。大神就是大神,轻松的写出了springAOP实现方法。完全懂了怎么用了。
一种方式是使用AspectJ提供的注解: package test.mine.spring.bean; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SleepHelper { public SleepHelper(){ } @Pointcut("execution(* *.sleep())")
public void sleeppoint(){} @Before("sleeppoint()")
public void beforeSleep(){
System.out.println("睡觉前要脱衣服!");
} @AfterReturning("sleeppoint()")
public void afterSleep(){
System.out.println("睡醒了要穿衣服!");
} } 用@Aspect的注解来标识切面,注意不要把它漏了,否则Spring创建代理的时候会找不到它,@Pointcut注解指定了切点,@Before和@AfterReturning指定了运行时的通知,注 意的是要在注解中传入切点的名称 然后我们在Spring配置文件上下点功夫,首先是增加AOP的XML命名空间和声明相关schema
命名空间:
xmlns:aop="http://www.springframework.org/schema/aop"
schema声明:
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 然后加上这个标签:
<aop:aspectj-autoproxy/> 有了这个Spring就能够自动扫描被@Aspect标注的切面了 最后是运行,很简单方便了:
public class Test { public static void main(String[] args){
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
Sleepable human = (Sleepable)appCtx.getBean("human");
human.sleep();
}
} 下面我们来看最后一种常用的实现AOP的方式:使用Spring来定义纯粹的POJO切面 前面我们用到了<aop:aspectj-autoproxy/>标签,Spring在aop的命名空间里面还提供了其他的配置元素:
<aop:advisor> 定义一个AOP通知者
<aop:after> 后通知
<aop:after-returning> 返回后通知
<aop:after-throwing> 抛出后通知
<aop:around> 周围通知
<aop:aspect>定义一个切面
<aop:before>前通知
<aop:config>顶级配置元素,类似于<beans>这种东西
<aop:pointcut>定义一个切点 我们用AOP标签来实现睡觉这个过程:
代码不变,只是修改配置文件,加入AOP配置即可:
<aop:config>
<aop:aspect ref="sleepHelper">
<aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
<aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
</aop:aspect>
</aop:config>
spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)的更多相关文章
- 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制
权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...
- Spring AOP无法拦截Controller中的方法
想使用AOP Annotation配置Spring MVC的Controller进行拦截, 发现无法拦截Controller的方法, 却可以拦截Service层的方法. 一开始: Spring的配置文 ...
- Spring的Aspect切面类不能拦截Controller中的方法
根本原因在于<aop:aspectj-autoproxy />这句话是在spring的配置文件内,还是在springmvc的配置文件内.如果是在spring的配置文件内,则@Control ...
- MVC中一个Form多个submit在controller中如何区分提交的是那个submit(如:登陆和注册)
1. 用Html.BeginForm(ActionName,ControllerName,Post)来实现controller-action的路由, 2. Form里的每个input的name值统一, ...
- ASP.NET Core MVC中的IActionFilter.OnActionExecuted方法执行时,Controller中Action返回的对象是否已经输出到Http Response中
我们在ASP.NET Core MVC项目中有如下HomeController: using Microsoft.AspNetCore.Mvc; namespace AspNetCoreActionF ...
- SpringMVC的Controller中使用线程安全的初始化
因为SpringMVC的Controller默认是单例, 在这种情况下, Controller中使用的私有变量必须也是单例, 例如各种service, 否则会有多线程访问数据互相修改的问题. 对于需要 ...
- (4)Maven快速入门_4在Spring+SpringMVC+MyBatis+Oracle+Maven框架整合运行在Tomcat8中
利用Maven 创建Spring+SpringMVC+MyBatis+Oracle 项目 分了三个项目 Dao (jar) Service (jar) Controller (web) ...
- 解决SpringMVC拦截器中Request数据只能读取一次的问题
解决SpringMVC拦截器中Request数据只能读取一次的问题 开发项目中,经常会直接在request中取数据,如Json数据,也经常用到@RequestBody注解,也可以直接通过request ...
- SpringMVC Controller中注入Request成员域和在方法中定义中HttpServletRequest有啥区别
先说结论,在Controller中注入Request是线程安全的. 以下是解释: 我们先来看看这两者有什么不同 在controller注入成员变量request 可以看到注入的是一个代理对象 写在方法 ...
随机推荐
- Tempdb--monitoring and troubleshooting
TempDB用来存放临时表.全局临时表.表变量以及排序或HASH等操作引发的中间结果集 TempDB在每次实例重启时重新创建,TempDB数据库文件的初始化大小取决于Model数据库的文件大小或显示A ...
- .Net WebApi部署问题
在IIS上部署web api 完成后,浏览时出现了“The compiler failed with error code -2146232576.”的错误(有时会出现这个情况).主要是 我们在.Ne ...
- 解决ie img标签内存泄漏
代码: <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- HBase - 数据写入流程解析
本文由 网易云发布. 作者:范欣欣 本篇文章仅限内部分享,如需转载,请联系网易获取授权. 众所周知,HBase默认适用于写多读少的应用,正是依赖于它相当出色的写入性能:一个100台RS的集群可以轻松 ...
- Hello world(世界,你好)
每一个学习语言的小白,第一行代码就是向世界问好,话不多,大圣要开始自己的代码人生了! 一.字符串是可以相加的,比如:print("hello"+"world") ...
- 486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...
- 微信小程序获取当前经纬度并逆解析地址代码
功能如标题. map.wxml代码如下: <!--miniprogram/pages/map/map.wxml--> <view><text>经度{{jd}}< ...
- [arc079f] Namori Grundy 分类讨论
Description 给给全有一个NN个点NN条边的有向图,点的的编号从11到NN 给给全的图有NN条边,形如:(p1,1),(p2,2),...,(pN,N)(p1,1),(p2,2),...,( ...
- webpack 安装
先安装node ----去nodejs官网上下载最新稳定版 一般node上面会有npm,所以选择npm安装webpack 命令:npm install webpack -g 有时候输入该命令会失败,一 ...
- oracle常用cmd命令
登陆 sqlplus username/password; 切换: conn username/password; 显示当前登陆用户: show user; 查看用户列表 select usernam ...