웹 & 프레임워크

@Aspect로 매개변수 체크를 해보자

늘근이 2014. 5. 25. 22:50

@Aspect

@Component

public class ParameterCheckAspect {

 

                  @Before("execution(* com.gorakgarak.sample2.annotation.GreetingService.*(*))")

                  public void checkNull(JoinPoint joinpoint) {

                                   if(joinpoint.getArgs()[0] == null){

                                                     throw new IllegalArgumentException("매개변수가 null입니다.");

                                   }

                  }

                 

                 

 

}//end public class

 

//이건 아예 에러가 뜨게 만들어준다.

//이 처리가 없다면 그냥 메인에서 null이 출력될것이다


걍 에러뜨기 싫다면


@Aspect

@Component

public class ParameterCheckAspect {

 

                  @Around("execution(* com.gorakgarak.sample2.annotation.GreetingService.*(*))")

                  public Object checkNull(ProceedingJoinPoint joinpoint) {

                                   Object obj = null;

                                   if(joinpoint.getArgs()[0] == null){

                                                     return null;

                                   }

                                   try {

                                                     obj = joinpoint.proceed();

                                   } catch (Throwable e) {

                                                     // TODO Auto-generated catch block

                                                     e.printStackTrace();

                                   }

                                   return obj;

 

                  }