최신글
hyeonga_code
Spring_AOP 심화 실습_ 3. XML 설정 이용하기 본문
반응형
- XML 스키마를 활용하여 AOP를 구현하는 과정
- 공통 기능을 제공하는 Advice 클래스를 구현합니다.
- XML 설정 파일에서 <aop:config> 태그를 이용하여 Aspect를 설정합니다.
- Advice를 어떤 Pointcut에 적용할 지 결정합니다.
- 공통 기능을 제공할 Advice 클래스를 작성합니다.
- src/main/java 폴더에 sts.spring.common 패키지를 생성합니다.
- sts.spring.common > ProfilingAdvice.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
|
package sts.spring.common;
import org.aspectj.lang.ProceedingJoinPoint;
public class ProfilingAdvice {
// 프로파일링 할 대상 메소드가 호출될 때마다 실행되는 메소드입니다.(실행 시간 측정, 출력)
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(">> " + signatureString + " time : " + (finish - start) + "ms");
}
}
}
|
- 스프링 설정 파일을 작성합니다.
- new > source folder > src/main/resources 생성하기
- src/main/resources > applicationContextOne.xml 'Spring Bean Configuration File' 생성하기
- applicationContextOne.xml 파일의 Namespaces에서 beans, aop 네임스페이스 체크하기
=====
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
|
<?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">
<!-- AspectJ를 사용한 프로파일링 적용 -->
<!-- 프로파일링 advice인 ProfilingAdvice를 빈으로 등록합니다. -->
<bean id="performanceTraceAdvice" class="sts.spring.common.ProfilingAdvice" />
<!-- AOP 설정을 담는 엘리먼트입니다. -->
<aop:config>
<!-- publicMethod 포인트 컷은 sts.spring.board.service 패키지의 모든 public 메소드를 대상으로 합니다. -->
<aop:aspect id="traceAspect1" ref="performanceTraceAdvice">
<aop:pointcut id="publicMethod" expression="execution(public * sts.spring.board.service..*(..))" />
<aop:around pointcut-ref="publicMethod" method="trace" />
</aop:aspect>
<!-- aspect는 sts.spring.member.service 패키지에 있는 모든 public 메소드를 대상으로 합니다. -->
<aop:aspect id="traceAspect2" ref="performanceTraceAdvice">
<aop:around pointcut="execution(public * sts.spring.member.service..*(..))" method="trace" />
</aop:aspect>
</aop:config>
<!-- 게시글을 등록하는 빈을 등록합니다. -->
<bean id="writeArticleService" class="sts.spring.board.service.WriteArticleServiceImpl">
<constructor-arg><ref bean="articleDAO" /></constructor-arg>
</bean>
<!-- 게시글을 관리하는 빈을 등록합니다. -->
<bean id="articleDAO" class="sts.spring.board.service.OracleArticleDAO" />
<!-- 회원 정보를 관리하는 빈을 등록합니다. -->
<bean id="memberService" class="sts.spring.member.service.MemberServiceImpl" />
</beans>
|
- 테스트할 메인 클래스 작성하기
- src/main/java 폴더에 sts.spring.board.controller 패키지 생성하기
- sts.spring.board.controller > MainOne.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
41
42
|
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 MainOne {
public static void main(String[] args) {
// 스프링 컨테이너를 생성합니다.
String[] configLocations = new String[] { "applicationContextOne.xml" };
AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
// 의존성 주입
// 빈으로 등록되어 있는 writeArticleService를 사용합니다.
WriteArticleService articleService = (WriteArticleService)context.getBean("writeArticleService");
articleService.write(new ArticleVO());
// 빈으로 등록되어 있는 memberService를 사용합니다.
MemberService memberService = context.getBean("memberService", MemberService.class);
memberService.regist(new MemberVO());
// 스프링 컨테이너 종료시 반환합니다.
context.close();
/*
> WriteArticleService.write(..) start
+ WriteArticleServiceImpl.write()
+ MyOracleArticleDAO.insert()
> WriteArticleService.write(..) end
>> WriteArticleService.write(..) time : 0ms
> MemberService.regist(..) start
+ MemberServiceImpl.regist()
> MemberService.regist(..) end
>> MemberService.regist(..) time : 0ms
*/
}
}
|
반응형
'Spring' 카테고리의 다른 글
Spring_AOP 심화 실습_5. 어노테이션 활용하기 (0) | 2024.01.09 |
---|---|
Spring_AOP 심화 실습_4. 다양한 AOP 실습 (1) | 2024.01.09 |
Spring_AOP 심화 실습_메이븐 프로젝트_2. 기본 구조 작성하기 (0) | 2024.01.08 |
Spring_AOP 심화 실습_ 1. 기본 설정 (0) | 2024.01.08 |
Spring_의존성 주입_어노테이션 기반 설정 (0) | 2023.12.17 |