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 ๋ก ์ ์ธํ ์ ์๋ค.
'๐ฉ๐ปโ๐ป Developer > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] ์คํ๋ง์ด ์ฃผ๊ธฐ์ ์ผ๋ก ์์กดํ๋ ๊ฒ์? (0) | 2024.03.23 |
---|---|
[Spring] ์คํ๋ง ๋ถํธ์์ auto-configuration์ ๋นํ์ฑํํ๋ ๋ฐฉ๋ฒ (0) | 2024.03.21 |
[Spring] traceld ์ spanld๋? (0) | 2024.03.20 |
[spring boot] Spring Boot ์์ํ๊ธฐ (0) | 2024.03.02 |
[spring] Spring ๊ณผ Spring Boot ์ ์ฐจ์ด (0) | 2024.03.01 |
๋๊ธ