spring aop的注解方式:和xml的配置方式略有区别,详细如下:

1、首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法。

  1. /**
  2. *
  3. */
  4. package com.lilin.maven.service.annotationaop;
  5.  
  6. import org.aspectj.lang.ProceedingJoinPoint;
  7. import org.aspectj.lang.annotation.After;
  8. import org.aspectj.lang.annotation.AfterReturning;
  9. import org.aspectj.lang.annotation.AfterThrowing;
  10. import org.aspectj.lang.annotation.Around;
  11. import org.aspectj.lang.annotation.Aspect;
  12. import org.aspectj.lang.annotation.Before;
  13. import org.aspectj.lang.annotation.Pointcut;
  14. import org.springframework.stereotype.Service;
  15.  
  16. /**
  17. * @author lilin
  18. *
  19. */
  20. @Aspect
  21. @Service
  22. public class AspectAop {
  23.  
  24. /**
  25. * 申明切点,同时配置将要被aop过滤的业务类
  26. */
  27. @Pointcut("execution (* com.lilin.maven.service.annotationaop.UserService.addUser(..))")
  28. public void pointcut() {
  29. }
  30.  
  31. @Before("pointcut()")
  32. public void doBefore() {
  33. System.out.println("doBefore advice");
  34. }
  35.  
  36. @AfterReturning("pointcut()")
  37. public void doAfterReturning() {
  38. System.out.println("doAfterReturning advice");
  39. }
  40.  
  41. @After("pointcut()")
  42. public void doAfter() {
  43. System.out.println("doAfter advice");
  44. }
  45.  
  46. @AfterThrowing("pointcut()")
  47. public void doAfterThrowing() {
  48. System.out.println("doAfterThrowing advice");
  49. }
  50.  
  51. @Around("pointcut()")
  52. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  53. System.out.println("doAround advice start");
  54. Object result = pjp.proceed();
  55. System.out.println("doAround advice end");
  56. return result;
  57. }
  58.  
  59. }

2、在spring的配置文件中,开启注解的扫描:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd">
  11.  
  12. <!-- 配置注解扫面路径 -->
  13. <context:component-scan base-package="com.lilin" />
  14. <!-- 开启注解 -->
  15. <context:annotation-config />
  16. <!-- 开启aspectj代理 -->
  17. <aop:aspectj-autoproxy />
  18. </beans>

3、建立业务的接口和类,方便aop的过滤测试。

  1. /**
  2. *
  3. */
  4. package com.lilin.maven.service.annotationaop;
  5.  
  6. /**
  7. * @author lilin
  8. *
  9. */
  10. public interface IUserService {
  11. void addUser();
  12. }
  1. /**
  2. *
  3. */
  4. package com.lilin.maven.service.annotationaop;
  5.  
  6. import org.springframework.stereotype.Service;
  7.  
  8. /**
  9. * @author lilin
  10. *
  11. */
  12. @Service
  13. public class UserService implements IUserService {
  14. @Override
  15. public void addUser() {
  16. System.out.println("增加用户信息");
         //这个异常信息的抛出,是为了测试after throwing的advice的
  17. throw new RuntimeException("测试异常");
  18. }
  19.  
  20. }

4、还是像xml配置的时候类似,建立testNG的测试类:继承的baseTest 和xml配置的中一样,请参见上一篇xml配置中的baseTest

  1. /**
  2. *
  3. */
  4. package com.lilin.maven.service.annotationaop;
  5.  
  6. import javax.annotation.Resource;
  7.  
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.testng.annotations.Test;
  10.  
  11. import com.lilin.maven.service.BaseTest;
  12.  
  13. /**
  14. * @author lilin
  15. *
  16. */
  17. @ContextConfiguration(locations = { "classpath:/config/spring/spring-aopannotation.xml" })
  18. public class AopAnnotationTest extends BaseTest {
  19.  
  20. @Resource
  21. private IUserService userService;
  22.  
  23. @Test
  24. public void aopAnnotationTest() {
  25. userService.addUser();
  26. }
  27.  
  28. }

5、运行测试方法,可以得到注解方式的aop配置已经起到作用:

正常的测试结果如下:

  1. 2016-1-29 15:25:09 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. 信息: Loading XML bean definitions from class path resource [config/spring/spring-aopannotation.xml]
  3. 2016-1-29 15:25:09 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  4. 信息: Refreshing org.springframework.context.support.GenericApplicationContext@10f6d3: startup date [Fri Jan 29 15:25:09 CST 2016]; root of context hierarchy
  5. 2016-1-29 15:25:09 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  6. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10721b0: defining beans [aspectAop,userService,light,lightOnCommand,remoteControl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
  7. doAround advice start
  8. doBefore advice
  9. 增加用户信息
  10. doAround advice end
  11. doAfter advice
  12. doAfterReturning advice
  13. PASSED: aopAnnotationTest
  14.  
  15. ===============================================
  16. Default test
  17. Tests run: 1, Failures: 0, Skips: 0
  18. ===============================================

@AfterThrowing 的测试结果如下,测试这个,请手动在业务方法里面抛出异常信息:

  1. 2016-1-29 15:26:50 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. 信息: Loading XML bean definitions from class path resource [config/spring/spring-aopannotation.xml]
  3. 2016-1-29 15:26:50 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  4. 信息: Refreshing org.springframework.context.support.GenericApplicationContext@10f6d3: startup date [Fri Jan 29 15:26:50 CST 2016]; root of context hierarchy
  5. 2016-1-29 15:26:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  6. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10721b0: defining beans [aspectAop,userService,light,lightOnCommand,remoteControl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
  7. doAround advice start
  8. doBefore advice
  9. 增加用户信息
  10. doAfter advice
  11. doAfterThrowing advice
  12. FAILED: aopAnnotationTest
  13. java.lang.RuntimeException: 测试异常
  14. at com.lilin.maven.service.annotationaop.UserService.addUser(UserService.java:17)
  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

好了,到此,spring aop的注解方式的实现,一个简单的demo就o了。

spring aop 使用注解方式总结的更多相关文章

  1. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  2. Spring AOP的注解方式实现

    spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置.修改更加方便. 1.开启AOP的注解配置方式 <!-- 开启aop属性注解 --> <aop:a ...

  3. Spring AOP(二)--注解方式

    本文介绍通过注解@AspectJ实现Spring AOP,这里要重点说明一下这种方式实现时所需的包,因为Aspect是第三方提供的,不包含在spring中,所以不能只导入spring-aop的包,为了 ...

  4. perf4j+spring+aop 配置 注解方式

    今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...

  5. Spring AOP(注解方式)

    配置文件: xmlns:aop="http://www.springframework.org/schema/aop" http://www.springframework.org ...

  6. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  7. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  8. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  9. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

随机推荐

  1. 跨平台c++ Coroutine,仿unity3d实现

    不多说,贴代码: #include "stdafx.h" #include <list> #include <thread> #include <ch ...

  2. MSP430F149学习之路——AD

    代码一:Timer_A触发转换 #include <msp430x14x.h> void main() { WDTCTL = WDTPW + WDTHOLD; P6SEL |= BIT0; ...

  3. org.springframework.core.NestedIOException

    今天遇到的一个小异常,报错信息如下: 2013-11-13 10:00:32 org.apache.catalina.core.StandardContext listenerStart严重: Exc ...

  4. 洛谷P2735 电网 Electric Fences

    P2735 电网 Electric Fences 11通过 28提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 在本题中,格点是 ...

  5. JavaScript之数组循环 forEach 循环输出数组元素

    var arrayAll = []; arrayAll.push(1); arrayAll.push(2); arrayAll[arrayAll.length] = 3; arrayAll[array ...

  6. php 导出csv

    public function doworks(){        //输出Excel文件头,可把user.csv换成你要的文件名        header('Content-Type: appli ...

  7. 文件服务器迁移—FSMT

    www.canway.net/Lists/CanwayOriginalArticels/DispForm.aspx?ID=282

  8. Semantic UI 使用回调函数

    html代码: <div class="ui dropdown item" id="region"> <div class="tex ...

  9. jquery加载页面的方法(页面加载完成就执行)

    jquery加载页面的方法(页面加载完成就执行),建议大家看下windows.onload与$(document).ready之间的区别. 1.$(function(){  $("#a&qu ...

  10. Cookie与Session的一些总结

    一.Cookie: Cookie主要存储一些不敏感的数据,只能存储字符串类型 执行过程: (1)第一次请求: 客户端将数据(比如用户名)以请求报文的形式请求服务器端响应, 服务器端得到数据(用户名), ...