public class Waiter { public void check(String name){ System.out.println("waiter说:结帐?"+name); } public void serve(String name){ System.out.println("waiter说:要点什么?"+name); } }
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class PreGreeting { @Before("execution(* serve(..))") public void beforeGreeting(){ System.out.println("how are you!"); } }
@Before注解表示前置加强,后面的切点表达式表示在目标类的serve()方法织入加强,serve()方法能够带任意的传入参数和任意的返回值。
PreGreeting类经过注解,将切点,加强类型和加强的横切逻辑组合在一块儿。PreGreeting类至关于上一篇中的BeforeAdvice(加强)
,NameMatchMethodPointcut(切点)
,DefaultPointcutAdvisor(切面)
三者联合表达的信息。java
Waiter waiter=new Waiter(); AspectJProxyFactory factory=new AspectJProxyFactory(); factory.setTarget(waiter);//设置目标类 factory.addAspect(PreGreeting.class);//添加切面 Waiter proxy=factory.getProxy(); proxy.serve("TheViper"); proxy.check("TheViper");
how are you! waiter说:要点什么?TheViper waiter说:结帐?TheViper
<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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id='waiter' class='aspectj.Waiter'/> <bean class='aspectj.PreGreeting'/> </beans>
引用aop命名空间spring
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="... http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
<aop:aspectj-autoproxy/>
自动为spring容器中那些匹配@AspectJ切面的Bean建立代理,完成切面织入express
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); Waiter waiter=(Waiter)ctx.getBean("waiter"); waiter.serve("TheViper"); waiter.check("TheViper");
使用的时候,直接获取bean操做就能够了,不用像前面还要建立AspectJProxyFactory,设置好代理才能用。编程
须要引入aspectjweaversegmentfault
public class GreetBefore { public void say(String name){ System.out.println("hallo!"+name); } }
加强不用像上一篇中那样,须要继承特定类或实现特定接口。app
<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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id='waiterTarget' class='com.Waiter'/> <bean id='greetBefore' class='schema.GreetBefore'/> <aop:config> <aop:pointcut expression="target(com.Waiter) and execution(* check(..)) and args(name,..)" id="servePointcut"/> <aop:aspect ref="greetBefore"> <aop:before method="say" pointcut-ref="servePointcut"/> </aop:aspect> </aop:config> </beans>
匹配Waiter类中的check()方法。代理
须要引入aop命名空间
code
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="... http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
<aop:aspect>
定义切面,其内部能够定义多个加强,ref属性指定引用的具体加强xml
<aop:before>
声明了一个前置加强,经过pointcut-ref属性引用切点,method属性指定加强中哪一个方法用于具体的加强行为对象
<aop:pointcut>
定义切点,expression属性设置切点表达式,其语法和@AspectJ中的语法彻底相同。
public class Waiter { public void check(String name){ System.out.println("waiter说:结帐?"+name); } public void serve(String name){ System.out.println("waiter说:要点什么?"+name); } }
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); Waiter waiter=(Waiter)ctx.getBean("waiterTarget"); waiter.serve("TheViper"); waiter.check("TheViper");
waiter说:要点什么?TheViper hallo!TheViper waiter说:结帐?TheViper
命名切点只能被当前<aop:aspect>内定义的元素访问到,<aop:config>中的配置顺序必须是<aop:pointcut>=><aop:advisor>=><aop:aspect>
<aop:config> ... <aop:aspect ref="greetBefore"> <aop:after-returning method="sayEnjoy" pointcut='target(com.Waiter) and execution(* serve(..))'/> </aop:aspect> </aop:config>
<aop:config> ... <aop:aspect ref="greetBefore"> <aop:around method="aroundMethod" pointcut='execution(* serve(..)) and within(com.Waiter))'/> </aop:aspect> </aop:config>
public void aroundMethod(ProceedingJoinPoint pjp){ //pjp能够访问到环绕加强的链接点信息 ... }
<aop:config> ... <aop:aspect ref="greetBefore"> <aop:after-throwing method="afterThrowingMethod" pointcut='target(com.Waiter) and execution(* serve(..))' throwing='exception'/> </aop:aspect> </aop:config>
public void afterThrowingMethod(Exception exception){ ... }
throwing属性声明须要绑定的异常对象,指定的异常名必须和加强方法对应的传入参数一致
<aop:config> ... <aop:aspect ref="greetBefore"> <aop:after method="afterMethod" pointcut='execution(*com.Waiter. serve(..))'/> </aop:aspect> </aop:config>