spring aop配置及用例说明(4)
欢迎交流转载: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.*.*(..)) && 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)的更多相关文章
- spring aop配置及用例说明(2)
欢迎交流转载:http://www.cnblogs.com/shizhongtao/p/3473362.html 这里先介绍下几个annotation的含义, @Before:表示在切入点之前执行. ...
- spring aop配置及用例说明(1)
欢迎转载交流,博客地址http://www.cnblogs.com/shizhongtao/p/3469776.html 首先,什么是aop,其实通俗一点讲就是,再方法执行时候我们加入其它业务逻辑.比 ...
- spring aop配置及用例说明(3)
欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...
- Spring AOP配置方式
AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...
- Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理
一.看一下简单的通过XML的AOP配置 1.首先创建一个简单的Student类 public class Student { private Integer age; private String n ...
- spring aop配置文档部分翻译
欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...
- Spring——AOP配置时的jar包异常
首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- perf4j+spring+aop 配置 注解方式
今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单.具体配置如下: logback.xml <!--perf4j配置--> <appender name= ...
随机推荐
- Codeforces Round #326 (Div. 2) D. Duff in Beach dp
D. Duff in Beach Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...
- 使用导入导出进行备份和恢复OCR(10g)
Oracle推荐在对集群调整时,比方添加.删除节点之前,应对OCR进行备份,能够用export备份到指定文件.假设做了replace或者restore等操作,Oracle建议使用cluvfy comp ...
- aspose.cells 模版
aspose.cells 模版 http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html
- mysqlpump 原理
Oracle官方多线程逻辑备份工具 昨天Inside君写到MySQL 5.7.11版本发布,其中最有意义的部分在于官方修复了之前mysqlpump工具一致性备份的问题,使得mysqlpump工具在生产 ...
- qsort函数、sort函数 (精心整理篇)
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...
- Asp.Net 之 Web.config下Authorization节点
WebConfig Authorization 节点 授权的目的是确定是否应该授予某个标识对给定资源请求的访问权限类型.有两种基本方式来授予对给定资源的访问权限: [1] 文件授权 文件授权由 Fil ...
- df 和 du 命令详解
df命令详细用法 a:显示全部的档案系统和各分割区的磁盘使用情形 i:显示i -nodes的使用量 k:大小用k来表示 (默认值) t:显示某一个档案系统的所有分割区磁盘使用量 x:显示不是某一个档案 ...
- CCTableView的使用和注意事项
#include "cocos-ext.h" using namespace cocos2d::extension; class TableViewTestLayer: publi ...
- Autowired properities class
1. Properties类 @ConfigurationProperties(locations = "classpath:build.properties") @JsonInc ...
- python(6)-执行shell命令
可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen* --废弃 popen2.* --废弃 commands.* ...