配置文件与注解方式的有很大不同,多了很多配置项。

beans2.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
     <aop:aspectj-autoproxy />
    <bean id="personService" class="test.spring.service.impl.PersonServiceBean"></bean>
    <bean id="myInterceptor" class="test.spring.aop.MyInterceptor2"></bean>
    <aop:config>
           <aop:aspect id="myAspect" ref="myInterceptor">
                    <aop:pointcut  id="myPointCut"  expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))" />
                    <aop:before pointcut-ref="myPointCut" method="doAccessCheck" />
                    <aop:after-returning pointcut-ref="myPointCut"  method="doAfterReturning" />
                    <aop:after-throwing pointcut-ref="myPointCut"  method="doAfterThrowing" />
                    <aop:around pointcut-ref="myPointCut" method="doAround" />
                    <aop:after pointcut-ref="myPointCut" method="doAfter" />
           </aop:aspect>
    </aop:config>
</beans> 

切面的切入点语法定义

  • 拦截test.spring.service.impl.PersonServiceBean下的所有方法
    expression="execution(* test.spring.service.impl.PersonServiceBean.*(..))"
  • 拦截test.spring.service.impl子包下的所有类的所有方法
    expression="execution(* test.spring.service.impl..*.*(..))"
  • 拦截test.spring.service.impl.PersonServiceBean下的所有返回值为String类型的方法
    expression="execution(java.lang.String test.spring.service.impl.PersonServiceBean.*(..))"
  • 拦截test.spring.service.impl.PersonServiceBean下的所有方法中第一个参数类型为String的方法
    expression="execution(* test.spring.service.impl.PersonServiceBean.*(java.lang.String,..))"
  1. package test.spring.service.impl;
  2. import test.spring.service.PersonService;
  3. //代理对象实现目标对象所有接口
  4. public class PersonServiceBean implements PersonService {
  5. public PersonServiceBean() {
  6. }
  7. @Override
  8. public void save(String name) {
  9. System.out.println("save()->>" + name);
  10. throw new RuntimeException(">>----自定义异常----<<");
  11. }
  12. @Override
  13. public String getResult() {
  14. return "getResult()==>>返回结果";
  15. }
  16. }
  1. package test.spring.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class MyInterceptor2 {
  4. public void doAccessCheck() {
  5. System.out.println("前置通知-->>");
  6. }
  7. public void doAfterReturning() {
  8. System.out.println("后置通知-->>");
  9. }
  10. public void doAfter() {
  11. System.out.println("最终通知");
  12. }
  13. public void doAfterThrowing() {
  14. System.out.println("异常通知-->");
  15. }
  16. public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable {
  17. System.out.println("环绕通知");
  18. // 这里如果pJoinPoint.proceed()不执行,后面拦截到的方法都不会执行,非常适用于权限管理
  19. Object result = pJoinPoint.proceed();
  20. System.out.println("退出");
  21. return result;
  22. }
  23. }
  1. package test.spring.junit;
  2. import org.junit.Test;
  3. import org.springframework.context.support.AbstractApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import test.spring.service.PersonService;
  6. public class AOPTest3 {
  7. @Test
  8. public void test() {
  9. AbstractApplicationContext aContext = //
  10. new ClassPathXmlApplicationContext("beans2.xml");
  11. PersonService pService = (PersonService) aContext
  12. .getBean("personService");
  13. pService.save("LinDL");
  14. pService.getResult();
  15. aContext.close();
  16. }
  17. }

 
 

Spring的xml文件配置方式实现AOP的更多相关文章

  1. Spring(十二)使用Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...

  2. spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)

    **这个整合.仅仅是最主要的整合,而且是xml配置文件的方式之中的一个,即当中的mybatis是採用非mapper接口的方式.(第二遍採用mapper接口方式.第三遍採用注解的方式:第四篇採用注解基于 ...

  3. SSM框架中spring的XML文件配置

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  4. spring——通过xml文件配置IOC容器

    创建相关的类(这里是直接在之前类的基础上进行修改) package com.guan.dao; public interface Fruit { String getFruit(); } packag ...

  5. Spring中xml文件配置也可以配置容器list、set、map

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. Spring中Bean的配置:基于XML文件的方式

    Bean的配置一共有两种方式:一种是基于XML文件的方式,另一种是基于注解的方式.本文主要介绍基于XML文件的方式 <bean id="helloWorld" class=& ...

  7. Spring整合Hibernate的XML文件配置,以及web.xml文件配置

    利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...

  8. 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

  9. 曹工说Spring Boot源码(17)-- Spring从xml文件里到底得到了什么(aop:config完整解析【中】)

    写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...

随机推荐

  1. 关于mybatis的理解

    http://blog.csdn.net/jiuqiyuliang/article/details/45132493 写的不错很好!

  2. paper 123: SVM如何避免过拟合

    过拟合(Overfitting)表现为在训练数据上模型的预测很准,在未知数据上预测很差.过拟合主要是因为训练数据中的异常点,这些点严重偏离正常位置.我们知道,决定SVM最优分类超平面的恰恰是那些占少数 ...

  3. 对jquery分页的升级

    以前写的分页用了好久了,但是分页时除了传页面外有时还要传一些其它参数,以前操作时要把传的参数放到隐藏hidden中,分页时在取出来,这样比较麻烦,今天无事,重新包装了一下 直接上代码吧 css用了bo ...

  4. 初次了解的Java多线程

    0.1熟悉多线程 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能 ...

  5. 使用requireJS,backboneJS,和underscoreJS完成自定义模板封装

    使用requireJS,backboneJS,和underscoreJS完成自定义模板封装 原来的代码 当我们进行一个列表的数据填充的时候,是这样做的: //获取美食列表 function getFo ...

  6. squid-nginx 基本配置

    #本地绑定的IP端口 http_port 192.168.1.253:801 vhost visible_hostname test-squid cache_dir ufs c:/squid/cach ...

  7. OC-01 编译链接的作用

    编译:检测代码的语法合法性,随后生成.o文件. 链接:把项目中所有的.out合并,生成一个可执行文件. OC编译连接过程 .m---->.o---->.out  . 检测源文件的语法合法性 ...

  8. Configure Visual Studio 2013 for debugging .NET framework

    https://referencesource.microsoft.com/ In order to configure Visual Studio 2013 do the following in ...

  9. vs打开项目出现“尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……”提示

    今天打开网上下载的一个源码,出现如标题的这个问题,这是从未遇见的提示.尝试点击是,但是网站还是运行不起来.于是网上搜索,就有了这篇. 解决的方案如下: 注意:也可以用记事本把工程文件(.vcxproj ...

  10. weboffice控件使用不能嵌入网页

    var s = ""s += "<object id=WebOffice1 height=586 width='100%' style='LEFT: 0px; TO ...