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

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,..))"

package test.spring.service.impl;

import test.spring.service.PersonService;

//代理对象实现目标对象全部接口
public class PersonServiceBean implements PersonService { public PersonServiceBean() { } @Override
public void save(String name) {
System.out.println("save()->>" + name);
throw new RuntimeException(">>----自己定义异常----<<");
} @Override
public String getResult() {
return "getResult()==>>返回结果";
} }
package test.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyInterceptor2 {

	public void doAccessCheck() {
System.out.println("前置通知-->>");
} public void doAfterReturning() {
System.out.println("后置通知-->>");
} public void doAfter() {
System.out.println("终于通知");
} public void doAfterThrowing() {
System.out.println("异常通知-->");
} public Object doAround(ProceedingJoinPoint pJoinPoint) throws Throwable {
System.out.println("围绕通知");
// 这里假设pJoinPoint.proceed()不运行。后面拦截到的方法都不会运行,很适用于权限管理
Object result = pJoinPoint.proceed();
System.out.println("退出");
return result;
} }
package test.spring.junit;

import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import test.spring.service.PersonService; public class AOPTest3 { @Test
public void test() {
AbstractApplicationContext aContext = //
new ClassPathXmlApplicationContext("beans2.xml");
PersonService pService = (PersonService) aContext
.getBean("personService");
pService.save("LinDL");
pService.getResult();
aContext.close();
} }

Spring(十二)使用Spring的xml文件配置方式实现AOP的更多相关文章

  1. Spring的xml文件配置方式实现AOP

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

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

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

  3. 三十二、python操作XML文件

    '''XML:模块 xml总结 1.解析 str 文件 tree,ElementTree,type root,Element,type2.操作 Element: tag,text,find,iter, ...

  4. Spring Boot(十二):spring boot如何测试打包部署

    Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...

  5. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  6. Spring中加载ApplicationContext.xml文件的方式

    Spring中加载ApplicationContext.xml文件的方式 原文:http://blog.csdn.net/snowjlz/article/details/8158560 1.利用Cla ...

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

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

  8. 4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean

    在 Spring 容器内拼凑 bean 叫做装配.装配 bean 的时候,你是在告诉容器,需要哪些 bean ,以及容器如何使用依赖注入将它们配合在一起. 理论上,bean 装配的信息可以从任何资源获 ...

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

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

随机推荐

  1. 【POJ】【3525】Most Distant Point from the Sea

    二分+计算几何/半平面交 半平面交的学习戳这里:http://blog.csdn.net/accry/article/details/6070621 然而这题是要二分长度r……用每条直线的距离为r的平 ...

  2. Java并发学习之十五——使用读写锁同步数据訪问

    本文是学习网络上的文章时的总结.感谢大家无私的分享. 读写锁重要的是写锁的使用,仅仅用一个入口. 以下是读写锁使用的样例 package chapter2; import java.util.conc ...

  3. spoj Goblin Wars(简单bfs)

    J - Goblin Wars Time Limit:432MS    Memory Limit:1572864KB    64bit IO Format:%lld & %llu Submit ...

  4. 【属性动画总结】Property Animation

    属性动画概述 3.0以前,android仅支持两种动画模式,tweened animation 和 frame-by-frame animation,在android3.0中又引入了一个新的动画系统: ...

  5. C#全屏截图

    Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); ...

  6. scala 学习笔记八 简洁性

    Scala可以简洁地表示概念,有时甚至可以说过于简洁. 1.消除中间结果 在组合表达式中,最后一个表达式会变成整个表达式的结果.下面的例子中,值会被捕获到val result中,然后result从方法 ...

  7. (转)U3D不同平台载入XML文件的方法——IOS MAC Android

    自:http://www.cnblogs.com/sifenkesi/archive/2012/03/12/2391330.html 在PC上和IOS上读取XML文件的方式略有差别,经测试,IOS上不 ...

  8. Javascript中计算脚本运行的时间

    console.time("timer名字") 其他脚本 console.timeEnd("timer名字"); 运行后结果为: timer名字: 运行时间

  9. PS中标尺工具在哪里

    打开photoshop应用程序,然后在PS菜单中点击“视图——标尺”选项即可打开ps标尺栏. 单位设置:      按Ctrl+K快捷键会弹出“首选项”对话框,你选择“单位与标尺”项,在“单位”一栏里 ...

  10. CheeseZH: Stanford University: Machine Learning Ex2:Logistic Regression

    1. Sigmoid Function In Logisttic Regression, the hypothesis is defined as: where function g is the s ...