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

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. 【linux】centos7终端中文显示乱码,命令返回中文乱码

    centos7终端中文显示乱码,命令返回中文乱码 1.查看服务器编码的命令 1.1 echo $LANG 1.2 locale 1.3 查看终端xshell编码 如果以上的三点依旧保持一致,而依旧乱码 ...

  2. Facebook 工程师是如何高效工作的?

    编者按:Facebook 的工程师有哪些高效工作的经验呢?软件工程师访谈了多位 Facebook 的高产工程师,总结了他们的共同经验以及晋级之路,供各位参考. 成为高效开发者这件事你可以通过经验.书本 ...

  3. MyBatis使用Collection查询多对多或一对多结果集bug

    情况描述:当使用JOIN查询,如果SQL查询出来的记录不是按id列排序的,则生成的List结果会有问题 案例: 1) 数据库模型 简而言之一个Goods包含多个Goods_Img 2) Java Be ...

  4. 金蝶KIS下载地址

    升级方法: 您好,建议您先升级到标准版7.5,再升级到标准版8.1,直接用7.5的软件打开金蝶2000的账套,会提示升级,再用8.1的软件打开7.5的账套,升级前,需先备份账套. 金蝶KIS标准版和业 ...

  5. 第四章 四种List实现类的对比总结

    1.ArrayList 非线程安全 基于对象数组 get(int index)不需要遍历数组,速度快: iterator()方法中调用了get(int index),所以速度也快 set(int in ...

  6. Python源码学习七 .py文件的解释

    Python源码太复杂了... 今天看了下对.py文件的parse, 云里雾里的 py文件是最简单的, 在python的交互式窗口 import这个模块 a = 10 print(a) 开始分析,堆栈 ...

  7. Java基础(十四):泛型

    一.Java 泛型: Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质是参数化类型,也就 ...

  8. linux文件系统命令(1)---概述

    一.目的 本系列博文将介绍linux文件系统相关的命令. linux文件系统分为4个层面:用户空间程序.系统调用.内核虚拟文件系统以及实际文件系统.本系列文章仅仅涉及用户空间程序的操作及用法.旨在掌握 ...

  9. GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  10. 解决Sqlserver 2008 R2在创建登录名出错"此版本的 Microsoft Windows 不支持 MUST_CHANGE 选项。 (Microsoft SQL Server,错误: 15195)"

    错误信息:   执行 Transact-SQL 语句或批处理时发生了异常. (Microsoft.SqlServer.ConnectionInfo)   此版本的 Microsoft Windows ...