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

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. python文本 单独处理每个字符的方法汇总

    python文本 单独处理字符串每个字符的方法汇总 场景: 用每次处理一个字符的方式处理字符串 方法: 1.使用list(str) >>> a='abcdefg'    >&g ...

  2. 0x80070570 文件或目录损坏且无法读取 CHKDSK 修复方法

    错误 0x80070570: 文件或目录损坏且无法读取. 不要太担心是出现了磁盘坏道,也许只是小小的存储问题.解决方法很简单,用chsdsk命令即可. 方法如下: 开始--运行--输入cmd--输入c ...

  3. linux下限制ip访问

    inux下最直接限制ip访问的方式有两种: 1.使用hosts.allow和hosts.deny来设置ip白名单和黑名单,/etc/目录下. 优先级为先检查hosts.deny,再检查hosts.al ...

  4. Log4j按级别输出日志到不同文件配置分析

    关于LOG4J 按照级别输出日志,并按照级别输出到不同文件中的说法有很多, 网上贴的最多的log4j.properties的设置是这样的 log4j.rootLogger=info,stdout,in ...

  5. Android几种Service常驻内存的小思路

    老话说的好:躲得了初一,躲只是高三 ! 大多数的Android开发人员遇到的一个问题-怎样保证Service常驻内存. 近期我最终也在项目中务必幸运的遇到了 先来了解一下什么是Service常驻内存. ...

  6. HTML常用标记

    HTML文档由4个主要标记组成,这4个标记是<html>.<head>.<title>和<body>.举例如下: <html> <he ...

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

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

  8. 读书笔记-C#中装箱拆箱性能

    前言   最近在看王涛大神的<你必须知道的.NET(第二版)>一书,嗯,首先膜拜一下….     在书中的第五章-品味类型中,对装箱与拆箱一节感触很深,概念本身相信每一个程序猿都不陌生,装 ...

  9. Oracle根据字段值找到表名和列名

    方法1: --Oracle 根据字段值查询其所在的表.字段 DECLARE CURSOR cur_query IS SELECT table_name, column_name, data_type ...

  10. 在不重装系统的情况下撤底删除oracle数据库及oralce的相关软件

    先从控制面板删除oracle的相关应用及数据库, 删除系统变量 ORACLE_OEM_CLASSPATH=%JAVA_HOME%\lib\ext\access-bridge-64.jar;%JAVA_ ...