본문 바로가기
BackEnd

5/30 - Spring, AspectJ

by Jiwon_Loopy 2025. 6. 1.
반응형

목차


AspectJ


  • @EnableAspectJAutoProxy
  • AOP
AOP (Aspect Oriented Programming), 관점 지향 프로그래밍어떤 로직을 기준으로 핵심적인 관점(비즈니스 로직), 부가적인 관점을 나누어 그 관점을 기준으로 모듈화- 요청(Request)에 대해 핵심 관심사항(Aspect)과 부가 관심사항으로 나눠 관점을 기준으로 프로그램을 구현하는 기법- OOP: 사용자의 관점에서 필요한 핵심적인 비즈니스 로직을 구현하는데 있어객체(클래스)를 모듈화함으로써 반복되는 코드를 줄임- AOP: OOP의 개념에 더해, 어플리케이션 전체에 사용되는부가기능(Aspect)들을 모듈화, 공통 기능(Corsscutting Concerns)관리를 더 효율적으로 가능하게 함 (개발, 운영 측면에서 OOP를 더욱 강력하게 만듦)

출처:

[Java Spring] AOP 개념 및 사용 방법(AspectJ, xml, annotation)

Proxy


  • 공통 기능을 따로 빼서 메인 기능 처럼 사용
package chap05;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
		Calculator calc = (Calculator)ctx.getBean("cal");
		long r = calc.factorial(10);
		System.out.println(r);
		
	}
}
package chap05;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Timer {
	// 공통기능을 적용할 위치(포인트) 지정
	@Pointcut("execution(public * chap05.excute..*(..))")
	private void timerTarget() {}
	
	@Around("timerTarget()")
	public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
		long start = System.nanoTime();
		Object result = joinPoint.proceed();
		long end = System.nanoTime();
		System.out.println("AOP에서 소요된 시간:" + (end-start));
		return result;
	}
}

package chap05;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class Config {
	@Bean
	public Calculator cal() {
		return new CalculatorImpl();
	}
	
	@Bean
	public Calculator cal2() {
		return new CalculatorImpl2();
	}
	
	@Bean
	public Timer timer() {
		return new Timer();
	}
}

package chap05;

package chap05;

public interface Calculator {
	long factorial(int n);
}

public class CalculatorImpl2 implements Calculator {

    @Override
    public long factorial(int n) {
        if (n == 0) return 1;
        return factorial(n-1)*n;
    }
}
package chap05;

public class CalculatorImpl implements Calculator {

    @Override
    public long factorial(int n) {
        if (n < 0) {
            throw new IllegalArgumentException("n은 0 이상의 정수여야 합니다.");
        }
        if (n == 0 || n == 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }
}

DB 연동 전 설치


  • HikariCP
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>6.3.0</version>
</dependency>
  • Mybatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>3.0.4</version>
</dependency>
  • Mybatis-Spring
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.19</version>
</dependency>

메모


  • 순서
    • Controller - Service - DAO - MyBatis - DBCP(JDBC) - DB
  • 라이브러리
    • HikariCP, MyBatis
  • MVN
    • Hikari CP 검색
    • Mybatis Spring, Mybatis 다운
  • pom.xml 추가
    • hikaricp →
728x90
반응형

'BackEnd' 카테고리의 다른 글

6/1 - 의존성 주입 (DI), 관점 지향 프로그래밍 (AOP)  (0) 2025.06.06
Spring Lagacy Project, Spring MVC, MyBatis 인텔리제이에서 세팅하기  (0) 2025.06.03
5/29 - MVC  (0) 2025.06.01
5/28 - Spring  (0) 2025.06.01
5/27 - ajax, JSON  (1) 2025.06.01