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

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. 纯JS实现图片预览与等比例缩放和居中

    最近做项目时有一个需求,广告位图片上传时要预览,并且要等比例缩放和居中.已经保存的广告位图片显示时也要等比例缩放和居中.我使用了下面的代码实现,不过可能有一些小问题. <!DOCTYPE HTM ...

  2. c语言中的左移和右移

    先说左移,左移就是把一个数的所有位都向左移动若干位,在C中用<<运算符.例如: int i = 1;i = i << 2;  //把i里的值左移2位 也就是说,1的2进制是00 ...

  3. 9月16日,base 福州,2018MAD技术论坛邀您一起探讨最前沿AR技术!

    “ 人工智能新一波浪潮带动了语音.AR等技术的快速发展,随着智能手机和智能设备的普及,人机交互的方式也变得越来越自然. 9月16日,由网龙网络公司.msup联合主办的MAD技术论坛将在福州举行.本次论 ...

  4. KONE-FLOW Vistor Key

    WELCOME TO KONE

  5. webstorm 智能提示忽略大小写

    setting-Editor-General-Code Completion里的 Case sensitive completion: 可以设置只第一个字母敏感.完全敏感或者不敏感. 选择none.. ...

  6. nethogs 查看 Linux 进程的网络使用

    有时候我们客户会发现服务器或 VPS 网络慢,进一步发现大量带宽被占用,一些客户到这里为止就不知道怎么办了.有什么简单办法能找出哪个程序(或者进程)占用了带宽呢?Linux 监控流量的小工具不少,如 ...

  7. protobuffer、gRPC、restful gRPC的相互转化

    转自:https://studygolang.com/articles/12510 文档 grpc中文文档 grpc-gateway,restful和grpc转换库 protobuf 官网 proto ...

  8. 介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码

    http://gijgo.com   gijgo组件 特点:简易.文档全-虽然是英文的但是清晰易懂可读性强.含示例代码(后端直接用原生.Net C# MVC的哦!非常合.Net开发胃口),网站网速快, ...

  9. LeetCode 690 Employee Importance 解题报告

    题目要求 You are given a data structure of employee information, which includes the employee's unique id ...

  10. redis的基本介绍

    redis是什么? redis是一种菲关系型数据库,存储key-value类型的数据. redis支持的数据类型 这里所说的数据类型其实就是value对应的数据类型.一共有五种: String 1.S ...