Spring AOP(注解方式)
2016-02-15 07:31
549 查看
配置文件: xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
<aop:aspectj-autoproxy/>
切面注解:@Aspect
定义切入点:@Pointcut
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
<aop:aspectj-autoproxy/>
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans>
切面注解:@Aspect
定义切入点:@Pointcut
package cn.itcast.service; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; /** * 切面 * */ @Aspect public class MyInterceptor { @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))") private void anyMethod() {}//声明一个切入点 @Before("anyMethod() && args(name)") public void doAccessCheck(String name) { System.out.println("前置通知:"+ name); } @AfterReturning(pointcut="anyMethod()",returning="result") public void doAfterReturning(String result) { System.out.println("后置通知:"+ result); } @After("anyMethod()") public void doAfter() { System.out.println("最终通知"); } @AfterThrowing(pointcut="anyMethod()",throwing="e") public void doAfterThrowing(Exception e) { System.out.println("例外通知:"+ e); } @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { //if(){//判断用户是否在权限 System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); //} return result; } }
相关文章推荐
- Java中使用SQL标签库<sql:setDataSource />设置数据源
- Java中运用数组的四种排序方法
- 为什么不在spring容器管理controller
- Spring MVC 中 HandlerInterceptorAdapter的使用
- 算法提高 日期计算 蓝桥杯java详解
- 【Java】JDBC
- 如何搭建一个SpringMVC的开发环境(非注解)
- 如何解决Java.lang.NoClassDefFoundError--第一部分
- java.lang.IncompatibleClassChangeError
- java中字符串的替换
- Java JDBC学习笔记
- [JavaWeb基础] 032.第三方插件pinyin4j的使用
- 为什么java读取sqlserver2008中的date数据类型的数据比实际的少2天?
- Chapter1 基本Web应用程序
- struts2学习笔记--使用Validator校验数据
- Eclipse中10个最有用的快捷键组合
- Android+struts上传文件时upload.parseRequest(request)为空
- JAVA中int、String的类型转换
- [015]Java编程思想——多态
- SSH实战项目——在线商品拍卖网