由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施。完成学习的小例子。

 

关于spring-Aop原理:http://m.oschina.net/blog/174838这篇文章写的非常好。

 

 

个人觉着可能上线的时候配置文件更方便一下。所以样例主要是配置文件方式

Demo文件下载地址:

http://download.csdn.net/detail/ruishenh/7261121

 

 

Spring配置文件

/idle-service-impl/src/main/resources/spring/app-config.xml

 


 

导入的aop-advisor.xml文件

 

/idle-service-impl/src/main/resources/spring/aop-advisor.xml

 


 

 

关于这个类

com.ruishenh.aop.aspect.advisor.GenericAdvisor中方法有

 

1.  before  相应Target运行之前

2.  heartbeat这个是围绕的一个方法

3.  after  相应target运行之后

4.  afterReturning 相应在target处理后结果返回增强处理

5.        handlerException 相应在target运行异常时增强处理

 

 

/idle-service-impl/src/main/java/com/ruishenh/aop/aspect/advisor/GenericAdvisor.java源代码

 

package com.ruishenh.aop.aspect.advisor;
 
import org.aspectj.lang.JoinPoint;
importorg.aspectj.lang.ProceedingJoinPoint;
 
import com.ruishenh.domain.account.AccountBank;
 
public class GenericAdvisor {
 
         /**
          * 对于要增强的方法。运行围绕处理检查心跳是否正常
          * @param joinPoint
          * @return
          * @throws Throwable
          */
         Objectheartbeat(ProceedingJoinPoint joinPoint) throws Throwable{
                  
//               if(checkHeartbeat()) {
//                        
//               }
                   System.out.println("2joinPoint.Signature.name:"+joinPoint.getSignature().getName());
                   //记得在调用运行方法的时候异常不要try-catch,要thrwos出去,不然可能事务层没法起效,或者增强异常处理也无法起效
                   Objectobj=joinPoint.proceed();
                   //下边的參入參数能够改动。可是类型和方法的个数要和原来一致,不然原方法无法运行
//               Objectobj=joinPoint.proceed(joinPoint.getArgs());
                  
                   //对于返回后对象,有可能要做处理,对返回參数的某一些值处理,
                   //能够通过org.springframework.beans.BeanUtils.copyProperties把一些值赋值
                   if(obj==null) {
                            returnnew AccountBank();
                   }
                   returnobj;
         }
         /**
          * 对于要增强的方法,在方法之前运行
          * @param joinPoint 连接点信息
          */
         voidbefore(JoinPoint joinPoint){
                   Object[] objs=joinPoint.getArgs();
                   System.out.println("1objs:"+objs[0]);
                   System.out.println("1joinPoint:"+joinPoint);
         };
        
         /**
          * 对于要增强的方法,在方法之后运行
          * @param joinPoint  连接点信息
          */
         voidafter(JoinPoint joinPoint){
                   Object[] objs=joinPoint.getArgs();
                   System.out.println("4objs:"+objs[0]);
                   System.out.println("4joinPoint:"+joinPoint);
         };
         /**
          * 对于要添加的方法,方法返回结果后。对结果进行记录或者分析
          * 方法
          * @param obj target运行后返回的结果
          */
         voidafterReturning(Object obj){
                   System.out.println("3obj:"+obj);
         };
         /**
          * 对于要增强的方法,方法抛出异常后。对异常的增强处理,比方记录异常。或者依据异常分析数据什么的
          * @param e target运行后抛出的异常
          */
         voidhandlerException(Throwable e){
                   System.out.println("handingException......");
                   e.printStackTrace();
         }
        
         booleancheckHeartbeat(){
                   returntrue;
         }
}


 

 

 

 

Junit測试

 

/idle-service-impl/src/test/java/com/ruishenh/business/impl/account/AccountServiceImplTest.java

 

package com.ruishenh.business.impl.account;
 
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
importorg.springframework.test.context.ContextConfiguration;
importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
importcom.ruishenh.domain.account.AccountBank;
importcom.ruishenh.domain.account.AccountBankParam;
import com.ruishenh.utils.SpringFactoryUtil;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/spring/app-config.xml"})
public class AccountServiceImplTest {
 
         @Before
         publicvoid setUp() throws Exception {
         }
 
         @Test
         publicvoid testStoreIn() throws Exception {
                   AccountServiceImplimpl= SpringFactoryUtil.getBean(AccountServiceImpl.class);
                   AccountBankParamparam=   new AccountBankParam();
                   param.setId(100);
                   AccountBankab=impl.storeIn(param);
                   System.out.println(ab.toString());
         }
 
}
 

 

 

运行后输出结果:

 

1objs:com.ruishenh.domain.account.AccountBankParam@d522de2[id=100,name=<null>,account=<null>]

1joinPoint:execution(AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn(AccountBankParam))

2joinPoint.Signature.name:storeIn

==============todo=====================

4objs:com.ruishenh.domain.account.AccountBankParam@d522de2[id=100,name=<null>,account=<null>]

4joinPoint:execution(AccountBankcom.ruishenh.business.impl.account.AccountServiceImpl.storeIn(AccountBankParam))

3obj:com.ruishenh.domain.account.AccountBank@2d397e5c[id=0,name=<null>,account=<null>]

com.ruishenh.domain.account.AccountBank@2d397e5c[id=0,name=<null>,account=<null>]

版权声明:本文博主原创文章,博客,未经同意不得转载。

相关文章:

  • 2021-11-04
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2021-10-04
  • 2022-01-19
  • 2021-07-16
  • 2022-12-23
猜你喜欢
  • 2021-06-03
  • 2021-08-23
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2021-11-01
  • 2021-10-02
相关资源
相似解决方案