CS

Spring의 특징 - DI, IoC, POJO, AOP

Seong-Jun 2025. 4. 19. 18:39
728x90
반응형
SMALL

DI - Dependency Injection

의존성 주입이라는 의미로 개발자가 직접 객체를 생성하지 않고 어노테이션을 통해 객체 간의 의존 관계를 프레임워크가 주입하는 것이다. 객체가 직접 의존하는 객체를 생성하거나 참조할 필요없이 의존성을 외부에서 주입받도록 한다.

코드의 가독성과 유지 보수성 향상!

권장되는 방식인 생성자 주입 방식!

@Service
public class TodoServiceImpl implements TodoService {

    private final TodoDAO todoDAO;

    @Autowired
    public TodoServiceImpl(TodoDAO todoDAO) {
        this.todoDAO = todoDAO;
    }
    // ...
}

IoC - Inversion of Control

제어의 역전으로 개발자 프로그램의 흐름을 제어하는 것이 아닌 프레임워크가 흐름을 제어하는 개념이다. 객체의 모든 생명 주기를 프레임워크가 담당한다.

AOP - Aspect Oriented Programming

관점 지향 프로그래밍으로 특정 로직에 대해 핵심적인 관점과 부가적인 관점으로 나누어서 보고 그 관점을 기준으로 각각 모듈화 한다는 의미이다. 예를 들어 공통적인 기능으로 로깅같은 경우에 메서드 별로 다 작성하는 것이 아니라 @Aspect로 모듈화하여 비즈니스 로직을 분리한다. 이를 통해 복잡도를 줄이고 재사용성을 높일 수 있다. 또한 해당 로직에는 딱 해당 기능관련 코드만 남게 된다.

@Aspect
@Component
public class LoggingAspect {
    // 롬복 없이 로그 객체 사용. Logger 객체 필요
    private final Logger log = LoggerFactory.getLogger(LoggingAspect.class);

    // 기본 패키지 경로 내의 모든 메서드를 대상
    @Pointcut("execution(* 경로작성")
    private void allPointCut() {}

    // 호출 전 메서드명, 파라미터 정보를 로그로 출력
    @Before("allPointCut()")
    public void beforeAdvice(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();

        Object[] obj = joinPoint.getArgs();

        log.info("=============== Before ===============");

        log.info("method info :: {}", joinPoint.getSignature().getName());
        log.info("parameter :: {}", joinPoint.getArgs());
    }

    @Around("allPointCut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("=============== Around Advice ===============");

        long start = System.currentTimeMillis();

        Object execute = joinPoint.proceed();

        long end = System.currentTimeMillis();

        log.info("info :: {}", joinPoint.getSignature());
        log.info("execution time :: {}", (end - start));

        return execute;
    }
}

POJO - Plain Old Java Object

순수 자바 객체이다. 다른 클래스나 인터페이스를 상속받아 메서드가 추가된 클래스가 아닌 getter나 setter 등 기본적인 기능만 가진 클래스(자바 객체)이다. 스프링에서는 특정 프레임워크에 의존하지 않는 순수한 자바 객체를 이용하여 개발할 수 있다.

728x90
반응형
LIST

'CS' 카테고리의 다른 글

REST API 정리  (0) 2025.04.19
프레임워크(Framework)와 라이브러리(Library)의 차이점  (0) 2025.04.19
Web server 와 WAS 의 차이점  (0) 2025.03.22