Spring - Aop(관점 지향 프로그래밍, Aspect Oriented Programming)
프로그래밍 정리/Spring
2019. 12. 30. 18:08
반응형
Spring - Aop
(관점 지향 프로그래밍, Aspect Oriented Programming)
AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것이다. 여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다.
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
-Spring-Aop란?
AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것이다. 여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다.
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
AOP는 Aspect Oriented Programming의 약자로 관점 지향 프로그래밍이라고 불린다. 관점 지향은 쉽게 말해 어떤 로직을 기준으로 핵심적인 관점, 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화하겠다는 것이다. 여기서 모듈화란 어떤 공통된 로직이나 기능을 하나의 단위로 묶는 것을 말한다.
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
출처: https://engkimbs.tistory.com/746 [새로비]
aop는 관점 지향프로그래밍으로,
핵심부분과 공통부분을 구분하여
관점을 기준으로 각각 모듈화 하는 과정을 말한다.
-사용되는 용어
-AOP 구현
AOP는 3번의 과정으로 호출부, Proxy, Target 으로 나뉜다.
-Proxy(대행)
위와 같이 흩어진 관심사를 Aspect로 모듈화하고 핵심적인 비즈니스 로직에서 분리하여 재사용하겠다는 것이 AOP의 취지다.
| AOP 주요 개념
- Aspect : 위에서 설명한 흩어진 관심사를 모듈화 한 것. 주로 부가기능을 모듈화함.
- Target : Aspect를 적용하는 곳 (클래스, 메서드 .. )
- Advice : 실질적으로 어떤 일을 해야할 지에 대한 것, 실질적인 부가기능을 담은 구현체
- JointPoint : Advice가 적용될 위치, 끼어들 수 있는 지점. 메서드 진입 지점, 생성자 호출 시점, 필드에서 값을 꺼내올 때 등 다양한 시점에 적용가능
- PointCut : JointPoint의 상세한 스펙을 정의한 것. 'A란 메서드의 진입 시점에 호출할 것'과 같이 더욱 구체적으로 Advice가 실행될 지점을 정할 수 있음
출처: https://engkimbs.tistory.com/746 [새로비]
프록시는 공통부분(point-cut)과 핵심부분(target)을 같이 실행시키기 위한 공간
혹은 따로 만들어논 공간이라고 생각하면 된다.
예) A라는 객체를 선언하고, A객체의 메서드(target)를 Weaving(위빙) 하였다.
위빙을 했으므로, A객체의 메서드(target)를 실행할 경우
항상 point-cut이 먼저 실행 후 A객체 메서드가 실행된다.
-실습
★실습하기전에 'pom.xml' 에서 'aspectjweaver' 를 추가해주어야 한다.
-main.java
항상 하던 방식으로 메서드를 실행하면 된다.
-Student.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 | public class Student { private String name; private int age; private int gradeNum; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGradeNum() { return gradeNum; } public void setGradeNum(int gradeNum) { this.gradeNum = gradeNum; } public void getStudentInfo() { System.out.println("이름 : " + getName()); System.out.println("나이 : " + getAge()); System.out.println("학년 : " + getGradeNum()); System.out.println("반 : " + getClassNum()); } } | cs |
-applicationCTX.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <bean id="logAop" class="com.javalec.ex.LogAop" /> //logAop.java 클래스 등록 <aop:config> <aop:aspect id="logger" ref="logAop"> //logAop.java 클래스를 aspect로 등록 <aop:pointcut id="publicM" expression="within(com.javalec.ex.*)" /> //ex패키지와 하위 패키지의 모든 메소드 실행 <aop:around pointcut-ref="publicM" method="loggerAop" /> //Advice 종류 지정 </aop:aspect> //pointcut으로 지정한target의 앞뒤로 실행 </aop:config> <bean id="student" class="com.javalec.ex.Student" > <property name="name" value="홍길동" /> <property name="age" value="10" /> <property name="gradeNum" value="3" /> <property name="classNum" value="5" /> </bean> | cs |
-advise 타입종류
-logAop
1 2 3 4 5 6 7 8 9 10 11 12 | public class LogAop { public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable{ System.out.println("┌(시작부분) 이부분은 공통적으로 들어가는 문장입니다.┐"); try { Object obj = joinpoint.proceed(); return obj; }finally { System.out.println("└(끝부분) 이부분은 공통적으로 들어가는 문장입니다.┘"); } } } | cs |
- 지정된 메서드(핵심부분)를 실행하는순간 loggerAop(프록시)메서드가 실행되며
loggerAop의 try으로 핵심부분이 call~end 된다.
----------------------------------------------------------모든소스-------------------------------------------------------
-main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.javalec.test; import org.springframework.context.support.GenericXmlApplicationContext; public class MainClass { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml"); Student stu = ctx.getBean("student", Student.class); stu.getStudentinfo(); Worker wor = ctx.getBean("worker", Worker.class); wor.getWorkerinfo(); } } | cs |
-student.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 | package com.javalec.test; public class Student { String name; int age; String gradenum; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGradenum() { return gradenum; } public void setGradenum(String gradenum) { this.gradenum = gradenum; } public void getStudentinfo() { System.out.println("이름 : " + getName()); System.out.println("나이 : " + getAge()); System.out.println("학년 : " + getGradenum()); } } | cs |
-LogAop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.javalec.test; import org.aspectj.lang.ProceedingJoinPoint; public class LogAop { public Object loggerAop(ProceedingJoinPoint joinpoint) throws Throwable{ System.out.println("┌(시작부분) 이부분은 공통적으로 들어가는 문장입니다.┐"); try { Object obj = joinpoint.proceed(); return obj; }finally { System.out.println("└(끝부분) 이부분은 공통적으로 들어가는 문장입니다.┘"); } } } | cs |
-applicationCTX.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?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-3.2.xsd"> <bean id="LogAop" class="com.javalec.test.LogAop"/> <aop:config> <aop:aspect id="logger" ref="LogAop"> <aop:pointcut expression="within(com.javalec.test.*)" id="publicM"/> <aop:around method="loggerAop" pointcut-ref="publicM"/> </aop:aspect> </aop:config> <bean id="student" class="com.javalec.test.Student"> <property name="name" value="홍길동"/> <property name="age" value="11"/> <property name="gradenum" value="4"/> </bean> </beans> | cs |
반응형
'프로그래밍 정리 > Spring' 카테고리의 다른 글
MVC_Spring - MVC_Spring 시작 (0) | 2019.12.31 |
---|---|
Spring - Aop autoproxy방식,pointcut 종류 (0) | 2019.12.31 |
Spring - bean을 이용해 .properties 값 가져오기 (0) | 2019.12.26 |
Spring - XML과 Bean을 이용해 .properties 값 가져오기 (0) | 2019.12.09 |
spring - Namespace 사용법 (0) | 2019.12.09 |