欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3476161.html

这里简单对xml的配置方式做一下描述.代码还是上一篇(http://www.cnblogs.com/shizhongtao/p/3473362.html)的代码,只是配置方式改为xml的配置方式.测试用例类包括NotVeryUsefulAspect和Maneger两个。代码我还是贴一下。

 package com.bing.test;

 public class Manager {
private String myName;
private String description; public void sayHello() {
System.out.println("Hello " + myName);
} public void getDes() {
System.out.println(description); } public String getName() {
System.out.println("执行getName");
return myName;
} public String throwTest() throws Exception {
if (true) { throw new Exception("new throwing test!");
}
return "fail";
}
}
 package com.bing.test;

 public class NotVeryUsefulAspect {
//配置切入点集合,这样在下面可以直接引入
/* @Pointcut("execution(public * com.bing.test..*.sayHello(..))")
public void inManager() {}
@Pointcut("within(com.bing.test..*)")
public void excutionManager() {}*/
// 表示在方法前面执行
public void before() { System.out.println("before Method");
}
public void afterReturning(Object retVal) {
if(retVal!=null)
System.out.println("参数是:"+retVal);
System.out.println("afterReturning Method");
}
//@After("execution(public * com.bing.test..*.*(..))")
public void after() { System.out.println("after Method");
}
public void afterThrow(Exception ex){ System.out.println(ex.getMessage()); System.out.println("AfterThrowing Method!");
}
}

下面我就对xml文件做一下说明。在第一篇中有一个简单的xml配置,如下

<bean id="myInterceptor" class="com.bing.test.NotVeryUsefulAspect"></bean>
<aop:config>
<aop:aspect id="myAspect" ref="myInterceptor">
<aop:before method="before"
pointcut="execution(public * com.bing..*.sayHello(..))" />
<!-- 第一个*表示任何返回类型,“..”表示这个包下的所有子包,第二个*代表所有类,第二个“..”匹配了一个接受任意数量参数 -->
<!-- 当然sayHello方法你也可以用*代替,这样就表示所有方法。 -->
</aop:aspect>
</aop:config>

  上面的配置很清晰,不过也有点不够灵活。加入做大量配置的话,可能会写一些重复的xml代码。另外一种配置方式是:我们用 <aop:config>元素来申明一个切入点。如果你想对    所有service层来声明,你可以这样写:

 <aop:config>

   <aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..))"/> </aop:config>

这个expression用的表达式和,前几篇介绍的表达式一样,这时候如果你想用定义的切面类,可以这样配置:

 <aop:config>

   <aop:aspect id="myAspect" ref="aBean">

     <aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..)) &amp;&amp; this(service)"/>
<aop:before pointcut-ref="businessService" method="monitor"/>
... </aop:aspect> </aop:config>

上面的配置说明在ref="aBean"所指向的类中有着么一个方法:

public void monitor(Object service) {
...
}

综上,我对上一篇进行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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <context:annotation-config />
<aop:aspectj-autoproxy /> <context:component-scan base-package="com.bing">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="regex"
expression="com\.bing\.vo.*" />
<context:exclude-filter type="regex"
expression="com\.bing\.util.*" />
</context:component-scan>
<!-- 自定义的配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/user.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<bean id="manager" class="com.bing.test.Manager">
<property name="myName" value="${user.name}"></property>
<property name="description" value="${user.description}"></property>
</bean> <bean id="myInterceptor" class="com.bing.test.NotVeryUsefulAspect"></bean> <aop:config>
<!-- 匹配com.bing包以及子包下所有类以say开头的方法 -->
<aop:pointcut expression="execution(public * com.bing..*.say*(..))"
id="someMethod" />
<!-- 匹配com.bing.test下的Manager类 -->
<aop:pointcut expression="execution(public * com.bing..*.get*(..))"
id="returnMethod" />
<!-- 匹配com.bing.test下所有类 -->
<aop:pointcut expression="within(com.bing.test.*)" id="allMethod" />
<!-- 配置切入点和建议 -->
<aop:aspect id="myAspect" ref="myInterceptor">
<aop:before method="before"
pointcut-ref="someMethod" />
<aop:after method="after"
pointcut-ref="someMethod" />
</aop:aspect>
<!-- 带返回值的切入点配置 -->
<aop:aspect id="myAspect2" ref="myInterceptor">
<aop:after-returning method="afterReturning" returning="retVal"
pointcut-ref="returnMethod" />
</aop:aspect>
</aop:config> </beans>

 补充:xml方法配置Around参数得到方法的参数。例如对于某一个有两个参数的方法,我配置如下切入点

  public Object profile(ProceedingJoinPoint call, String name, int age) throws Throwable {
StopWatch clock = new StopWatch(
"Profiling for '" + name + "' and '" + age + "'");
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}

对应上面java代码的xml配置文件如下:

    <aop:config>
<aop:aspect ref="profiler"> <aop:pointcut id="theExecutionOfSomeFooServiceMethod"
expression="execution(* x.y.service.FooService.getFoo(String,int))
and args(name, age)"/> <aop:around pointcut-ref="theExecutionOfSomeFooServiceMethod"
method="profile"/> </aop:aspect>
</aop:config>

spring aop配置及用例说明(4)的更多相关文章

  1. spring aop配置及用例说明(2)

    欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...

  2. spring aop配置及用例说明(1)

    欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...

  3. spring aop配置及用例说明(3)

    欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...

  4. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  5. Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理

    一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...

  6. spring aop配置文档部分翻译

    欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...

  7. Spring——AOP配置时的jar包异常

    首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...

  8. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  9. perf4j+spring+aop 配置 注解方式

    今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...

随机推荐

  1. C#文件读写常用类介绍

    首先要熟悉.NET中处理文件和文件夹的操作.File类和Directory类是其中最主要的两个类.了解它们将对后面功能的实现提供很大的便利.      本节先对和文件系统相关的两个.NET类进行简要介 ...

  2. iOS开发——动画篇Swift篇&炫酷弹出菜单

    炫酷弹出菜单   这个是一个第三方按钮菜单组件,原版是使用Objective-C编写的名为AwesomeMenu的组件,地址是:https://github.com/levey/AwesomeMenu ...

  3. rabbitmq——用户管理

    安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...

  4. Spring与Hibernate、Mybatis整合

    在Web项目中一般会把各个web框架结合在一起使用,比如spring+hibernate,spring+ibatis等,如此以来将其他的框架整合到spring中来,便有些少许的不便,当然spring已 ...

  5. Android(java)学习笔记88:TextView属性大全

    TextView属性大全: android:autoLink       设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/ph ...

  6. 不支持关键字:metadata

    将 string sqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cos ...

  7. SSIS 学习(6):包配置(上)【转】

    Integrartion Services 包实际上就是一个对象属性的集合,在前面我们开发的所有 Integration Services包,其中的变量.属性,比如:数据库链接.同步文件目录等,我们都 ...

  8. Recovery启动流程(3)--recovery.cpp分析

    转载请注明来源:cuixiaolei的技术博客 这篇文章主要通过分析高通recovery目录下的recovery.cpp源码,对recovery启动流程有一个宏观的了解.MTK和高通的recovery ...

  9. 经常被忽略的几个CSS3属性之强大应用(一、timing-function: steps() 二、animation-direction  三、timing-function: cubic-bezier())

    一.timing-function: steps() 一开始在使用CSS3的时候并没有太注意这个timing-function,只是注意到自定义贝塞尔曲线. 1)一个项目中的实例 先来看看左边加了st ...

  10. LeetCode 343

    Integer Break Given a positive integer n, break it into the sum of at least two positive integers an ...