spring采用注解方式实现AOP
来源:互联网 发布:淘宝导航条怎么做 编辑:程序博客网 时间:2024/05/22 01:52
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;
/**
* 必须交给spring管理
* 注解方式:@Component
* 配置方式:<bean id="myInterceptor" class="com.saly.service.MyInterceptor">
* @author saly
*
*/
@Aspect
public class MyInterceptor {
/*
* @Pointcut("execution (* com.saly.service..*.*.(..))")
* 第一个※代表返回类型(任何类型),
* 第一个..对子包进行拦截,
* 第二个※对哪个类拦截(对所有类拦截),
* 第三个※代表所有方法,括号中的两个..代表参数任意
*
*/
@Pointcut("execution (* com.saly.service.impl.PersonServiceBean.*(..))")//对所有方法进行拦截,参数任意
private void anyMethod(){}//声明一个切入点
@Before("anyMethod() && args(name)")//切入点的名称
public void doAccessCheck(String name){//只能拦截到参数为一个切是string类型的方法
System.out.println("前置通知"+name);
}
@AfterReturning(pointcut="anyMethod()",returning="result")//只能拦截返回值是string类型的方法
public void doAccessCheckAfter(String result){
System.out.println("后置通知"+result);
}
@After("anyMethod()")
public void doAccessCheckFinally(){
System.out.println("最终通知");
}
@AfterThrowing(pointcut="anyMethod()",throwing="e")//列外信息作为参数传入到doAfterThrowing方法中
public void doAfterThrowing(Exception e){
System.out.println("例外通知"+e);
}
//环绕通知,struts2做权限控制//可以替换以上其他方法
@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
//if(){//判断用户是否在权限
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
//}
return result;
}
}
辅助类
public interface PersonService {
public String getPersonName(Integer id);
public void save(String name);
public void update(String name,Integer id);
}
public String getPersonName(Integer id) {
System.out.println("我是getPersonName()方法");
return "salyfan";
}
public void save(String name) {
throw new RuntimeException("例外异常");
//System.out.println("我是save()方法");
}
public void update(String name, Integer id) {
System.out.println("我是update()方法");
}
}
<?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="com.saly.service.MyInterceptor"/>
<bean id="personService" class="com.saly.service.impl.PersonServiceBean"/>
</beans>
- spring采用注解方式实现AOP
- Spring-注解方式实现AOP
- 注解方式实现spring-aop
- spring 注解方式实现aop
- Spring 注解方式实现AOP
- Spring---->采用Annotation的方式实现AOP
- Spring---->采用静态配置文件方式实现AOP
- 采用Spring注解方式, 实现事务
- Spring采用注解使用AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- Spring使用注解的方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用Spring的注解方式实现AOP
- 使用spring注解方式实现AOP(一)
- newlisp 使用crypto模块
- freeChart设置字体
- 在.NET使用JSON作为数据交换格式
- TSE源码中MD5代码分析(1)
- Linux内核3.7 TCP Fast Open验证实例
- spring采用注解方式实现AOP
- 邮票面值,求最大的连续区间
- linux 动态库的生成与使用
- Linux安装JDK1.7
- watir中如何获取dom属性?
- Asp.net Post Sample
- 说说watir-webdriver那些事(三)如何定位页面元素
- Java判断文件是否完整
- dumpbin的几个命令