欢迎交流转载: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. zookeeper作为soa服务器集群的协调调度服务器

    zookeeper作为soa服务器集群的协调调度服务器,当然自身也支持集群. ZooKeeper搭建系列集 ZooKeeper系列之一:ZooKeeper简介 ZooKeeper系列之二:ZooKee ...

  2. [MODX] 1. Template *

    After uploading javascript, css and images to the assets folder. We try to use Template to customize ...

  3. Android 滑动效果基础篇(三)—— Gallery仿图像集浏览

    Android系统自带一个Gallery浏览图片的应用,通过手指拖动时能够非常流畅的显示图片,用户交互和体验都很好. 本示例就是通过Gallery和自定义的View,模仿实现一个仿Gallery图像集 ...

  4. 使用maven命令建立java项目

    在terminal中输入: mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -Darc ...

  5. Python 学习之二:Python超短教程

    前言 本教程综合Stanford CS231N和UC Berkerley CS188的Python教程. 教程非常短,但适合有一定编程基础.学过其它语言的童鞋. Python 启动Python 解释器 ...

  6. sysbench 安装 原创

    1.下载sysbench version 0.5 https://github.com/akopytov/sysbench 2. [root@server1 sysbench-0.5]# pwd/ro ...

  7. C++中创建对象的时候加括号和不加括号的区别

    c++创建对象的语法有----- 1 在栈上创建 MyClass a; 2 在堆上创建加括号 MyClass *a= new MyClass(); 3 不加括号 MyClass *a = new My ...

  8. cocos2d-x使用ant批量打包

    当项目需要在多渠道上线时,要打很多的渠道包,少则几十个,多种几百个.它们的区别一般只是渠道id或部分配置信息不同,这些信息均可写在配置文件中. 例如常见的渠道id不同,一般定义在AndroidMani ...

  9. Javascript oop深入学习笔记(三)--javascript中类的实现

    一.类的实现机制 在javascript中可以使用function关键字来定义一个类.在函数内通过this指针引用的变量或则方法都会成为类的成员. function classDemo(){ var ...

  10. Sqlite和CoreData的区别

    使用方便性.实际上,一个成熟的工程中一定是对数据持久化进行了封装的,因此底层使用的到底是core data还是sqlite,不应该被业务逻辑开发者关心.因此,即使习惯写SQL查询的人,也应该避免在业务 ...