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

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. SVN环境

    SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...

  2. jQuery插件——可以动态改动颜色的jQueryColor

    1.请选择代码框中所有代码后, 保存为 jquery.color.js /*! * jQuery Color Animations v@VERSION * https://github.com/jqu ...

  3. eclipse 开发web程序,启动tomcat插件服务器的时候。部署目录在那里?

    不在tomcat-home/webapps/下面, 你做一个文件上传功能看看就知道了,临时目录一般是你的工作区间workspace\.metadata\.plugins\org.eclipse.wst ...

  4. springmvc最简单的搭建,初学者必看

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  5. Ceph rgws客户端验证

    修改/etc/ceph/ceph.conf文件,加入rados gw监听的端口 [client.rgw.rgws] rgw_frontends = "civetweb port=80&quo ...

  6. EasyUI 常规用法

    (function () {     // 获取树的路径,如 组织分类 > YHBH > 湖南省卫生厅 > 湖南省长沙市     var getBreadcrumbs = funct ...

  7. Go 语言简介(上)— 语法

    周末天气不好,只能宅在家里,于是就顺便看了一下Go语言,觉得比较有意思,所以写篇文章介绍一下.我想写一篇你可以在乘坐地铁或公交车上下班时就可以初步了解一门语言的文章.所以,下面的文章主要是以代码和注释 ...

  8. iOS开发-UIWebView加载本地和网络数据

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档,关于浏览网页榜样可以参考UC,手机必备浏览器,至于文档浏览的手机很多图书阅读软件,UIWebView是一个混合体,具体的功能控件内置 ...

  9. 不要再使用JS框架了

    停止编写Javascript框架吧. Javascript框架就好像死亡和税收一样:终究不可避免它的存在.我确信如果我是那面墙上的一只苍蝇,每次有人开始一个新的网页项目时,第一个问题肯定是我们用的是哪 ...

  10. C#: 实现支持断点续传多线程下载

    /* .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)* Reflector 了一下 System.Net.WebClien ...