280 lines
10 KiB
Markdown
280 lines
10 KiB
Markdown
자바에서 기본적으로 자주 사용되는 어노테이션들을 정리하겠습니다. 여기서 "기본적으로 자주 사용되는"이란 자바 표준 API(JDK)와 스프링 프레임워크, 그리고 일반적인 ORM(JPA) 환경에서 널리 활용되는 어노테이션을 기준으로 하겠습니다. 이들은 자바 개발에서 필수적인 기능(예: 클래스/메서드 정의, DI, 데이터베이스 매핑 등)을 제공하며, 특별한 외부 라이브러리 없이도 자주 등장합니다.
|
|
|
|
아래에서 주요 어노테이션을 표 형식으로 정리한 뒤, 각 어노테이션에 대한 설명과 예시를 제공하겠습니다.
|
|
|
|
---
|
|
|
|
### 자주 사용되는 어노테이션 표
|
|
|
|
| 어노테이션 | 설명 | 출처 |
|
|
|-----------------------|----------------------------------------------------------------------------------------|---------------------|
|
|
| `@Override` | 메서드가 상위 클래스나 인터페이스의 메서드를 재정의했음을 명시 | JDK (java.lang) |
|
|
| `@Deprecated` | 해당 요소가 더 이상 권장되지 않음을 표시 | JDK (java.lang) |
|
|
| `@SuppressWarnings` | 컴파일러 경고를 억제 | JDK (java.lang) |
|
|
| `@FunctionalInterface`| 인터페이스가 단일 추상 메서드를 가지는 함수형 인터페이스임을 명시 | JDK (java.lang) |
|
|
| `@SafeVarargs` | 가변 인자 메서드에서 타입 안전성을 보장함을 명시 | JDK (java.lang) |
|
|
| `@Retention` | 어노테이션의 유지 범위를 지정 (SOURCE, CLASS, RUNTIME) | JDK (java.lang.annotation) |
|
|
| `@Target` | 어노테이션이 적용될 수 있는 대상을 지정 | JDK (java.lang.annotation) |
|
|
| `@Documented` | 어노테이션이 Javadoc에 포함되도록 지정 | JDK (java.lang.annotation) |
|
|
| `@Inherited` | 어노테이션이 하위 클래스에 상속되도록 지정 | JDK (java.lang.annotation) |
|
|
| `@SpringBootApplication` | 스프링 부트 애플리케이션의 메인 클래스임을 나타내며, 여러 설정을 포함 | Spring Boot |
|
|
| `@Autowired` | 의존성 주입(DI)을 자동으로 수행 | Spring Framework |
|
|
| `@RestController` | RESTful 웹 서비스 컨트롤러임을 지정 | Spring Framework |
|
|
| `@RequestMapping` | HTTP 요청을 메서드나 클래스에 매핑 | Spring Framework |
|
|
| `@GetMapping` | HTTP GET 요청을 메서드에 매핑 | Spring Framework |
|
|
| `@PostMapping` | HTTP POST 요청을 메서드에 매핑 | Spring Framework |
|
|
| `@Entity` | 클래스가 JPA 엔티티임을 나타냄 | JPA (jakarta.persistence) |
|
|
| `@Id` | 엔티티의 기본 키(PK)를 지정 | JPA (jakarta.persistence) |
|
|
| `@Column` | 필드가 데이터베이스 컬럼에 매핑됨을 지정 | JPA (jakarta.persistence) |
|
|
| `@Table` | 엔티티가 매핑될 데이터베이스 테이블을 지정 | JPA (jakarta.persistence) |
|
|
|
|
---
|
|
|
|
### 각 어노테이션 상세 설명 및 예시
|
|
|
|
#### 1. `@Override`
|
|
- **설명**: 메서드가 상위 클래스나 인터페이스의 메서드를 재정의했음을 컴파일러에 알립니다.
|
|
- **예시**:
|
|
```java
|
|
class Parent {
|
|
void display() { System.out.println("부모"); }
|
|
}
|
|
|
|
class Child extends Parent {
|
|
@Override
|
|
void display() { System.out.println("자식"); }
|
|
}
|
|
```
|
|
|
|
#### 2. `@Deprecated`
|
|
- **설명**: 메서드나 클래스가 더 이상 사용되지 않음을 표시하며, 사용 시 경고를 발생시킵니다.
|
|
- **예시**:
|
|
```java
|
|
class OldClass {
|
|
@Deprecated
|
|
void oldMethod() { System.out.println("구형 메서드"); }
|
|
}
|
|
```
|
|
|
|
#### 3. `@SuppressWarnings`
|
|
- **설명**: 특정 경고(예: "unchecked", "deprecated")를 억제합니다.
|
|
- **예시**:
|
|
```java
|
|
@SuppressWarnings("unchecked")
|
|
List rawList = new ArrayList(); // 경고 억제
|
|
```
|
|
|
|
#### 4. `@FunctionalInterface`
|
|
- **설명**: 단일 추상 메서드를 가진 인터페이스를 함수형 인터페이스로 표시합니다.
|
|
- **예시**:
|
|
```java
|
|
@FunctionalInterface
|
|
interface MyFunction {
|
|
void run();
|
|
}
|
|
```
|
|
|
|
#### 5. `@SafeVarargs`
|
|
- **설명**: 가변 인자 메서드가 타입 안전함을 보장합니다.
|
|
- **예시**:
|
|
```java
|
|
@SafeVarargs
|
|
static <T> void printAll(T... items) {
|
|
for (T item : items) System.out.println(item);
|
|
}
|
|
```
|
|
|
|
#### 6. `@Retention`
|
|
- **설명**: 어노테이션의 유지 범위를 지정합니다 (SOURCE: 소스 코드, CLASS: 바이트 코드, RUNTIME: 런타임).
|
|
- **예시**:
|
|
```java
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
@interface MyAnnotation {}
|
|
```
|
|
|
|
#### 7. `@Target`
|
|
- **설명**: 어노테이션이 적용될 수 있는 위치(예: 메서드, 클래스 등)를 지정합니다.
|
|
- **예시**:
|
|
```java
|
|
import java.lang.annotation.Target;
|
|
import java.lang.annotation.ElementType;
|
|
|
|
@Target(ElementType.METHOD)
|
|
@interface MyMethodAnnotation {}
|
|
```
|
|
|
|
#### 8. `@Documented`
|
|
- **설명**: 어노테이션이 Javadoc에 포함되도록 합니다.
|
|
- **예시**:
|
|
```java
|
|
import java.lang.annotation.Documented;
|
|
|
|
@Documented
|
|
@interface MyDocAnnotation {}
|
|
```
|
|
|
|
#### 9. `@Inherited`
|
|
- **설명**: 어노테이션이 하위 클래스에 상속됩니다.
|
|
- **예시**:
|
|
```java
|
|
import java.lang.annotation.Inherited;
|
|
|
|
@Inherited
|
|
@interface MyInheritedAnnotation {}
|
|
```
|
|
|
|
#### 10. `@SpringBootApplication`
|
|
- **설명**: 스프링 부트 애플리케이션의 진입점을 정의하며, `@Configuration`, `@EnableAutoConfiguration`, `@ComponentScan`을 포함합니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
@SpringBootApplication
|
|
public class Application {
|
|
public static void main(String[] args) {
|
|
SpringApplication.run(Application.class, args);
|
|
}
|
|
}
|
|
```
|
|
|
|
#### 11. `@Autowired`
|
|
- **설명**: 스프링의 의존성 주입을 자동으로 수행합니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
public class UserService {
|
|
@Autowired
|
|
private UserRepository repository;
|
|
}
|
|
```
|
|
|
|
#### 12. `@RestController`
|
|
- **설명**: REST API 컨트롤러를 정의하며, `@Controller`와 `@ResponseBody`를 결합한 형태입니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
@RestController
|
|
public class HelloController {
|
|
@GetMapping("/hello")
|
|
public String hello() { return "Hello, World!"; }
|
|
}
|
|
```
|
|
|
|
#### 13. `@RequestMapping`
|
|
- **설명**: HTTP 요청을 클래스나 메서드에 매핑합니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
@RestController
|
|
@RequestMapping("/api")
|
|
public class ApiController {
|
|
@RequestMapping("/test")
|
|
public String test() { return "API 테스트"; }
|
|
}
|
|
```
|
|
|
|
#### 14. `@GetMapping`
|
|
- **설명**: HTTP GET 요청을 메서드에 매핑합니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
@RestController
|
|
public class UserController {
|
|
@GetMapping("/users")
|
|
public String getUsers() { return "유저 목록"; }
|
|
}
|
|
```
|
|
|
|
#### 15. `@PostMapping`
|
|
- **설명**: HTTP POST 요청을 메서드에 매핑합니다.
|
|
- **예시**:
|
|
```java
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
@RestController
|
|
public class UserController {
|
|
@PostMapping("/users")
|
|
public String createUser(@RequestBody String user) { return "유저 생성: " + user; }
|
|
}
|
|
```
|
|
|
|
#### 16. `@Entity`
|
|
- **설명**: 클래스가 JPA 엔티티임을 나타냅니다.
|
|
- **예시**:
|
|
```java
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.Id;
|
|
|
|
@Entity
|
|
public class User {
|
|
@Id
|
|
private Long id;
|
|
private String name;
|
|
}
|
|
```
|
|
|
|
#### 17. `@Id`
|
|
- **설명**: 엔티티의 기본 키를 지정합니다.
|
|
- **예시**:
|
|
```java
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.Id;
|
|
|
|
@Entity
|
|
public class Product {
|
|
@Id
|
|
private Long productId;
|
|
}
|
|
```
|
|
|
|
#### 18. `@Column`
|
|
- **설명**: 필드가 매핑될 데이터베이스 컬럼을 정의합니다.
|
|
- **예시**:
|
|
```java
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.Id;
|
|
import jakarta.persistence.Column;
|
|
|
|
@Entity
|
|
public class Employee {
|
|
@Id
|
|
private Long id;
|
|
|
|
@Column(name = "full_name")
|
|
private String name;
|
|
}
|
|
```
|
|
|
|
#### 19. `@Table`
|
|
- **설명**: 엔티티가 매핑될 데이터베이스 테이블을 지정합니다.
|
|
- **예시**:
|
|
```java
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.Id;
|
|
import jakarta.persistence.Table;
|
|
|
|
@Entity
|
|
@Table(name = "customers")
|
|
public class Customer {
|
|
@Id
|
|
private Long id;
|
|
private String name;
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
### 결론
|
|
위 어노테이션들은 자바 개발에서 필수적인 역할을 합니다. JDK 제공 어노테이션은 기본적인 코드 품질과 가독성을 높이고, 스프링 프레임워크 어노테이션은 의존성 주입과 웹 애플리케이션 개발을 간소화하며, JPA 어노테이션은 데이터베이스와 객체 간 매핑을 쉽게 처리합니다. 프로젝트 요구사항에 따라 적절히 선택하고 사용하면 개발 효율성과 유지보수성을 크게 향상시킬 수 있습니다.
|
|
|
|
추가로 궁금한 점이 있다면 말씀해주세요! |