최신글
hyeonga_code
Spring_AOP 심화 실습_5. 어노테이션 활용하기 본문
반응형
- @Aspect 어노테이션 이용한 AOP
- XML 파일에 Advice, Pointcut 등의 설정을 하지 않고 사용할 수 있습니다.
- XML 스키마 기반의 AOP와의 차이점
- @Aspect 어노테이션을 이용하여 Aspect 클래스를 구현합니다.
- Aspect 클래스는 Advice 를 구현한 메소드와 Pointcut을 포함합니다.
- XML 설정에서 <aop:aspectj-autoproxy/> 태그를 설정해야 합니다.
- @Aspect 클래스 작성하기
- sts.spring.common > ProfilingAspect.java 클래스 생성하기
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package sts.spring.common;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
@Aspect
@Order(3) // 우선순위를 지정합니다.
public class ProfilingAspect {
// sts.spring 패키지 내의 모든 Impl 클래스의 모든 public 메소드를 대상으로합니다.
@Pointcut("execution(public * sts.spring..*Impl.*(..))")
private void profileTarget() {}
// 메소드 호출 전후에 추가 동작을 수행하는 것입니다. (실행 시간 측정, 출력)
@Around("profileTarget()")
public Object trace(ProceedingJoinPoint joinPoint) throws Throwable {
// 현재 대상 메소드의 이름을 문자열로 받아옵니다.
String signatureString = joinPoint.getSignature().toShortString();
System.out.println("> " + signatureString + " start");
long start = System.currentTimeMillis();
try {
Object result = joinPoint.proceed();
return result;
} finally {
long finish = System.currentTimeMillis();
System.out.println("> " + signatureString + " end");
System.out.println(">> Time : " + (finish - start) + "ms");
}
}
}
|
- applicationContextThree.xml 파일 생성
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- @Aspect 어노테이션을 가지고 있는 클래스가 자동으로 Aspect 으로 등록됩니다. -->
<aop:aspectj-autoproxy />
<!-- AOP Aspect로 사용되는 클래스를 빈으로 등록합니다. -->
<bean id="performanceTraceAspect" class="sts.spring.common.ProfilingAspect" />
<!-- 게시물 작성 서비스를 나타내는 빈을 등록합니다. 생성 인자로 articleDAO를 주입합니다. -->
<bean id="writeArticleService" class="sts.spring.board.service.WriteArticleServiceImpl">
<constructor-arg><ref bean="articleDAO" /></constructor-arg>
</bean>
<!-- 데이터 베이스 작업을 수행하는 DAO를 나타냅니다. -->
<bean id="articleDAO" class="sts.spring.board.dao.OracleArticleDAO" />
<!-- 회원 서비스를 나타냅니다. -->
<bean id="memberService" class="sts.spring.member.service.MemberServiceImpl" />
</beans>
|
- 테스트 할 실행 파일을 작성합니다.
- sts.spring.board.controller > MainThree.java 클래스 생성
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package sts.spring.board.controller;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sts.spring.board.service.WriteArticleService;
import sts.spring.board.vo.ArticleVO;
import sts.spring.member.vo.MemberService;
import sts.spring.member.vo.MemberVO;
public class MainThree {
public static void main(String[] args) {
String[] configLocations = new String[] { "applicationContextThree.xml" };
AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
// 의존성 주입
// 빈으로 등록되어 있는 writeArticleService를 사용합니다.
WriteArticleService articleService = (WriteArticleService)context.getBean("writeArticleService");
articleService.write(new ArticleVO());
MemberService memberService = context.getBean("memberService", MemberService.class);
memberService.regist(new MemberVO());
context.close();
/*
> WriteArticleService.write(..) start
+ WriteArticleServiceImpl.write()
+ MyOracleArticleDAO.insert()
> WriteArticleService.write(..) end
>> Time : 0ms
> MemberService.regist(..) start
+ MemberServiceImpl.regist()
> MemberService.regist(..) end
>> Time : 0ms
*/
}
}
|
반응형
'Spring' 카테고리의 다른 글
@PostConstruct 어노테이션 (0) | 2024.02.15 |
---|---|
Spring_AOP 심화 실습_@Aspect 어노테이션 활용한 다양한 Advice 적용 (1) | 2024.01.09 |
Spring_AOP 심화 실습_4. 다양한 AOP 실습 (1) | 2024.01.09 |
Spring_AOP 심화 실습_ 3. XML 설정 이용하기 (0) | 2024.01.08 |
Spring_AOP 심화 실습_메이븐 프로젝트_2. 기본 구조 작성하기 (0) | 2024.01.08 |