본문 바로가기
SPRING

aop적용방식

by brilliant-growth 2023. 7. 5.

aop.zip
0.01MB

깃으로 확인 : brilliant-growth/spring_aop10 at main · kdh11112/brilliant-growth (github.com)

 

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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">

<!-- <bean id="ct" class="kr.co.jhta.app.CheckReloadTime1"></bean> -->
<!-- <bean id="ct" class="kr.co.jhta.app.CheckReloadTime2"></bean> -->
<bean id="ct" class="kr.co.jhta.app.CheckReloadTime3"></bean>

<!-- 1번째 방식 -->
<!--  <bean id="w" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="imple"></property>
<property name="interceptorNames">
<list>
<value>ct</value>
</list>
</property>
</bean> -->

<!-- 2번째 방식 -->
<!--  <aop:config>
<aop:aspect id="ch" ref="ct">
<aop:pointcut expression="execution (public * tire(..))" id="ch"/>
<aop:around method="logAround" pointcut-ref="ch"/>
</aop:aspect>
</aop:config> -->

<bean id="imple" class="kr.co.jhta.app.WeaponImple"></bean>
<!-- 3번째 방식 -->


<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

 

 

package kr.co.jhta.app;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

public class CheckReloadTime implements MethodInterceptor{

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {

		StopWatch sw = new StopWatch();
		
		sw.start();
		Object obj = invocation.proceed();
		sw.stop();
		
		System.out.println("걸린시간 : "+ sw.getTotalTimeSeconds()+"초");
		
		return obj;
	}

}

 

package kr.co.jhta.app;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

public class CheckReloadTime2 {
	
	public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
		
		String methodname = pjp.getSignature().getName();
		
		System.out.println("실행중인 메서드명: " +methodname);
		
		StopWatch sw = new StopWatch();
		
		sw.start();
		
		Object obj = pjp.proceed();
		sw.stop();
		
		System.out.println("실행 시간"+sw.getTotalTimeSeconds()+"초");
		
		return obj;
	}



}

 

package kr.co.jhta.app;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

@Aspect
public class CheckReloadTime3 {
	
	@Pointcut("execution (public * tire(..))")
	public void publicTarget() {
		
	}
	
	@Around("publicTarget()")
	public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
		
		String methodname = pjp.getSignature().getName();
		System.out.println("실행중인 메서드: "+methodname);
		
		StopWatch sw = new StopWatch();
		
		sw.start();
		
		Object obj = pjp.proceed();
		sw.stop();
		
		System.out.println("처리 시간 : "+sw.getTotalTimeSeconds()+"초");
		
		return obj;
	}



}

package kr.co.jhta.app;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
	
	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("app.xml");
		
		//1번째 방식
//		Weapon w = ac.getBean("w",Weapon.class);
		
		//2번째 방식
		Weapon w = ac.getBean("imple",Weapon.class);
		
		w.tire();
		w.reload();
		
	}

}

'SPRING' 카테고리의 다른 글

스프링 어노테이션을 사용하기 위한 xml설정  (0) 2023.07.05
DI의 종류  (0) 2023.07.05
단위테스트  (0) 2023.07.05
AOP란?  (0) 2023.07.05
Spring Framework란?  (0) 2023.07.05