欢迎交流转载: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. MySQL 行子查询(转)

    MySQL 行子查询 行子查询是指子查询返回的结果集是一行 N 列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集. 一个行子查询的例子如下: SELECT * FROM table1 WH ...

  2. fedora 20 注销

    当系统只有一个用户和只有一个桌面环境时,Fedora 20将不会显示Log Out菜单. 如果你确实需要logout,可以通过执行gnome-session-quit命令来logout. 如果你确实需 ...

  3. 在 Fedora 里安装自带的 MATE和 cinnamon

    参见  http://wiki.mate-desktop.org/download#fedora安装方法: yum groupinstall mate-desktop yum groupinstall ...

  4. gradle使用小记

    1.全局排除依赖: allprojects {    apply plugin: 'java'    apply plugin: 'eclipse'    apply plugin: 'maven-p ...

  5. 循环语句until和while

    一.until语句的基本格式 until 条件测试 do 语句块 done 只要条件测试语句未成功结束,则执行语句块.(如果一开始条件测试语句就成功退出,那么一次也不执行语句块.这里跟C语言中的do. ...

  6. 强烈推荐240多个jQuery插件提供下载

    jQuery 是继 prototype 之后又一个优秀的 Javascript 框架.其宗旨是—写更少的代码,做更多的事情.它是轻量级的 js 库(压缩后只有21k) ,这是其它的 js 库所不及 的 ...

  7. Qt focusoutevent 不响应的解决方法

    一般利用focus(焦点)来实现弹窗自动关闭效果. Qt的focus貌似是自己的bug, 经常无法接收到focusout的事件 例如: widgetA 中执行  widgetB->show(); ...

  8. 小白日记20:kali渗透测试之后渗透测试阶段(一)--上传工具

    后渗透测试阶段--上传工具 为防止管理员将漏洞补上后,我们无法再通过该漏洞控制对方主机,所以需要进行后渗透测试阶段 1.上传各种工具 2.提权:为了全面控制目标系统 3.擦除攻击痕迹:防止管理员通过日 ...

  9. 小白日记9:kali渗透测试之主动信息收集(二)四层发现:TCP、UDP、nmap、hping、scapy

    四层发现 四层发现的目的是扫描出可能存活的IP地址,四层发现虽然涉及端口扫描,但是并不对端口的状态进行精确判断,其本质是利用四层协议的一些通信来识别主机ip是否存在. 四层发现的优点: 1.可路由且结 ...

  10. 视频-某hadoop高级应用-搜索提示

    看了北风的免费视频,只有一个案例,苦逼买不起几百上千的视频教程 先搭建简单的web项目,基于struts,使用到了bootstrap. 界面: web.xml <filter> <f ...