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

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的面向切面实现的两种方式的更多相关文章

  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. Thrift的一些概念

    Thrift最初是由Facebook开发的,因为随着流量和网络结构的扩展,一些操作如搜索.分发.事件日志记录等已经超出系统的处理范围,所以Facebook的工程师开发服务时选择了多种不同的编程语言来达 ...

  2. python 同时运行两个程序

    linux的话: python py1.py & python py2/py &

  3. ubuntu为文件添加可执行权限

    为一个文件添加可执行权限 chmod +x filename 为一个文件夹下的所有文件添加可执行权限 chmod +x *

  4. 在VMware中使用Nat方式设置静态IP, 宿主机可以 ssh

    坑很多:  麻痹,  主要还是要先 防火墙关掉,永久关掉.  seliux 也永久关掉. 临时关闭防火墙:systemctl stop firewalld    开机不启动: systemctl di ...

  5. LeetCode 4 - 两个排序数组的中位数 - [分治]

    题目链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/ 给定两个大小为 m 和 n 的有序数组 n ...

  6. 关于ionic如何到最新版本

    首先删除 我叫他它叫做依赖node_modules 文件夹然后修改 修改依赖版本package.json 各个文件的版本号接着下载 下载最新的依赖 会根据package.json 各个文件的版本号添加 ...

  7. wpf中通过ObjectDataProvider实现文本框的双向数据绑定(ps:适用于在文本框比较多的时候使用)

    前端代码: 也页面的xaml中引入ObjectDataProvider: <Window.Resources> <ResourceDictionary> <ObjectD ...

  8. DATAPUMP进程查询

    SELECT * FROM DBA_DATAPUMP_SESSIONS; SELECT * FROM DBA_DATAPUMP_JOBS; 例如: SYS@orclasm > SELECT * ...

  9. redis使用rdb恢复数据

    redis中存在rdb备份和aof备份两种方式. 如果在redis多个节点发生雪崩时,我们往往使用定期冷备rdb或者aof文件,去恢复数据的方式,但往往数据量较大时rdb恢复更加的快速,毕竟aof保存 ...

  10. falsk_蓝图(blueprint)

    蓝图(blueprint) 随着业务代码的增加,将所有代码都放在单个程序文件中,是非常不合适的.这不仅会让代码阅读变得困难,而且会给后期维护带来麻烦. 什么是蓝图 蓝图:用于实现单个应用的视图.模板. ...