[SPRING] Annotation 란❓

수경·2025년 3월 25일

SpringFrameWork

목록 보기
14/24
post-thumbnail

Annotation 란?

스프링 애너테이션은 XML 설정 없이 간결하게 스프링 프레임워크를 설정하고 사용할 수 있도록 해주는 자바의 메타데이터입니다. 애너테이션을 사용하면 스프링이 자동으로 객체를 생성하고, 주입하고, 관리할 수 있습니다.

💡주요 SPRING Annotation

<context:component-scan>

스프링 컨테이너가 빈을 자동으로 감지하고 등록할 때 사용
📌 관련 패키지:

  • org.springframework.stereotype

    @Component 는 가장 기본적인 애너테이션이고, @Controller, @Service , @Repository 는 각각의 역할에 맞게 세분화한 것을 말한다.

<context:component-scan> 태그의 역할

스프링 프레임워크에서는 <context:component-scan> 태그를 사용하면 특정 패키지를 스캔하여, 해당 패키지 안에 있는 특정 애너테이션이 붙은 클래스를 자동으로 빈(Bean)으로 등록한다.

즉, XML 설정에서 component-scan 을 사용하면, 해당 패키지의 클래스를 자동으로 스프링 컨테이너가 관리하도록 만든다는 의미이다.

✅ 예제: XML 설정 방식에서 <context:component-scan> 사용
< applicationContext.xml>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd">

    <!-- 특정 패키지를 스캔하여 @Component, @Service 등의 애너테이션이 붙은 클래스를 자동으로 빈 등록 -->
    <context:component-scan base-package="com.example.service"/>
</beans>

👉 com.example.service 패키지 안에 있는 클래스 중에서 애너테이션이 붙은 클래스는 자동으로 빈(Bean)으로 등록된다.

✅ 예제: @Component 를 사용한 클래스
,<MyService.java>

package com.example.service;

import org.springframework.stereotype.Component;

@Component  // @Component가 붙어 있기 때문에 자동으로 빈으로 등록됨
public class MyService {
    public void doSomething() {
        System.out.println("MyService 실행!");
    }
}

<UserService.java>

package com.example.service;

import org.springframework.stereotype.Service;

@Service  // @Service도 결국 @Component의 확장 개념이므로 자동으로 빈 등록됨
public class UserService {
    public void processUser() {
        System.out.println("UserService 실행!");
    }
}

✅ 예제: @Autowired 로 의존성 주입
<MyController.java>

package com.example.controller;

import com.example.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class MyController {

    @Autowired  // MyService를 자동으로 주입받음
    private MyService myService;

    public void execute() {
        myService.doSomething();
    }
}

의존성 주입(DI) 관련 패키지

자동으로 빈을 주입하는 기능
📌 관련 패키지:

  • org.springframework.beans.factory.annotation
  • jakarta.inject

스프링 설정 관련 패키지

애플리케이션의 설정을 정의하는 애너테이션을 제공
📌 관련 패키지:

  • org.springframework.context.annotation

트랜잭션 관리 관련 패키지

스프링에서 트랜잭션을 관리하는 애너테이션을 제공
📌 관련 패키지:

  • org.springframework.transaction.annotation

AOP(관점 지향 프로그래밍) 관련 패키지

AOP(Aspect-Oriented Programming)를 구현하기 위한 애너테이션이 포함되어 있다.
📌 관련 패키지:

  • org.springframework.aop
  • org.aspectj.lang.annotation

웹(Spring MVC) 관련 패키지

웹 요청을 처리하는 컨트롤러 및 RESTful API 구현을 위한 애너테이션을 제공
📌 관련 패키지:

  • org.springframework.web.bind.annotation
profile
개발 공부중•••

0개의 댓글