spring采用注解方式实现AOP
2013-01-24 23:55
459 查看
package com.saly.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;
/**
* 必须交给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 class PersonServiceBean implements PersonService {
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>
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 class PersonServiceBean implements PersonService {
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(1)
- 使用Spring的注解方式实现AOP
- Spring使用注解的方式实现AOP
- Spring 通过注解方式实现AOP切面编程
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- 转:Spring AOP 注解方式实现的一些“坑”
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- 使用Spring的注解方式实现AOP
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring---->采用静态配置文件方式实现AOP
- 用Spring Aop方式拦截自定义注解的实现
- java SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- Spring实现AOP方式之二:使用注解配置 Spring AOP
- 使用Spring的注解方式实现AOP
- spring 注解方式实现aop
- Spring---->采用静态配置文件方式实现AOP
- JSF2.0+Spring+Hibernate采用注解方式实现
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)