10/18

BS_Lee·2023년 10월 18일
0

Spring 교육일지

목록 보기
2/3

10/18

AOP연습 문제


BeforeAdvice

package exam;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;

@Aspect
public class BeforeAdvice {
	@Autowired
	LoginTest login;
	
	@Before("execution(* exam.SecurityImple.*(..))")
	public void ChK() {
		if(login.isRes()) {
			System.out.println("인증완료");
		}else {
			System.out.println("비밀번호 오류!");
			System.exit(0);
		}
	}
}

BizMain

package exam;

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

public class BizMain {
	public static void main(String[] args) {
		ApplicationContext ctx = new GenericXmlApplicationContext("exam/exam.xml"); //여기서 생성자가 실행..?
		BizService bean = ctx.getBean("bzService", BizService.class);
		bean.securityMethod();
	}
}

BizService

package exam;

public interface BizService {
	public void securityMethod();
}

LoginTest

package exam;

import java.util.*;

public class LoginTest {
	private Scanner sc;
	private String pwd;
	private boolean res;
	private Properties prop;
	
	public LoginTest(){
		String[] msg = {"틀렸습니다", "아쉽군요. 마지막 한번만 더", "너OUT"};
		sc = new Scanner(System.in);
		
		for (int i = 0; i < msg.length; i++) {
			System.out.println("비밀번호 입력: ");
			String pwd = sc.nextLine();
			if(!pwd.equals("1")) {
				System.out.println(msg[i]);
				res = false;
			}
			else {
				res=true;
				break;
			}
		}
	}
	public boolean isRes() {
		return res;
	}
}

SecurityImple

package exam;

public class SecurityImple implements BizService{
	//인증이 완료되었을 경우 수행
	@Override
	public void securityMethod() {
		System.out.println("비즈니스 로직 3 수행");
	}
	
}

exam.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"
	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">

<context:annotation-config/>
<aop:aspectj-autoproxy/> <!-- advice를 관리해주고 proxyFactoryBean. -->
<!-- BeforeAdvice에 적용할 빈 -->
<bean id="bzService" class="exam.SecurityImple"/>

<!-- Aspect Bean -->
<bean id="bzMain" class="exam.BeforeAdvice"/>

<!-- Target Bean -->
<bean id="login" class="exam.LoginTest"/>
</beans>

문제 풀면서 후기…

사실 지금 AOP가 뭔지는 감은 잡았지만 코드를 어떻게 작성해야되는지 전혀 감이 안온다…

→ 이 부분은 주말에 연습해서 조금 더 역량을 길러야 될듯….

두번째로 AutoWired였는데 byName과 byType에 대한 지식이 확고하지 않은거같다…

세번째로 AOP를 하면서 중간에 막히는 경우가 많았는데, 첫번째로 @Before() 괄호 안에 들어가야되는게 타겟인지 헷갈린다…. (타겟은 맞음)

네번째로 Joinpoint를 언제 써줘야되는가…? @After@Before는 필요 없지만 @Arround는 필수.. JoinPoint는 코드가 들어갈 부분을 말해주는건데 @Arround같은 경우에는 어디든지 들어갈 수 있기 때문에 필수이다…

다섯번째로 BizMain에서 해맸는데…. BizService bean = ctx.getBean("bzService", BizService.class); 에서 나는 당연히 주는 타입을 SecurityImple클래스라고 생각했다… 하지만 클래스를 꺼내올 때는 상위 타입으로 적어야함!!

profile
설명이 가능할 때까지 공부하기.

0개의 댓글