[Spring] 스프링 AOP 이해를 위한 초간단 코드 만들어보기

heegon·6일 전
0

Web

목록 보기
13/14

Human.java

public class Human {

	private String name;
	private int age;
	
	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 void getHumanInfo() {
		System.out.println("이름 : " + getName());
		System.out.println("나이 : " + getAge());
	}
	
}

MainClass.java

public class MainClass {
	
	public static void main(String[] args) {
		
		AbstractApplicationContext ctx
		= new GenericXmlApplicationContext("classpath:applicationCTX.xml");
		
		Human human1 = ctx.getBean("human", Human.class);
		human1.getHumanInfo();
		
		ctx.close();
	}

}

applicationCTX.xml

<?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.1.xsd">

<bean id="logAop" class="kr.study.spring.LogAop" />

<aop:config>
	<aop:aspect id="logger" ref="logAop">
		<aop:pointcut id="pointcut1" expression="within(kr.study.spring..*)" />
		<aop:around pointcut-ref="pointcut1" method="aopMethod" />
	</aop:aspect>
</aop:config>


<bean id="human" class="kr.study.spring.Human">
	<property name="name" value="희곤"></property>
	<property name="age" value="20"></property>
</bean>

</beans> 

LogAop.java

public class LogAop {
	
	public Object aopMethod(ProceedingJoinPoint jp) throws Throwable {
		
		System.out.println("실행 전");
		try {
			Object obj = jp.proceed();
			return obj;
		} finally {
			System.out.println("실행 끝");
		}
	}
}
profile
❤️

0개의 댓글