[size=x-large][b]基于Schema的AOP编程[/b][/size]
基于AspectJ的AOP编程已经可以满足我们的编程需要,为什么这里还要做一套基于Schema的逻辑呢,这里有两个理由:
1.Java语言直到5.0才支持注解功能,所以在5.0之前的版本如果也想体验到AspectJ的便利,就需要使用特殊的方法。
2.AspectJ无法针对切面Advisor编程,但是Schema可以。
[size=large]前置增强:[/size]
测试代码:
输出:
[color=blue]
--how are you--
NaiveWaiter.greet to John
NaughtyWaiter: greet to John
[/color]
我们并没有使用注解或者AspectJ的相关功能,只是在XML配置类相关的Bean和<aop:config>,也达到了相同的效果。
[size=large]后置增强:[/size]
可以看到我们依然可以在配置文件中绑定返回值。
[size=large]环绕增强:[/size]
环绕增强可以绑定连接点信息
[size=large]抛出异常增强:[/size]
这里绑定类异常信息。
[size=large]Final增强:[/size]
无论是否有异常都会执行。
[size=large]引介增强:[/size]
[size=large]增强绑定参数:[/size]
输出:
[color=blue]
-----bindParams-----
name:John
num:2
-----bindParams-----
NaiveWaiter.smile to John 2 times
[/color]
基于AspectJ的AOP编程已经可以满足我们的编程需要,为什么这里还要做一套基于Schema的逻辑呢,这里有两个理由:
1.Java语言直到5.0才支持注解功能,所以在5.0之前的版本如果也想体验到AspectJ的便利,就需要使用特殊的方法。
2.AspectJ无法针对切面Advisor编程,但是Schema可以。
[size=large]前置增强:[/size]
public class AdviceMethods {
public void preGreeting() {
System.out.println("--how are you--");
}
}
<bean id="naiveWaiter" class="com.firethewhole.maventest08.NaiveWaiter"/>
<bean id="naughtyWaiter" class="com.firethewhole.maventest08.NaughtyWaiter"/>
<bean id="adviceMethods" class="com.firethewhole.maventest08.schema.AdviceMethods"/>
<bean id="smartSeller" class="com.firethewhole.maventest08.SmartSeller"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="adviceMethods">
<aop:pointcut expression="target(com.firethewhole.maventest08.NaiveWaiter) and execution(* greetTo(..))" id="greetToPointcut"/>
<aop:before method="preGreeting" pointcut-ref="greetToPointcut"/>
</aop:aspect>
</aop:config>
测试代码:
public class SchemaProxyTest {
public static void main(String[] args) {
String configLocation = "com/firethewhole/maventest08/schema/beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
Waiter naughtyWaiter = (Waiter) ctx.getBean("naughtyWaiter");
naiveWaiter.greetTo("John");
naughtyWaiter.greetTo("John");
}
}
输出:
[color=blue]
--how are you--
NaiveWaiter.greet to John
NaughtyWaiter: greet to John
[/color]
我们并没有使用注解或者AspectJ的相关功能,只是在XML配置类相关的Bean和<aop:config>,也达到了相同的效果。
[size=large]后置增强:[/size]
<aop:after-returning method="afterReturning" pointcut="target(com.firethewhole.maventest08.SmartSeller)" returning="retVal"/>
可以看到我们依然可以在配置文件中绑定返回值。
[size=large]环绕增强:[/size]
<aop:around method="aroundMethod" pointcut="execution(* serveTo(..)) and target(com.firethewhole.maventest08.Waiter)"/>
环绕增强可以绑定连接点信息
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("开始进入方法:" + pjp.getTarget().getClass());
System.out.println("参数为:" + pjp.getArgs()[0]);
pjp.proceed();
System.out.println("开始退出方法:" + pjp.getTarget().getClass());
}
[size=large]抛出异常增强:[/size]
<aop:after-throwing method="afterThrowingMethod" pointcut="execution(* checkBill(..)) and target(com.firethewhole.maventest08.SmartSeller)" throwing="iae"/>
public void afterThrowingMethod(IllegalArgumentException iae) {
System.out.println("抛出异常:" + iae.getMessage());
}
这里绑定类异常信息。
[size=large]Final增强:[/size]
<aop:after method="afterMethod" pointcut="execution(* com..*.Waiter.greetTo(..))"/>
无论是否有异常都会执行。
[size=large]引介增强:[/size]
<aop:declare-parents types-matching="com.firethewhole.maventest08.Waiter+" implement-interface="com.firethewhole.maventest08.Seller" default-impl="com.firethewhole.maventest08.SmartSeller"/>
[size=large]增强绑定参数:[/size]
<aop:before method="bindParams" pointcut="target(com.firethewhole.maventest08.NaiveWaiter) and args(name,num,..)"/>
public void bindParams(int num, String name) {
System.out.println("-----bindParams-----");
System.out.println("name:" + name);
System.out.println("num:" + num);
System.out.println("-----bindParams-----");
}
public class SchemaProxyTest {
public static void main(String[] args) {
String configLocation = "com/firethewhole/maventest08/schema/beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
((NaiveWaiter)naiveWaiter).smile("John", 2);
}
}
输出:
[color=blue]
-----bindParams-----
name:John
num:2
-----bindParams-----
NaiveWaiter.smile to John 2 times
[/color]