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
๋ฐ์ํ
'๐ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ํ์ ๊ด๋ฆฌ ์์ (Annotation ์ฌ์ฉX) (0) | 2024.12.12 |
---|---|
[Spring] MVC ํจํด์ด๋? (0) | 2024.12.06 |
[Spring] CRUD - Controller ๊ณต๋ถ (0) | 2024.12.05 |
[Spring] Spring MVC ๊ตฌ์กฐ (0) | 2024.12.02 |
[Spring] .toEntity() .toList() .toString()์ด๋? (0) | 2024.11.05 |