您的位置:首页 > 编程语言 > Java开发

Spring aop基于注解

2015-06-24 16:53 519 查看
***applicationContext.xml***
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<context:component-scan base-package="cn.com"></context:component-scan>
<!--启用Spring对@AspectJ的支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>


***LogServiceImp.java***
package cn.com.service;

import org.springframework.stereotype.Service;

@Service
public class LogServiceImp {
public void queryUsers() {
System.out.println(">> 查询User <<");
}
public void addUser() {
System.out.println(">> 添加一个User <<");
}
public void deleteUser() {
System.out.println(">> 删除一个User <<");
}

}


***MyAdvice.java***
package cn.com.aop;
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;
import org.springframework.stereotype.Repository;
//定义通知类
@Repository
@Aspect
public class MyAdvice {
// 在@AspectJ注解风格的AOP中 一个切入点签名通过一个普通的方法来定义
// 1,作为切入点签名的方法必须不能有参数
// 2,一般使用private void修饰,方法体为空
@Pointcut("execution(public * cn.com..*(..))")
private void myPointcut() {
}

// Before 前置通知 在方法调用前执行
// "myPointcut()" 表示前面定义的切入点
// 也可以写为@Before("execution(* cn.itcast..*(..))")
@Before("myPointcut()")
public void before() {
System.out.println("== 前置通知 ==");
}

// AfterReturning,后置通知 在一个匹配的方法返回的时候执行
// pointcut 使用的切入点
// returning 表示方法的返回值,方法无返回值时,返回值为null
@AfterReturning(pointcut = "myPointcut()", returning = "returnValue")
public void afterReturning() {
System.out.println("== 后置通知 ==");
}

// AfterThrowing 异常通知,在一个方法抛出异常后执行
// pointcut 使用的切入点
// throwing 表示抛出的异常
@AfterThrowing(pointcut = "myPointcut()", throwing = "ex")
public void throwsException() {
System.out.println("== 异常通知 ==");
}

// @After 最终通知 不论一个方法如何结束,最终通知都会执行
// 最终通知必须准备处理正常和异常两种情况,通常用它来释放资源
@After("myPointcut()")
public void after() {
System.out.println("== 最终通知 ==");
}

// @Around 环绕通知 可以控制方法的执行
// 通知的第一个参数必须是 ProceedingJoinPoint类型
// 调用ProceedingJoinPoint的proceed()方法会导致 后台的连接点方法执行
@Around("myPointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
System.out.println("== 环绕通知(前) ==");
Object result = point.proceed();
System.out.println("== 环绕通知(后) ==");
return result;
}

}


***测试类***
@Test
public void testAop() throws Exception {
ApplicationContext  app = new ClassPathXmlApplicationContext("applicationContext.xml");
LogServiceImp logService = (LogServiceImp) app.getBean("logServiceImp");
logService.addUser();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aop