๐Ÿ’š Spring

[Spring] Spring Annotation 1

Genie_. 2024. 11. 18. 23:40
728x90
๋ฐ˜์‘ํ˜•

Spring ์˜์กด์„ฑ ์ฃผ์ž… ๊ด€๋ จ Annotation

@Component

์ด ํด๋ž˜์Šค๋ฅผ Spring Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ฒ ๋‹ค๋Š” ํ‘œ์‹œ์ด๋‹ค.

์ฃผ๋กœ Service, Repository, Controller์™€ ๊ฐ™์€ ํŠน์ • ์—ญํ• ์„ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š๋Š” ์ผ๋ฐ˜์ ์ธ ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

@Component
public class MyComponent {
    public String sayHello() {
        return "Component";
    }
}

 

@Controller

  • ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ(Request)์„ ์ฒ˜๋ฆฌํ•˜๊ณ , ์‘๋‹ต(Response)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ณ„์ธต(Presentation Layer)์— ์‚ฌ์šฉ๋œ๋‹ค.
  • ์ฃผ๋กœ HTTP ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

  • ํŠน์ง•
    • HTTP ์š”์ฒญ์„ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด @RequestMapping ๋˜๋Š” ๋‹ค๋ฅธ HTTP ๋ฉ”์„œ๋“œ ๊ด€๋ จ ์• ๋…ธํ…Œ์ด์…˜(@GetMapping, @PostMapping ๋“ฑ)๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.
    • View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.ui.Model;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String sayHello(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
        model.addAttribute("name", name);
        return "hello"; // View ์ด๋ฆ„ ๋ฐ˜ํ™˜ (์˜ˆ: hello.html)
    }
}

 

 

@Service

  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค ๊ณ„์ธต(Service Layer)์— ์‚ฌ์šฉ๋œ๋‹ค.
  • ์ปจํŠธ๋กค๋Ÿฌ์™€ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต(Repository) ์‚ฌ์ด์—์„œ ์ค‘๊ฐ„๋‹ค๋ฆฌ ์—ญํ• ์„ ํ•œ๋‹ค.
  • ํŠน์ง• : ๋ณต์žกํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
import org.springframework.stereotype.Service;

@Service
public class GreetingService {

    public String getGreeting(String name) {
        return "Hello, " + name + "!";
    }
}

 

 

@Repository

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ๋‹ด๋‹นํ•˜๋Š” ์˜์†์„ฑ ๊ณ„์ธต(Persistence Layer)์— ์‚ฌ์šฉ๋œ๋‹ค.
  • ํŠน์ง•:
    • Spring์ด ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ด€๋ จ ์˜ˆ์™ธ๋ฅผ Spring DataAccessException์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋„๋ก ์ง€์›ํ•œ๋‹ค.
    • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ CRUD ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    User findByUsername(String username);
}

์ „์ฒด ๊ตฌ์กฐ ์˜ˆ์‹œ

Controller

@Controller
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable Long id, Model model) {
        User user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user-detail"; // View ์ด๋ฆ„
    }
}

Service

@Service
public class UserService {

    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User findUserById(Long id) {
        return userRepository.findById(id)
                             .orElseThrow(() -> new RuntimeException("User not found"));
    }
}

Repository

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // JpaRepository๊ฐ€ ๊ธฐ๋ณธ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ ์™ธ์—๋„ ํ•„์š”ํ•œ ์ปค์Šคํ…€ ๋ฉ”์„œ๋“œ ์„ ์–ธ ๊ฐ€๋Šฅ
}

 

728x90
๋ฐ˜์‘ํ˜•