상세 컨텐츠

본문 제목

[스프링] around aop

카테고리 없음

by esoesmio 2023. 6. 6. 20:20

본문

package com.bit.springboard.common;

import org.aspectj.lang.ProceedingJoinPoint;

public class Around {
    
    
    
    
    
    
public Object aroundlog(ProceedingJoinPoint pjp) throws Throwable{
    System.out.println("[사전처리]비즈니스 로직 전 실행");
    Object returnObject = pjp.proceed();
    System.out.println("[사후처리]비즈니스 로직 후 실행");
    return returnObject;
}

}
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<!-- Root Context: defines shared resources visible to all other web components -->



	<!-- Rootcontext.xml 파일을 읽어서 스프링테이너 구동시 bean 태그로 된 클래스의 객체 자동 생 -->
	<!-- bean객체 : bean 태그로 등록된 클래스의 객체. bean 객체로 등록하는 클래스들은 라이브러리들의 객체를 생성할 
		때 등록. 라이브러리들에는 개발자가 어노테이션으로 객체생성이 불가능하기 때문. -->

	<!-- initmethod : 자동객체 생성시 초기화 필요한 객체들은 초기화 메소드를 만들어 매핑 -->
	<!-- destroy method : 스프링 컨테이너에 의해 객체가 삭제되기 전에 수행할 동작을 작성한 메소드 매핑 자동삭제되기전에 
		수행할 동작들을 매핑을 해높을수 있다. -->

	<!-- scope : 객체의 생성 방식을 지정. singleton, prototype인지 지정가능.생성된 하나의 객체를 공유할 
		것인지 요청때마다 객체를 새로 생성할 것인지 결정. --><!-- 레이지이닛 : 객체의 생성 시점을 설정. 트루 설정시 객체 요청 시에 객체 생 -->
	<!-- IOC 제어의 역전 : 개발자가 직접 생성하던 객체를 스프링 설정 파일응ㄹ 통해 스프링에 위임 -->
	<!-- <bean id="kcar" class="com.bit.springboard.coupling.kiacar"></bean> -->

	<!-- constructor-arg: 생성자 호출에 넘겨줄 파라미터지정 constructor-arg 앨리먼트 개수만큼에 알맞는 
		생성자를 찾아서 호출 -->

	<!-- property : name 속성의 값과 동일한 변수명에 해당하는 변수의 setter 메소드를 자동 호출. -->


	<!-- namespace탭에서 p 선택. -->
	<!-- property 엘리먼트 더 편하게 사용하 p: property의 약자 p:변수명 => 해당 변수에 대한 세터 메소드 호출 
		ref : 변수가 클래스 타입일 경우 bean객체랑 매핑 아니면 그냥 일반 값으로.. -->

	<!-- <bean id="hcar" class="com.bit.springboard.coupling.hyundaicar" p:caraudio-ref="sonycaraudio" 
		p:model="gv70"></bean> -->
	<!-- <property name = "caraudio" ref = "sonycaraudio"></property> <property 
		name = "model" value = "gv70"></property> -->




	<!-- <bean id="caraudio" class="com.bit.springboard.coupling.caraudio"></bean> -->
	<!-- <bean id="applecaraudio" class="com.bit.springboard.coupling.applecaraudio"></bean> -->
	<!-- <bean id="sonycaraudio" class="com.bit.springboard.coupling.sonycaraudio"></bean> -->
	<!-- 생성자로 하는거 -->



	<!-- component-scan : 스캔할 피키지를 지정하여 어노테이션으로 자동 객체 생성. -->
	<!-- 이 패키지 안에 @component어노테이션이 달란 클래스들만 찾아서 객체 생성. -->

	<context:component-scan
		base-package="com.bit.springboard"></context:component-scan>

	<bean id="consoleLog"
		class="com.bit.springboard.common.LogConsoleV2"></bean>
	<!-- aop 동작설정 aop:config 엘리먼트 : aop 설정의 루트 엘리먼트. -->
	
	
	<bean id = "afterReturning" class = "com.bit.springboard.common.AfterReturning"></bean>
	
	<bean id = "afterThrowing" class = "com.bit.springboard.common.AfterThrowing"></bean>
	<bean id = "afterLog" class = "com.bit.springboard.common.After"></bean>
	
	<bean id = "around" class = "com.bit.springboard.common.Around"></bean>
	
	
	
	
	<aop:config>
		<!-- aop.pintcut : 공통 관심코드가 실행될 메소드를 지정 -->
		<aop:pointcut id="allpointcut"
			expression="execution(* com.bit.springboard.service..BoardServiceimpl.*(..))"></aop:pointcut>
			
			<aop:pointcut id="getpointcut"
			expression="execution(* com.bit.springboard.service..*impl.get*(..))"></aop:pointcut>


	<!-- 	<aop:aspect ref="afterReturning">
		
			<aop:after-returning method="afterReturningLog"
				pointcut-ref="allpointcut"></aop:after-returning>

	
		</aop:aspect> -->
		
		<!-- <aop:aspect ref="afterThrowing">
			<aop:after-throwing method="afterThrowingLog"
				pointcut-ref="allpointcut"></aop:after-throwing>
		</aop:aspect> -->
		
		
	<!-- 	<aop:aspect ref="afterLog">
			<aop:after method="afterLog"
				pointcut-ref="allpointcut"></aop:after>
		</aop:aspect> -->
		
		<aop:aspect ref="around">
			<aop:around method="aroundLog"
				pointcut-ref="allpointcut"></aop:around>
		</aop:aspect>
		
		
	</aop:config>

	<!-- aop:aspect 공통관심코드가 실행될 메소드와 실행될 코드를 매칭하는 작업 -->

</beans>

댓글 영역