씨네
공부하는 개발자 강씨네
씨네
  • 분류 전체보기 (460)
    • Web (21)
      • HTML (11)
      • CSS (10)
    • JS 관련 (49)
      • JavaScript (27)
      • JQuery (22)
    • TS 관련 (15)
      • TypeScript (15)
    • NodeJS (7)
      • NodeJS (7)
    • 따라하며 배우는 시리즈 (23)
      • NodeJS & ReactJS Basic (23)
      • NodeJS & ReactJS Movie (0)
      • NodeJS & ReactJS Youtube (0)
      • NodeJS & ReactJS ChatBot (0)
    • SPA (14)
      • React (14)
      • Vue (0)
      • Anguler (0)
    • Java 관련 (118)
      • Java (52)
      • JDBC (6)
      • JSP & Servlet (18)
      • Spring Legecy (38)
      • SpringBoot (4)
    • Python (26)
      • Python (20)
      • PyMongo (1)
      • Django (5)
    • Git (24)
      • Github (24)
    • RDB (22)
      • Oracle (21)
      • MySQL (1)
    • NoSQL (5)
      • MongoDB (5)
    • OS (4)
      • Linux (4)
    • 빅데이터 (2)
      • hadoop (2)
    • IDE (20)
      • eclipse (11)
      • VSCODE (4)
      • VisualStudio (1)
      • IntelliJ (1)
      • PyCharm (1)
      • DBeaver (2)
    • Install (3)
      • Tomcat (1)
      • Docker (1)
      • Anaconda (1)
    • 오류&에러 (28)
      • TS (2)
      • NodeJS (7)
      • SQL (8)
      • Java (1)
      • Spring (4)
      • Git (6)
      • 기타 (0)
    • 알고리즘 (67)
      • 수열 (1)
      • 백준(backjoon) (39)
      • Programmers (27)
    • 자격증 (5)
      • SQLD (5)
    • 기타 (2)
    • IT유튜브로 지식쌓기 (2)

공지사항

인기 글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
씨네

공부하는 개발자 강씨네

[Spring] AOP(Aspect Oriented Programming)
Java 관련/Spring Legecy

[Spring] AOP(Aspect Oriented Programming)

2022. 3. 26. 11:44
728x90

 


Spring_18보다 살짝 심화된 내용입니다.

이전 포스팅을 먼저 보고 오시는 것을 권장드립니다.

 

패키지 구조

Student.java

package com.test03;

public interface Student {
	
	void classWork();

}
 

Woman.java

package com.test03;

public class Woman implements Student {

	@Override
	public void classWork() {
		System.out.println("컴퓨터를 켜서 주식본다.");
	}

}
 

Man.java

package com.test03;

public class Man implements Student {

	@Override
	public void classWork() {
		System.out.println("컴퓨터를 켜서 뉴스본다.");
	}

}
 

MyAspect.java

package com.test03;

import org.aspectj.lang.JoinPoint;

public class MyAspect {
	
	public void before(JoinPoint join) {
		System.out.println("출석한다.");
		System.out.println(join.getTarget().getClass());
		System.out.println(join.getSignature().getName());
	}
	
	public void after() {
		System.out.println("집에간다.");
	}

}
 

applicationContext.xml (Namespaces에서 aop 체크해주세요)

<?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">

	<bean id="woman" class="com.test03.Woman" />
	<bean id="man" class="com.test03.Man" />
	<bean id="myAspect" class="com.test03.MyAspect" />
	
	<aop:config>
		<aop:aspect ref="myAspect">
			<!-- 첫번째 *->리턴타입 (..)은 파라미터의 갯수가 몇개여도 상관없다 -->
			<!-- execution(public * com.test03.Woman.classWork(..)) -->
			<!-- Woman.classWork가 호출될때만 before붙여주세요 하는 명령 -->
			<aop:before method="before" pointcut="execution(public void com.test03.Woman.classWork())" />
			<aop:after method="after" pointcut="execution(public void com.test03.Woman.classWork())" />
		</aop:aspect>
	</aop:config>

</beans>
 

MTest.java

package com.test03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MTest {
	
	public static void main(String[] args) {
		ApplicationContext factory = new ClassPathXmlApplicationContext("com/test03/applicationContext.xml");
		
		Student w = factory.getBean("woman", Student.class);
		Student m = (Student) factory.getBean("man");
		
		System.out.println("여학생 입장");
		w.classWork();
		System.out.println("---------");
		System.out.println("남학생입장");
		m.classWork();
	}

}
 

실행결과

 

 

이번 코드에서도 이전 코드와 매우 비슷합니다.

Man과 Woman은 주 관심사항(cc)이고 MyAspect는 공통관심사항(ccc)입니다.

 

<aop:config>
	<aop:aspect ref="myAspect">
		<aop:before method="before" pointcut="execution(public void com.test03.Woman.classWork())" />
		<aop:after method="after" pointcut="execution(public void com.test03.Woman.classWork())" />
	</aop:aspect>
</aop:config>
 

xml의 다음 코드에서 myAspect객체를 연결했는데 누구한테 연결을 할지에 관한 코드입니다.

execution(public void com.test03.Woman.classWork())의 경우

execution(public * *(..))이라고 할 경우 모든 리턴타입의 모든 메서드에 연결하게됩니다.

하지만 위의 코드에서는 before, after 모두 Woman클래스의 classWork()메서드와 연결이 됩니다.

 

이클리스에서 보면 좌츶에 화살표로 표시가 되어있는데 연결이 되었다는 표시입니다.

MyAspect클래스와 Woman클래스에서 화살표가 있네요!

 

Woman에만 연결이 되어있어서 Man의 m을 호출할때는 proxy 객체가 만들어지지 않게됩니다

 

728x90

'Java 관련 > Spring Legecy' 카테고리의 다른 글

[Spring] AOP(@Component)  (0) 2022.03.28
[Spring] AOP(before, after, after-returning, after-throwing, around)  (0) 2022.03.27
[Spring] AOP(joinpoin, pointcut, advice, aspect, weaving)  (0) 2022.03.25
[Spring] AOP(cc / ccc) 관점 지향 프로그래밍  (0) 2022.03.24
[Spring] Annotation(@Component)  (0) 2022.03.23
    'Java 관련/Spring Legecy' 카테고리의 다른 글
    • [Spring] AOP(@Component)
    • [Spring] AOP(before, after, after-returning, after-throwing, around)
    • [Spring] AOP(joinpoin, pointcut, advice, aspect, weaving)
    • [Spring] AOP(cc / ccc) 관점 지향 프로그래밍
    씨네
    씨네
    개발자 씨네가 공부하는 내용을 기록 겸 공유하는 블로그입니다!

    티스토리툴바