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

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. python爬取并批量下载图片

    import requests from lxml import etree url='http://desk.zol.com.cn/meinv/' add1='.html' urls=[] i = ...

  2. uploadify中文开发文档,解决php多图上传

    图片上传好用插件有,比如 uploadify  ueditor html5的各种ajax上传插件,大部分都是异步,返回只是true之类,有些时候需要上传图片需要一起上传,其实可以通过操作流程来避免这个 ...

  3. Entity Framework Core的贴心:优雅处理带默认值的数据库字段

    对于用于保存记录添加时间的数据库日期字段,我们通常会设置一个 GETDATE() 的默认值,而不是在应用程序的代码中获取当前时间进行保存,这样可以避免由于web服务器时钟不同步引起的时间偏差. Ent ...

  4. SQLServer 索引重建

    SQL Server 索引重建脚本 在数据的使用过程中,由于索引page碎片过多,带来一些不利的性能问题,我们有时候需要对数据库中的索引进行重组或者重建工作.通常这个阈值为30%,大于30%我们建议进 ...

  5. 应用打开其xlspptdoc等

    http://www.libxl.com/documentation.html  xls读写编辑类库libxl https://blog.csdn.net/songbob/article/detail ...

  6. Chap8:加密货币TOP100[《区块链中文词典》维京&甲子]

    根据2018年1月15日CoinMarketCap的加密货币市值排名编写,这里介绍TOP10,具体请参考<区块链中文词典>维京&甲子 01.比特币/Bitcoin/BTC 一种点对 ...

  7. Xcode编辑器之基本使用(一)

    前言. 苹果原生xcode使用介绍文档 1.Xcode IDE概览 说明: 从左到右,依次是“导航窗格(Navigator)->边列(Gutter)->焦点列(Ribbon)->代码 ...

  8. [skill][makefile] makefile 常用内容记录

    其实,makefile有点复杂. 文档看了又看,还是要经常翻,做个记录备忘 :) 1.  隐含命令 implicit rules 与 implicit rule 相对应的有 pattern rules ...

  9. Java如何连接SQLServer,并实现查询、修改、删除方法

    场景:A:在UI自动化时,删除数据时候,在界面UI提示“该XX已被使用,无法删除”. 这时候我们有需要做数据初始化的操作,需要把历史数据做删除,来确脚本运行的重复执行,和稳定性质. B: 在做新增操作 ...

  10. 快速安装elkstack

    一.介绍 The Elastic Stack - 它不是一个软件,而是Elasticsearch,Logstash,Kibana 开源软件的集合,对外是作为一个日志管理系统的开源方案.它可以从任何来源 ...