Spring AOP 的三种配置方法

Spring AOP 的三种配置方法  

2012-03-18 01:11:48|  分类: Spring |  标签:spring   |字号 订阅

Class Files:

public interface Work {

         public void doWork(String username);
}

public class WorkImpl implements Work {

          @Override
          public void doWork(String username) {
                   System.out.println(username + " 正在工作...");
                   //  int i = 5/0;
          }
}

public class LogBeforeAdvice implements MethodBeforeAdvice {

           @Override
           public void before(Method method, Object[] parameters, Object target) throws Throwable {
                     System.out.println(parameters[0] + " 开始工作...");
          }
}

public class LogAfterReturnAdvice implements AfterReturningAdvice
          @Override
          public void afterReturning(Object returnValue, Method method, Object[] parameters, Object target) throws Throwable {
                   System.out.println(parameters[0] + " 结束工作...");
          }
}

public class LogExceptionAdvice implements ThrowsAdvice
          public void afterThrowing(Method method, Object[] parameters, Object target, Exception ex){
                     System.out.println(parameters[0] + " 工作中出现异常... ", ex);
          }
}

public class LogAroundAdvice implements MethodInterceptor {

           @Override
           public Object invoke(MethodInvocation arg0) throws Throwable {
                      System.out.println(arg0.getArguments()[0] + " 工作中,请勿打扰...");
                      return arg0.proceed();
           }
}

XML配置文件

第一种方法

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
          <!-- Target Object -->
          <bean id="work" class="com.demo.aop.WorkImpl"></bean>
 
         <!-- define Advice -->
         <bean id="logBeforeAdvice" class="com.demo.aop.LogBeforeAdvice"></bean>
         <bean id="logAfterReturnAdvice" class="com.demo.aop.LogAfterReturnAdvice"></bean>
         <bean id="logExceptionAdvice" class="com.demo.aop.LogExceptionAdvice"></bean>
 
          <!-- define Advisor -->
          <bean id="logBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                   <property name="advice" ref="LogBeforeAdvice"/> <!--  logBeforeAdvice 必须实现Advice接口-->
                   <property name="patterns" value=".*doWork"/>
          </bean>
 
         <bean id="logAfterReturnAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                  <property name="advice" ref="logAfterReturnAdvice"/> <!--  logAfterAdvice 必须实现Advice接口-->
                  <property name="patterns" value=".*doWork"/>
         </bean>

         <bean id="logExceptionAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
                 <property name="advice" ref="logExceptionAdvice"/> <!--  logExceptionAdvice 必须实现Advice接口-->
                 <property name="patterns" value=".*doWork"/>
         </bean>
 
         <!-- Proxy Object -->
         <!-- <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
                         <property name="proxyInterfaces">
                                  <list>
                                        <value>com.demo.aop.Work</value>
                                 </list>
                        </property>
                        <property name="target" ref="work"></property>
                        <property name="interceptorNames">
                          <list>
                                <value>logBeforeAdvisor</value>
                                <value>logAfterReturnAdvisor</value>
                                <value>logExceptionAdvisor</value>
                        </list>
                      </property>
               </bean> -->

               <bean id="beanNameAutoProxyCreator"  
         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
           <list>
              <value>work</value>
              <value>*Service</value>
              <value>*DAO</value>
           </list>
        </property>
        <property name="interceptorNames">
           <list>
             <value>logBeforeAdvisor</value>
             <value>logAfterReturnAdvisor</value>
             <value>logExceptionAdvisor</value>
             <value>*Advisor</value>
           </list>
        </property>
     </bean>

   <!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> -->
</beans>

第二种方法

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
          <!-- Target Object -->
          <bean id="work" class="com.demo.aop.WorkImpl"></bean>
 
          <!-- define Advice -->
          <bean id="logBeforeAdvice" class="com.demo.aop.LogBeforeAdvice"></bean>
          <bean id="logAfterReturnAdvice" class="com.demo.aop.LogAfterReturnAdvice"></bean>
          <bean id="logExceptionAdvice" class="com.demo.aop.LogExceptionAdvice"></bean>
 
          <aop:config>
                <aop:pointcut id="pointcut" expression="execution(* com.demo.aop.*.doWork(..))" />
                <aop:advisor advice-ref="logBeforeAdvice" pointcut-ref="pointcut"/>
                <aop:advisor advice-ref="logAfterReturnAdvice" pointcut-ref="pointcut"/>
                <aop:advisor advice-ref="logExceptionAdvice" pointcut-ref="pointcut"/>
          </aop:config>
</beans>

第三种方法

//定义一个切面类

public class LogAspect { 
          public void before(JoinPoint joinPoint) {
                     System.out.println(joinPoint.getArgs()[0] + " 开始工作...");
          }
 
          public void afterReturning (JoinPoint joinPoint, Object retValue) {
                   System.out.println(joinPoint.getArgs()[0] + " 结束工作...");
          }
 
          public void afterThrowing (JoinPoint joinPoint, Exception ex) {
                   System.out.println(joinPoint.getArgs()[0] + " 工作中出现异常... " + ex);
          }
 
          public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
                   System.out.println(joinPoint.getArgs()[0] + " 工作中,请勿打扰...");
                   return joinPoint.proceed();
          }
 
          public void after (JoinPoint joinPoint) {
                 System.out.println(joinPoint.getArgs()[0] + " 回家...");
          }
}

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
         <bean id="work" class="com.demo.aop.WorkImpl"></bean>

         <bean id="logAspect" class="com.demo.aop.LogAspect"/>


          <aop:config>
                  <aop:pointcut id="pointcut" expression="execution(* com.demo.aop.*.doWork(..))" />
                  <aop:aspect id="aspect" ref="logAspect">
                          <aop:before pointcut-ref="pointCut" method="before"/>
                          <aop:after-returning pointcut-ref="pointcut" method="afterReturning" returning="retValue"/>
                          <aop:after-throwing pointcut-ref="pointcut" method="afterThrowing" throwing="ex"/>
                          <aop:after pointcut-ref="pointcut" method="after"/>
                          <aop:around pointcut-ref="pointcut" method="around"/>
                 </aop:aspect>
          </aop:config>
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值