Spring的AOP實現是經過集成AspectJ框架實現的。express
想必這句話你們都知道了吧,不知道也不要緊,最起碼你如今知道了。框架
四種實現方案,接下來咱們一一去揭開它的神祕的面紗.......spa
第一種(僞代碼的形式)xml
經典AOP形式的:it
<bean id="userDao" class="UserDaoImpl"></bean>io
<bean id="service" class="UserServiceImpl">class
<property name="dao" ref="userDao"></property>配置
</bean>aop
<bean id="myBefore" class="MyBeforeAdvice"></bean>service
<bean id="myAfter" class="MyAfterAdvice"></bean>
<aop:config>
<aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>
<aop:advisor pointcut-ref="point" advice-ref="myBefore"></aop:advisor>
<aop:advisor pointcut-ref="point" advice-ref="myAfter"></aop:advisor>
</aop:config>
第二種(僞代碼形式)
使用註解方式
@Aspect
public class MyBeforeAspect{
@Before(value="execution(* *..biz.*.*(..))")
public void before(){
System.out.println("====before===");
}
@AfterReturning(value="execution(* *..biz.*.*(..))")
public void after(){
System.out.println("=====after=====");
}
}
<bean id="stuDao" class="StudentDaoImpl"></bean>
<bean id="stuService" class="StudentService">
<property name="dao" ref="stuDao">
</bean>
<!-- 準備aop:aspectj-autoproxy-->
<bean class="MyBeforeAspect"></bean>
<aop:aspectj-autoproxy />
第三種(僞代碼形式)
使用AspectJ基於xml形式
public class MyAspectJMethod{
public void myBefore(){
System.out.println("AspectJ=========before");
}
public void myAfter(){
System.out.println("AspectJ==========after");
}
}
<bean id="stuDao" class="StudentDaoImpl"></bean>
<bean id="stuBiz" class="StudentBizImpl"></bean>
<bean id="myAspectJ" class="MyAspectJMethod">
<aop:config>
<aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>
<aop:aspect ref="myAspectJ">
<aop:before method="myBefore" ponitcut-ref="point"></aop:before>
<aop:after method="myAfter" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
上面配置<aop:pointcut>節點也能夠放到<aop:aspect>節點內,以下:
<aop:config>
<aop:aspect ref="myAspectJ">
<aop:pointcut id="point" expression="execution(* *..biz.*.*(..))"></aop:pointcut>
<aop:before method="myBefore" ponitcut-ref="point"></aop:before>
<aop:after method="myAfter" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
上述兩種配置均可以實現該功能。。。。。