spring的面向切面实现的两种方式
面向切面:主要应用在日志记录方面。实现业务与日志记录分离开发。
spring面向切面有两种实现方式:1、注解 2、xml配置。
1、注解实现如下:
(1)配置如下:
- <?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"
- 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 ">
- <context:annotation-config/>
- <aop:aspectj-autoproxy/>
- <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
- <bean id="testAop" class="com.test.spring.AOP.testAop"/>
- </beans>
注意:红色部分的配置
(2)切入实现的代码如下:
- @Aspect
- public class testAop {
- //指明所要代理的类或方法
- @Pointcut("execution(* com.test.spring.AOP.Personservice.*(..))")
- public void pointCut(){}
- @After("pointCut()")
- public void after(){
- System.out.println("after");
- }
- @Before("pointCut()")
- public void before(){
- //JoinPoint joinPoint
- System.out.println("before");
- }
- @Around("pointCut()")
- public Object around(ProceedingJoinPoint joinpoint){
- //joinpoint.getArgs()能够得到传入到方法的参数
Object valu = null- try {
- System.out.println("aaaaaaaa");
- valu = joinpoint.proceed();
- //返回所代理的方法(有返回值的方法)返回值
System.out.println(valu);- } catch (Throwable e) {
- e.printStackTrace();
- }
- return valu;
- }
- @AfterReturning("pointCut()")
- public void afteReturning(){
- System.out.println("afteReturning");
- }
- @AfterThrowing("pointCut()")
- public void afterThrowing(){
- System.out.println("afterThrowing");
- }
- }
2、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"
- 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 ">
- <context:annotation-config/>
- <bean id="Psv" class="com.test.spring.AOP.Personservice"/>
- <bean id="testAop" class="com.test.spring.AOP.testAop"/>
- <aop:config>
- <aop:aspect id="Pservice" ref="testAop">
- <!-- 配置指定切入的对象 -->
- <aop:pointcut id="point_cut" expression="execution(* execution(* frame.com.Action.login.*(..)))" />
- <!-- 只匹配add方法作为切入点
- <aop:pointcut id="except_add" expression="execution(* com.test.spring.AOP.*.addPerson(..))" />
- -->
- <!-- 前置通知 -->
- <aop:before method="before" pointcut-ref="point_cut" />
- <!-- 后置通知 returning指定返回参数 -->
- <aop:after-returning method="after" pointcut-ref="point_cut" returning="result" />
- <!-- 环绕通知 -->
- <aop:around method="around" pointcut-ref="point_cut"/>
- <aop:after-throwing method="doThrow" pointcut-ref="point_cut" throwing="e"/>
- </aop:aspect>
- </aop:config>
- </beans>
spring的面向切面实现的两种方式的更多相关文章
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Spring Boot定义系统启动任务的两种方式
Spring Boot定义系统启动任务的两种方式 概述 如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web ...
- Spring Boot 中实现定时任务的两种方式
在 Spring + SpringMVC 环境中,一般来说,要实现定时任务,我们有两中方案,一种是使用 Spring 自带的定时任务处理器 @Scheduled 注解,另一种就是使用第三方框架 Qua ...
- Spring容器自动调用方法的两种方式
先看一个Spring中Bean的实例化过程: 1.配置文件中指定Bean的init-method参数 <bean class="com.jts.service.UserService& ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- 在Spring整合aspectj实现aop的两种方式
-----------------------------基于XML配置方案目标对象接口1 public interface IUserService { public void add(); pub ...
- Spring使用JMS传递消息的两种方式
方式一:同步收发消息,使用JMS template 消费者阻塞等待消息的到来. 方式二:异步收发消息,使用message listener container 消费者提供一个listener,注册一个 ...
- spring boot中读取配置文件的两种方式
application.properties test.name=测试 test.url=www.test.com 1.@Value注解 在controller里可以这样直接调用 @Value(&qu ...
- Spring集成Quartz框架的两种方式。
可参考:https://blog.csdn.net/yk614294861/article/details/84324603 1.使用Spring与Quarta配置作业得两种方式: a.方式一,Met ...
随机推荐
- RabbitMQ 学习
参考:https://www.rabbitmq.com/getstarted.html 先在本地安装RabbitMQ 组件(需要安装Erlang组件),启动服务. 激活 RabbitMQ's Mana ...
- shell利用数组分割组合字符串
#!/bin/bash #接收脚本参数如[sh a.txt .0_3_4_f_u_c_k_8080] a=$ #把参数分割成数组 arr=(${a//_/ }) #显示数组长度 #echo ${#ar ...
- java中常用的加密方式
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...
- 配置数据源的三种方式和sql心跳的配置
三种方式配置数据源连接池: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- solus 系统 - 自定义终端快捷键
终端: 命令方式:Alt+F2 输入“gnome-terminal“ 全屏显示 control+alt+F1 (退出 control+alt+F2) 可在`键盘`菜单中添加自定义快捷方式: 其它快捷方 ...
- 《JAVA编程思想》第四版 PDF 下载 中文版和英文版 高清PDF扫描带书签
一.链接: 中文版: https://pan.baidu.com/s/1d07Kp4 密码:x2cd 英文版: https://pan.baidu.com/s/1boOSdAZ 密码: rwgm 文件 ...
- MySQL使用mysqldump备份及还原
MySQL可以使用mysqldump进行数据的逻辑备份,配合开启bin log日志可以实现数据的全量恢复及增量恢复 MySQL版本查看 修改配置文件记录bin log日志 [mysqld] #bin ...
- 标准库random
pseudo-random number generators for various distributions. Almost all module functions depend on the ...
- win10 安装 open live write
安装完 open live write后将Memento.OLW_V1.0.0.3.7z解压到C:\Users\pc_name\AppData\Local\OpenLiveWriter\app-0.6 ...
- Sticks POJ - 1011 少林神棍 dfs四次剪枝
http://poj.org/problem?id=1011 题意:若干根棍子被截成小段的木棒,现在给你这些木棒,问最短可以拼出的棍子长度. 题解:搜索,dfs(r,m) 二个参数分别代表还剩r个木棒 ...