欢迎交流转载: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>