@AspectJ可以使用切点函数定义切点,我们还可以使用逻辑运算符对切点进行复核运算得到复合的切点,为了在切面中重用切点,我们还可以对切点进行命名,以便在其他的地方引用定义过的切点。
当一个连接点匹配多个切点时,需要考虑织入顺序的问题,此外一个重要的问题是如何再增强中访问连接点上下文的信息。
Waiter接口:
package com.yyq.aspectJAdvanced;
public interface Waiter {
void greetTo(String name);
void serveTo(String name);
}
NaiveWaiter实现类:
package com.yyq.aspectJAdvanced;
public class NaiveWaiter implements Waiter {
@Override
public void greetTo(String name) {
System.out.println("NaiveWaiter:greet to " + name + "...");
}
@Override
public void serveTo(String name) {
System.out.println("NaiveWaiter:serving to " + name + "...");
}
public void smile(String clientName,int times){
System.out.println("NaiveWaiter:smile to "+clientName+ times+"times...");
}
}
NaughtyWaiter实现类:
package com.yyq.aspectJAdvanced;
public class NaughtyWaiter implements Waiter {
public void greetTo(String clientName) {
System.out.println("NaughtyWaiter:greet to " + clientName + "...");
}
public void serveTo(String clientName) {
System.out.println("NaughtyWaiter:serving " + clientName + "...");
}
public void joke(String clientName, int times) {
System.out.println("NaughtyWaiter:play " + times + " jokes to " + clientName + "...");
}
}
Seller接口:
package com.yyq.aspectJAdvanced;
public interface Seller {
int sell(String goods, String clientName);
}
SmallSeller实现类:
package com.yyq.aspectJAdvanced;
public class SmartSeller implements Seller {
public int sell(String goods,String clientName) {
System.out.println("SmartSeller: sell "+goods +" to "+clientName+"...");
return 100;
}
public void checkBill(int billId){
if(billId == 1) throw new IllegalArgumentException("iae Exception");
else throw new RuntimeException("re Exception");
}
}
beans.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="naiveWaiter" class="com.yyq.aspectJAdvanced.NaiveWaiter"/>
<bean id="naughtyWaiter" class="com.yyq.aspectJAdvanced.NaughtyWaiter"/>
<bean id="seller" class="com.yyq.aspectJAdvanced.SmartSeller"/>
<!--
<bean class="com.yyq.aspectJAdvanced.TestAspect"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect2"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect3"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect4"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect5"/>
<bean id="naiveWaiter2" class="com.yyq.aspectJAdvanced.NaiveWaiter2"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect6"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect7"/>
<bean class="com.yyq.aspectJAdvanced.TestAspect8"/>
-->
</beans>
1、切点符合运算
使用切点符合运算符,我们将拥有强大而灵活的切点表达能力。
TestAspect:切点符合运算定义切面
package com.yyq.aspectJAdvanced;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class TestAspect {
//与非运算
@Before("!target(com.yyq.aspectJAdvanced.NaiveWaiter) && execution(* serveTo(..))")
public void notServeInNaiveWaiter(){
System.out.println("--notServeInNaiveWaiter() executed!--");
}
//与运算
@After("within(com.yyq.aspectJAdvanced.*) && execution(* greetTo(..))")
public void greetToFun(){
System.out.println("--greetToFun() executed!--");
}
//或运算
@AfterReturning("target(com.yyq.aspectJAdvanced.Waiter) || target(com.yyq.aspectJAdvanced.Seller)")
public void waiterOrSeller(){
System.out.println("--waiterOrSeller() executed!--");
}
}
测试方法:
@Test
public void pointAspectJTest() {
String configPath = "com\\yyq\\aspectJAdvanced\\beans.xml";
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath);
Waiter naiveWaiter = (Waiter) ctx.getBean("naiveWaiter");
Waiter naughtyWai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