Spring_06_ AOP

hyeong taek jo·2023년 10월 3일
0

Spring

목록 보기
6/34

📌 AOP

  • 핵심관심사와 횡단 관심사가 있는데 핵심관심사가 실행할때 횡단 관심사가 따라서 실행되는 것이다
package AOP2;

import org.aspectj.lang.ProceedingJoinPoint;

public class LogAop {
	
	public Object loggerAop(ProceedingJoinPoint joinPoint) throws Throwable {
		//핵심  관심사 Method(그냥 이렇게 쓴다라고 알기)
		String signatureStr = joinPoint.getSignature().toShortString();
		System.out.println(signatureStr + " is start..");
		//성능 측정(시작시간..)
		long startTime = System.currentTimeMillis();
		
		Object obj;
		
		try {
			// 핵심관심사 Method 수행
			obj = joinPoint.proceed();
			return obj;
		} finally {
			// 핵심관심사의 종료시간
			long endTime = System.currentTimeMillis();
			System.out.println( signatureStr + " is finished.");
			System.out.println( signatureStr + " 경과시간 " + (endTime - startTime));
		}
	}
	
	public void beforeAdvice() {
		System.out.println("beforeAdvice()");
	}
	
	public void afterReturningAdvice() {
		System.out.println("afterReturningAdvice()");
	}
	
	public void afterThrowingAdvice() {
		System.out.println("afterThrowingAdvice()");
	}
	
	public void afterAdvice() {
		System.out.println("afterAdvice()");
	}
	
	
}

package AOP2.buz;

public class Student {
	private String name;
	private int age;
	private int gradeNum;
	private int classNum;
	
	public Student() {
		System.out.println("Student 생성자");
	}
	
	public void getStudentInfo() {
		System.out.println("이름 : " + getName());
		System.out.println("나이 : " + getAge());
		System.out.println("학년 : " + getGradeNum());
		System.out.println("반 : " + getClassNum());
		// 어쩌다 오류
		// System.out.println(10/0);
	}
	
	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 int getClassNum() {
		return classNum;
	}
	public void setClassNum(int classNum) {
		this.classNum = classNum;
	}
	
	
}

package AOP2.buz;

public class Worker {
	private String name;
	private int age;
	private String job;
	
	public void getWorkerInfo() {
		System.out.println("이름 : " + getName());
		System.out.println("나이 : " + getAge());
		System.out.println("직업 : " + getJob());
	}

	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 getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}
}

<?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="AOP2.LogAop"></bean>
	
	<aop:config>
		<aop:aspect id="logger" ref="logAop">
			<aop:pointcut expression="within(AOP2.buz.*)" id="pointcut1"/>
			<aop:around method="loggerAop" pointcut-ref="pointcut1"/>
		</aop:aspect>
		
		<aop:aspect id="logger" ref="logAop">
			<aop:pointcut expression="within(AOP2.buz.*)" id="pointcut2"/>
			<aop:before method="beforeAdvice" pointcut-ref="pointcut2"/>
		</aop:aspect>
		
		<aop:aspect id="logger" ref="logAop">
			<aop:pointcut expression="within(AOP2.buz.*)" id="pointcut3"/>
			<aop:after-throwing method="afterThrowingAdvice" pointcut-ref="pointcut3"/>
		</aop:aspect>
		
		<aop:aspect id="logger" ref="logAop">
			<aop:pointcut expression="within(AOP2.buz.*)" id="pointcut4"/>
			<aop:after method="afterAdvice" pointcut-ref="pointcut4"/>
		</aop:aspect>
	</aop:config>


	<bean id="student" class="AOP2.buz.Student">
		<property name="name" value="김춘추"/>
		<property name="age" value="10"/>
		<property name="gradeNum" value="3"/>
		<property name="classNum" value="5"/>
	</bean>
	
	<bean id="worker" class="AOP2.buz.Worker">
		<property name="name" value="김유신"/>
		<property name="age" value="35"/>
		<property name="job" value="개발자"/>
	</bean>

	
</beans>
profile
마포구 주민

0개의 댓글