도담도담

AOP(Aspect Oriented Programming) 본문

IT 공부/KH 정보교육원

AOP(Aspect Oriented Programming)

Zinisang 2021. 6. 17. 21:11


스프링에서 AOP를 활용하려면 pom.xml에서 <dependency> ~~~ 를 추가해 주어야한다.
(추가되었는지는 maven dependency에 가서 aspectjrt 와 aspectjweaver가 있는지 확인!)
핵심관심 메서드와 횡단관심 메서드 사이에서 소스상을 결합은 절대 발생하지 않는다!!

AOP 용어

조인 포인트(포괄적)

  • 클라이언트가 호출하는 모든 비즈니스 메서드

포인트컷(일부 메서드를 지정)

  • 필터링된 조인포인트


어드바이스 동작 시점

before
after
after-returning
after-throwing
around

 

위빙 - 메서드가 삽입되는 과정

  • 스프링프레임워크는 런타임 중에서만 가능

 

애스펙트/어드바이저

  • 포인트컷과 어드바이스의 결합 - 어떤 어스바이스를 어떤 시점에서 사용할지 정해준다

 

 

AOP를 사용하기 위해서 필요한거

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>1.8.8</version>
	</dependency>

 

applicationContext.xml

<beans xmlns:aop="http://www.springframework.org/schema/aop"

	<!-- AOP -->
	<bean id="log" class="com.lsj.biz.common.LogAdvice"></bean>
	
	<aop:config>
		<aop:pointcut expression="execution(* com.lsj.biz..*Impl.*(..))" id="allPointcut"/>
		<aop:pointcut expression="execution(* com.lsj.biz..*Impl.get*(..))" id="getPointcut"/>
		<aop:aspect ref="log">
			<aop:after method="printLog" pointcut-ref="getPointcut" />
		</aop:aspect>
	</aop:config>


<aop:config>				<!-- AOP 설정 -->
	<aop:pointcut ~~/>		<!-- 비지니스로직 메서드 설정 -->		
	<aop:aspect ~~/>		<!-- 어드바이스 메서드 실행 시점 -->
</aop:config>



	<aop:pointcut ~~/>		<!-- 비지니스로직 메서드 설정 -->
				id속성="유일한이름" , expression속성="execution()"
				execution(리턴 패키지..클래스명.메서드명(..)
						   *  com.test.biz..*impl.*(..)	
						   *  com.test.biz..Board*.get*(..)	


	<aop:aspect ref="어드바이스객체" />		<!-- 어드바이스 메서드 실행 시점 -->
								포인트컷 메서드와 어드바이스 메서드를 결합 : 실행 시점	
		<aop:실행시점 pointcut-ref="포인트컷id" method="어드바이스메서드명" />
	</aop:aspect>
	
	
	<aop:config>
		<aop: advisor ~~~ /> <!-- 트렌잭션과 같은 특수한 경우에 사용 -->
	<aop:config>

</beans>
Comments