面向切面:主要应用在日志记录方面。实现业务与日志记录分离开发。

spring面向切面有两种实现方式:1、注解 2、xml配置。

1、注解实现如下:

(1)配置如下:

  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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
  12. <context:annotation-config/>
  13. <aop:aspectj-autoproxy/>
  14. <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
  15. <bean id="testAop" class="com.test.spring.AOP.testAop"/>
  16. </beans>

注意:红色部分的配置

(2)切入实现的代码如下:

  1. @Aspect
  2. public class testAop {
  3.  
  4. //指明所要代理的类或方法
  5. @Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))")
  6. public void pointCut(){}
  7.  
  8. @After("pointCut()")
  9. public void after(){
  10. System.out.println("after");
  11. }
  12.  
  13. @Before("pointCut()")
  14. public void before(){
  15. //JoinPoint joinPoint
  16. System.out.println("before");
  17. }
  18.  
  19. @Around("pointCut()")
  20. public Object around(ProceedingJoinPoint joinpoint){
  21. //joinpoint.getArgs()能够得到传入到方法的参数
    Object valu = null
  22. try {
  23. System.out.println("aaaaaaaa");
  24. valu = joinpoint.proceed();
  25. //返回所代理的方法(有返回值的方法)返回值
    System.out.println(valu);
  26.  
  27. } catch (Throwable e) {
  28. e.printStackTrace();
  29. }
  30. return valu;
  31. }
  32.  
  33. @AfterReturning("pointCut()")
  34. public void afteReturning(){
  35. System.out.println("afteReturning");
  36. }
  37.  
  38. @AfterThrowing("pointCut()")
  39. public void afterThrowing(){
  40. System.out.println("afterThrowing");
  41. }
  42. }

2、xml配置实现方式

  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"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  8. http://www.springframework.org/schema/aop
  9. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
  12. <context:annotation-config/>
  13.  
  14. <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
  15. <bean id="testAop" class="com.test.spring.AOP.testAop"/>
  16. <aop:config>
  17. <aop:aspect id="Pservice" ref="testAop">
  18. <!-- 配置指定切入的对象 -->
  19. <aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" />
  20. <!-- 只匹配add方法作为切入点
  21. <aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" />
  22. -->
  23. <!-- 前置通知 -->
  24. <aop:before method="before" pointcut-ref="point_cut" />
  25. <!-- 后置通知 returning指定返回参数 -->
  26. <aop:after-returning method="after" pointcut-ref="point_cut" returning="result" />
  27. <!-- 环绕通知 -->
  28.  
  29.         <aop:around method="around" pointcut-ref="point_cut"/>
  30. <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/>
  31. </aop:aspect>
  32. </aop:config>
  33. </beans>

spring的面向切面实现的两种方式的更多相关文章

  1. Spring加载properties文件的两种方式

    在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...

  2. Spring Boot定义系统启动任务的两种方式

    Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...

  3. Spring Boot 中实现定时任务的两种方式

    在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...

  4. Spring容器自动调用方法的两种方式

    先看一个Spring中Bean的实例化过程: 1.配置文件中指定Bean的init-method参数 <bean class="com.jts.service.UserService& ...

  5. Spring系列之AOP实现的两种方式

    AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...

  6. 在Spring整合aspectj实现aop的两种方式

    -----------------------------基于XML配置方案目标对象接口1 public interface IUserService { public void add(); pub ...

  7. Spring使用JMS传递消息的两种方式

    方式一:同步收发消息,使用JMS template 消费者阻塞等待消息的到来. 方式二:异步收发消息,使用message listener container 消费者提供一个listener,注册一个 ...

  8. spring boot中读取配置文件的两种方式

    application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...

  9. Spring集成Quartz框架的两种方式。

    可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...

随机推荐

  1. RabbitMQ 学习

    参考:https://www.rabbitmq.com/getstarted.html 先在本地安装RabbitMQ 组件(需要安装Erlang组件),启动服务. 激活 RabbitMQ's Mana ...

  2. shell利用数组分割组合字符串

    #!/bin/bash #接收脚本参数如[sh a.txt .0_3_4_f_u_c_k_8080] a=$ #把参数分割成数组 arr=(${a//_/ }) #显示数组长度 #echo ${#ar ...

  3. java中常用的加密方式

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  4. 配置数据源的三种方式和sql心跳的配置

    三种方式配置数据源连接池: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  5. solus 系统 - 自定义终端快捷键

    终端: 命令方式:Alt+F2 输入“gnome-terminal“ 全屏显示 control+alt+F1 (退出 control+alt+F2) 可在`键盘`菜单中添加自定义快捷方式: 其它快捷方 ...

  6. 《JAVA编程思想》第四版 PDF 下载 中文版和英文版 高清PDF扫描带书签

    一.链接: 中文版: https://pan.baidu.com/s/1d07Kp4 密码:x2cd 英文版: https://pan.baidu.com/s/1boOSdAZ 密码: rwgm 文件 ...

  7. MySQL使用mysqldump备份及还原

    MySQL可以使用mysqldump进行数据的逻辑备份,配合开启bin log日志可以实现数据的全量恢复及增量恢复 MySQL版本查看 修改配置文件记录bin log日志 [mysqld] #bin ...

  8. 标准库random

    pseudo-random number generators for various distributions. Almost all module functions depend on the ...

  9. win10 安装 open live write

    安装完 open live write后将Memento.OLW_V1.0.0.3.7z解压到C:\Users\pc_name\AppData\Local\OpenLiveWriter\app-0.6 ...

  10. Sticks POJ - 1011 少林神棍 dfs四次剪枝

    http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...