๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Developer/Spring

[Spring] ์–ด๋…ธํ…Œ์ด์…˜์˜ ์šฉ๋„

Junyung 2024. 3. 19. 21:17

Annotation

Annotation์ด๋ž€ ์‚ฌ์ „์  ์˜๋ฏธ๋กœ๋Š” "์ฃผ์„" ์ด๋ž€ ๋œป์ด์ง€๋งŒ, 

์ž๋ฐ”์—์„œ๋Š” "์ฝ”๋“œ ์‚ฌ์ด์— ํŠน๋ณ„ํ•œ ์˜๋ฏธ, ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๊ธฐ์ˆ "์ด๋ž€ ๋œป์„ ์˜๋ฏธํ•œ๋‹ค. 

ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ํ”„๋กœ๊ทธ๋žจ์— ๊ด€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ฝ”๋“œ์— ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ •ํ˜•ํ™”๋œ ๋ฐฉ๋ฒ•์ด๋‹ค. 

 

Annotation์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

1. ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง€๋ฉฐ ์žฌ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

2. ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ ๋ฌธ๋ฒ• ์—๋Ÿฌ๋ฅผ ์ฒดํฌํ•˜๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

3. ๋Ÿฐํƒ€์ž„ ์‹œ ํŠน์ • ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

4. ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ํˆด์ด ๋นŒ๋“œ๋‚˜ ๋ฐฐ์น˜์‹œ ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค. 

 

Annotation ์ข…๋ฅ˜

-> ๋งŽ์€ Annotation์ค‘์—์„œ ์ž์ฃผ ์“ฐ์ด๋ฉด์„œ, ์ค‘์š”ํ•œ Annotation์„ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค

@Component

: ์Šคํ”„๋ง์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์ž„์„ ํ‘œ๊ธฐํ•˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฒ”์šฉ ์Šคํ…Œ๋ ˆ์˜คํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. 

์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก์‹œํ‚ค๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. 

Application Contect์—์„œ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ, ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•  ํ›„๋ณด ํด๋ž˜์Šค๋“ค์„ ํƒ์ง€ํ•˜์—ฌ ๋“ฑ๋กํ•œ๋‹ค. 

import org.springframework.stereotype.Component;

@Component
public class MessageService {
    
    public String getMessage() {
        return "Hello, world!";
    }
}

@RequestMapping

: ์–ด๋–ค URL์„ ์–ด๋–ค method๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ง€ ๋งคํ•‘ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. 

Controller๋‚˜ Controller์˜ ๋ฉ”์†Œ๋“œ์— ์ ์šฉํ•œ๋‹ค. 

์š”์ฒญ์„ ๋ฐ›๋Š” ํ˜•์‹์ธ Get/Post/Put/Patch/Delete๋ฅผ ์ •์˜ํ•ด์ฃผ๊ธฐ๋„ ํ•œ๋‹ค. (Default = Get) 

@RequestMapping("/")
public String index(Model model) {
	model.addAttribute("list", boardService.listAll());
    return "index";
 }

@RestController

:Spring MVC์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ RESTful ์›น ์„œ๋น„์Šค์˜ ์—”๋“œ ํฌ์ธํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ด ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋Š” HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ JSON ๋˜๋Š” XML ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, world!";
    }
}

@Service

: ์Šคํ”„๋ง์—์„œ ์„œ๋น„์Šค๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” annotation์ด๋‹ค. 

@Component ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๊ฑฐ์˜ ์ฐจ์ด๊ฐ€ ๋ณ„๋กœ ์—†์ง€๋งŒ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์„œ๋น„์Šค ๋ ˆ์ด์–ด ํด๋ž˜์Šค์ž„์„ ์ง์ ‘์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด์ค€๋‹ค. 

์บก์Аํ™” ์—†์ด ๋ชจ๋ธ ๋‚ด ๋‹จ๋… ์ธํ„ฐํŽ˜์ด์Šค๋กœ์„œ ์ œ๊ณต๋˜๋Š” ๋™์ž‘์„ ์˜๋ฏธํ•œ๋‹ค. 

//์˜ˆ์ œ -> @service์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ๊ธฐ ์„œ๋น„์Šค ์ œ๊ณต
import org.springframework.stereotype.Service;

@Service
public class CalculatorService {

    public int add(int num1, int num2) {
        return num1 + num2;
    }

    public int subtract(int num1, int num2) {
        return num1 - num2;
    }

    public int multiply(int num1, int num2) {
        return num1 * num2;
    }

    public double divide(double num1, double num2) {
        if (num2 == 0) {
            throw new IllegalArgumentException("Cannot divide by zero");
        }
        return num1 / num2;
    }
}

@Repository

: ํŠน์ • ์˜ˆ์™ธ๋ฅผ ์žก์•„ ์Šคํ”„๋ง์˜ unchecked ์˜ˆ์™ธ๋กœ ๋‹ค์‹œ ๋˜์ง„๋‹ค. 

spring์—๊ฒŒ ์ด ํด๋ž˜์Šค๊ฐ€ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค๋ฅผ ๋‹ด๋‹นํ•˜๋Š” Repository๋กœ ์‚ฌ์šฉ๋  ๊ฒƒ์ž„์„ ๋ช…์‹œํ•ด์ค€๋‹ค. 

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserRepository {

    private List<User> users = new ArrayList<>();

    public UserRepository() {
        // ์ดˆ๊ธฐ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
        users.add(new User(1, "John"));
        users.add(new User(2, "Alice"));
        users.add(new User(3, "Bob"));
    }

    public List<User> getAllUsers() {
        return users;
    }

    public User getUserById(int id) {
        for (User user : users) {
            if (user.getId() == id) {
                return user;
            }
        }
        return null;
    }

    public void addUser(User user) {
        users.add(user);
    }

    public void deleteUserById(int id) {
        users.removeIf(user -> user.getId() == id);
    }
}

@Entity

: JPA๋ฅผ ์‚ฌ์šฉํ•ด ์‹ค์ œ DBํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ ํ•  Class์ด๋‹ค ๋ผ๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. 

์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ JPA๊ฐ€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. 

@Entity(name="user")
public class User{}

//final, enum, interface, inner class์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. 
//๊ธฐ๋ณด ์ƒ์„ฑ์ž๊ฐ€ ๊ผญ ํ•„์š”ํ•˜๋‹ค
//ํ•„๋“œ(๋ณ€์ˆ˜)๋ฅผ final ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค.