스프링

import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;


//aspectjweaver(1.6.11)와 aspectjrt(1.6.8)  을 mavenrepository 에서 검색해서 pom.xl 넣는다.
//******************* 중요
/*       <!-- aop의 설정을 통한 자동적인 Proxy 객체 설정 --> -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
        <context:component-scan base-package="com.example.wbe04.*" />
//<aop:config> 추가    - 네임스페이스 aop 체크    
//component 스켄 전에 붙여야 한다.    안되면 root-context.xml 과 servlet-context.xml 양쪽 모두 맨 상단위에 추가

* */
//종단 관심 - 핵심적인 비즈니스 로직
//횡단 관심(Aspect) -공통적인 로직
//Advice - 횡단관심을 모든 코드에서 처리하지 않고 공통적으로 지원할수 있도록
//처리하는 코드 
//@Controller 컨트롤빈
//@Service 서비스빈
//@Repository dao빈
//@Component 기타빈

@Component //스프링에서 관리하는 빈
@Aspect //스프링에서 관리하는 aop bean

public class LogAdvice {
//포인트 컷(실행시점) -Before-실행 전, After -실행 후 , Around -실행 전후
//컨트롤러의 모든 method 실행 전후에 logPrint method 가 호출됨    

//    controller.. 점 두개의미는 controller 다음 모든 패키지 -> xxxController 클래스 의 .* 의미는  모든 메소드 
    @Around(
     "execution(* com.example.wbe04.controller..*Controller.*(..))"
     + " or execution( * com.example.wbe04.service..*Impl.*(..))"
     + " or execution( * com.example.wbe04.model..dao.*Impl.*(..))")

    public Object logPrint(ProceedingJoinPoint joinpoint) throws Throwable{
        
        //시스템의 현재시각
        long startTime =System.currentTimeMillis();
        Object result =joinpoint.proceed();
        
        //클래스 이름 
        String type =joinpoint.getSignature().getDeclaringTypeName();
        //method 이름
        String method=joinpoint.getSignature().getName();
        //매개 변수
        String args =Arrays.toString(joinpoint.getArgs());
        System.out.println("클래스 :"+ type);
        System.out.println("method :"+ method);
        System.out.println("매개변수 :"  + args);
        
        long endTime =System.currentTimeMillis();
        System.out.println("실행시간 : " + (endTime-startTime));
        
        //return joinpoint.proceed();// 컨트롤러의 method가 실행됨 -- 현재는 before로 적용 
        //만약joinpoint.proceed() 중간에 있으면 Around 적용

        System.out.println("=====================구분선========================================");
        return result;
    }
    
}


 

 

about author

PHRASE

Level 60  머나먼나라

가마 타고 시집가기는 다 틀렸다 , 일이 잘못되어 제대로의 격식을 차릴 수 없게 되었다는 말.

댓글 ( 4)

댓글 남기기

작성

스프링 목록    more