1.分析

     1>首先我们有一个Service需要增强 将Service增加一个日志(Logger)
          2>写了一个日志的通知并且它可以对Service进行日志增强
          3>配了一个切面并且切面在引用logger这个通知  
          4>并明确告诉printlog要对execution中的方法增强 怎么增强就是2种创建代理对象的过程

2.ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置spring的IOC,把service对象配置进来 -->
<bean id="acctionService" class="com.hdh.service.impl.AccountServiceImpl"></bean> <!--spring中基于XML的AOP配置步骤
1.把bean通知也交给spring来管理
2.使用aop:config标签表明开始的AOP的配置
3.使用aop:aspect标签表明配置切面
id:是给切面提供一个
ref:是指定通知类bean的id
4.在aop:aspect标签的内部使用对应标签来配置通知类型
5.我们现在是让printLog方法在切入点执行之前,所以是前置通知
<aop:before></aop:before>
method属性:用于指定Logger中那个类中的那个方法是前置通知
6.切入点表达式的写法
                关键字:execution    
                 标准写法  表达式: 访问修饰符 返回值  包名...包名。方法名(参数列表)   
              public void com.hdh.service.impl.AccountServiceImpl.saveAccount()
              
    全通配写法:           *        *..*     .*          (..)
    修饰符可以省略     返回值任意     任意包下的      任意方法        参数任意
                                                    int 返回值为int
                                                     *返回值任意但必须有
通常配法:* com.hdh.service.impl.*.*(..) 表示包com.hdh.service.impl下同子包下所有返回值任意、类任意、方法任意、参数任意 都视为切入点
-->
<!--配置Logger -->
<bean id="logger" class="com.hdh.utils.Logger"></bean> <!--配置AOP -->
<aop:config>
<!--配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型,并且建立通知方法和切入点方法的关联
printlog在增强之前执行 (aop:before)
<aop:before method="printlog" pointcut="execution(public void com.hdh.service.impl.AccountServiceImpl.saveAccount())"/>
</aop:aspect>
</aop:config> </beans>

3种常用的通知类型

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置spring的IOC,把service对象配置进来 -->
<bean id="acctionService" class="com.hdh.service.impl.AccountServiceImpl"></bean>
<bean id="logger" class="com.hdh.utils.Logger"></bean>
<!--配置AOP -->
<aop:config>
        <!--  
            切入点通配表达式  pointcut_ref 引入
            作用范围更具 <aop:pointcut>配置的位置决定
            <aop:aspect>中作用范围就在<aop:aspect>    
            <aop:config>内作用范围则是整个切面中 但是必须在配置切面之前
             -->
            <aop:pointcut  id="pt1" expression="execution(* com.hdh.service.impl.*.*(..))"/>
<!--配置切面 -->
<aop:aspect id="beforePrintlog" ref="logger">
<!--配置前置通知,在切入点方法执行之前执行-->
<aop:before method="beforePrintlog" pointcut-ref="pt1" />
<!--配置后置通知,在切入点方法正常执行之后执行 -->
<aop:after-returning method="afterReturningPrintlog" pointcut-ref="pt1"/>        <!--配置异常通知,在切入点方法执行产生异常之后执行-->
       <aop:after-throwing method="exceptionPrintlog" pointcut="execution(* com.hdh.service.impl.*.*(..))" />         <!--配置最终通知,无论切入点方法是否正常执行,它都会最后执行 -->
       <aop:after method="afterPrintlog" pointcut="execution(* com.hdh.service.impl.*.*(..))" /> </aop:aspect> </aop:config> </beans>

4.配置一个log文件的前置通知

<bean id="acctionService" class="com.hdh.service.impl.AccountServiceImpl"></bean>
<!-- 配置log类 -->
<bean id="logger" class="com.hdh.service.impl.PrintLog"></bean>
<!--使用aop:config标签开始AOP配置 -->
<!--<aop:aspect>配置切面 -->
<aop:config>
<!--配置切面 -->
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知类型 -->
<aop:before method="prientLog" pointcut="execution(* com.hdh.service.impl.*.*(..))" />
</aop:aspect>
<aop:aspect>
<!-- ..... -->
</aop:aspect>
</aop:config>

TZ_05_Spring_基于AOP的xml配置的更多相关文章

  1. Spring中基于AOP的XML架构

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/xml-schema-based-aop-wi ...

  2. Spring 中基于 AOP 的 XML架构

    Spring 中基于 AOP 的 XML架构 为了使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述: <?xml version="1.0" e ...

  3. Spring AOP-xml配置

    在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...

  4. Spring AOP之xml 配置实现

    首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...

  5. day39-Spring 11-Spring的AOP:基于AspectJ的XML配置方式

    package cn.itcast.spring3.demo2; import org.aspectj.lang.ProceedingJoinPoint; /** * 切面类 * @author zh ...

  6. Spring3实战第一章 Aop 切面 XML配置

    刚看spring3实战书籍第一章  切面以前没有关注过 现在看到了  随手试验一下 AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Objec ...

  7. spring中aop以xml配置方式

    1 引jar包 springAOP\aopalliance.jar springAOP\aspectjrt.jar springAOP\aspectjweaver.jar springAOP\spri ...

  8. 【Spring四】AOP之XML配置

    AOP:Aspect Oriented  Programming 面向切面编程 面向切面编程的核心是动态代理设计模式.请先參见动态代理设计模式笔记. 以Hibernate保存一个对象到数据库为例,因为 ...

  9. spring自带的定时任务功能,基于注解和xml配置

    1.spring的配置文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&quo ...

随机推荐

  1. telnet- Linux必学的60个命令

    1.作用 telnet表示开启终端机阶段作业,并登入远端主机.telnet是一个Linux命令,同时也是一个协议(远程登陆协议). 2.格式 telnet [-8acdEfFKLrx][-b][-e] ...

  2. bzoj1010: [HNOI2008]玩具装箱toy——斜率优化

    方程 $\Large f(i)=min(f(j)+(s(i)-s(j)-1-L)^2)$ 其中$s(i)$为i的前缀和再加上$i$ 对于某个$i$若$j$比$k$优,则 $\large f(j)+(s ...

  3. win7 删除多余启动项的方法

    win7已经没有像xp那么简单的boot.ini让我们修改了,取而代之的是bcdedit.现在就简单的说下bcdedit的常规应用吧.开始,运行,输入bcdedit /?可以看到帮助.简单的应用开始. ...

  4. 【默默努力】PixelFire

    先放下我玩游戏的效果图: 关于游戏最后的结束部分其实我还没有截图,看着挺好看的,后面的效果 再放作者大大的项目地址:https://github.com/panruiplay/PixelFire 接下 ...

  5. make: 警告:检测到时钟错误。您的创建可能是不完整的。

    我在make的时候也出现了同样的问题,不过不是什么大问题,这个不影响编译结果,但是强迫症还是希望能解决掉 分析原因可能是:服务器上的文件最后修改时间比当前时钟要晚 解决办法:用touch 命令把源程序 ...

  6. Java-MyBatis-MyBatis3-XML映射文件:缓存

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:缓存 1.返回顶部 1. 缓存 MyBatis 内置了一个强大的事务性查询缓存机制,它可以非常方便地配置和定制. 为了使它更 ...

  7. iOS开发NSFetchedResultsController的使用CoreData和TableView数据同步更新

    1.效果 2.代码 #import "ViewController.h" #import "Student+CoreDataProperties.h" #def ...

  8. Spring Boot 集成Jsp与生产环境部署

    一.简介 提起Java不得不说的一个开发场景就是Web开发,也是Java最热门的开发场景之一,说到Web开发绕不开的一个技术就是JSP,因为目前市面上仍有很多的公司在使用JSP,所以本文就来介绍一下S ...

  9. [Ceoi2011]Traffic

    #2387. [Ceoi2011]Traffic Online Judge:Bzoj-2387,Luogu-4700 Label:Yy,Tarjan缩点,dfs 题目描述 格丁尼亚的中心位于Kacza ...

  10. Luogu P1429 平面最近点对(加强版)(分治)

    P1429 平面最近点对(加强版) 题意 题目描述 给定平面上\(n\)个点,找出其中的一对点的距离,使得在这\(n\)个点的所有点对中,该距离为所有点对中最小的. 输入输出格式 输入格式: 第一行: ...