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

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. day_6.6 py

    Tftp 下载器演示 2018-6-6 14:11:09 #!/usr/bin/env python #!--*--coding:utf-8 --*-- #!@Time :2018/6/6 11:55 ...

  2. day_5.07py

    正则:

  3. js 使用a标签 下载资源

    文档 let data = new Blob(['hello ajanuw'], { type: 'application/text' }) let src = window.URL.createOb ...

  4. 快速排序中的partition.

    经典快速排序中的partition, 将最后一个元素作为划分点. 维护两个区域. <= x 的, >x 的区域. 划分过程中还有个待定的区域. [L,less] 区域小于x, [less+ ...

  5. [No0000BB]ReSharper操作指南4/16-配置ReSharper代码快修与导航

    代码问题的快速修复 ReSharper可以帮助您立即修复设计时检测到的大部分代码问题.就像按Alt+Enter突出显示的代码问题一样简单,并选择合适的方法来解决问题或改进次优代码. GIF 应用快速修 ...

  6. [No0000121]Python教程4/9-输入和输出

    输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...

  7. centos 安装docker-compose

    #查看docker compose版本 docker-compose version #查看pip版本 pip -v #上一条语句没有显示版本信息则运行下面语句安装 python-pip yum -y ...

  8. Node的REPL环境

    1. Node的REPL环境 什么是REPL REPL全称 Read-eval-print-loop,交互式解析器 REPL可以提供给程序员对Node.js的一些api快速测试 REPL的基本操作 定 ...

  9. DRBD数据镜像与搭建

    一.数据安全工具DRDB 1. 数据镜像软件DRDB介绍 分布式块设备复制,是基于软件.基于网络的块复制存储解决方案 作用:用于服务器之间的磁盘.分区.逻辑卷等进行数据镜像. 例如:当用户将数据写入本 ...

  10. 关于richtextbox改变字体颜色,加下划线

    参考了三份有用的资料: 1.关于richtextbox设置字体颜色的问题 http://biancheng.dnbcw.net/c/180381.html 2.C#Winform使用扩展方法自定义富文 ...