1. pom.xml
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
2. servelt-context.xml aspectJ설정
<!-- @Aspect어노테이션이 있는 클래스를 자동으로 찾아서 설정해줌 -->
<aop:aspectj-autoproxy/>
3. Annotation Interface 설정
package secretary.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
4. Aspectj으로 Annotation 구현
package secretary.common.aspect;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
@Component
@Aspect
public class LogAspect {
Logger log = LogManager.getLogger(this.getClass());
@Around("@annotation(secretary.common.annotation.LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable{
StopWatch stopWatch = new StopWatch();
stopWatch.start();
log.debug("시작================>");
Object proceed = joinPoint.proceed();
stopWatch.stop();
log.debug("끝================>"+stopWatch.prettyPrint());
System.out.println("annotation======>");
return proceed;
}
}
5. Controller에 Annotation 설정
package secretary.common.controller;
import java.util.Locale;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import secretary.common.annotation.LogExecutionTime;
import secretary.common.service.TestService;
@Controller
public class MainController {
private Logger log = LogManager.getLogger(this.getClass());
@Autowired
private TestService testService;
@RequestMapping(value = "/main.do")
@LogExecutionTime
public String main(Locale locale, Model model) {
String result = testService.selectNow();
log.info("=======>main: " + result);
return "common/main";
}
}
'개발 > 개발환경설정' 카테고리의 다른 글
Filter 설정 (0) | 2020.03.18 |
---|---|
Interceptor 설정 (0) | 2020.03.17 |
LOG4J 설정 (0) | 2020.03.17 |
MySQL 연결 (0) | 2020.03.16 |
Spring MVC 설정 (0) | 2020.03.12 |