spingAOP在springMVC中的使用(我用在拦截controller中的方法。主要用于登录控制)
2024-08-27 06:33:28
首先截取了网上的一张配置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>
最新文章
- sap MD04中常用函数
- pip 安装 lxml 出错
- SAP第一轮面试总结
- windows 编程中的常见bug
- iOS6 以上设置文本高度,行高(转)
- c++ primer复习(二)
- oracle4
- php拦截器(魔术方法)
- Intent的概念及应用(二)
- 课堂作业 泛型类-Bag
- (Java)微信之个人公众账号开发(二)——接收并处理用户消息(下)
- 二、redis命令简单使用(不区分大小写)
- 第2章 Java编程基础
- JSP标准标签库:JSTL
- Scala:First Steps in Scala
- 前缀和与差分之IncDec sequence
- clock()、time()、clock_gettime()和gettimeofday()函数的用法和区别
- 【上传-下载】-jmeter工具
- ORA-14402:更新分区关键字列将导致分区更改
- c++ 虚基类应用
热门文章
- django:multivaluedictkeyerror错误
- LINQ to Entities 基于方法的查询语法
- ASP.net/C#中如何调用动态链接库DLL
- D - How Many Tables (并查集)(水题)
- 【12c OCP】最新CUUG OCP-071考试题库(52题)
- 2018 OCP 052最新题库及答案-4
- Visual Studio code安装步骤
- JAVA Double去掉科学计数";E";
- spring包下载方法
- [Swift实际操作]八、实用进阶-(1)Swift语言中的两种单例模式实际操作