๐ ์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ
- ์์ฑ์ ์ฃผ์ (Constructor Injection)
- ํ๋ ์ฃผ์ (Field Injection)
- ์์ ์ ์ฃผ์ (Setter Injection)
1๏ธโฃ ์์ฑ์ ์ฃผ์ (Constructor Injection)
์์ฑ์ ์ฃผ์ ๋ฐฉ๋ฒ์ ์์ฑ์๋ฅผ ํตํด ์์กด ๊ด๊ณ๋ฅผ ์ฃผ์ ํ๋ค.
์์ฑ์ ์ฃผ์ ์ ์์ฑ์์ ํธ์ถ ์์ ์ 1ํ ํธ์ถ ๋๋ ๊ฒ์ด ๋ณด์ฅ๋๊ธฐ ๋๋ฌธ์ ์ฃผ์ ๋ฐ์ ๊ฐ์ฒด๊ฐ ๋ณํ์ง ์๊ฑฐ๋, ๋ฐ๋์ ๊ฐ์ฒด์ ์ฃผ์ ์ด ํ์ํ ๊ฒฝ์ฐ์ ๊ฐ์ ํ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์๋ค.
@Service
public class ProductService {
// final์ ๋ถ์ผ ์ ์์
private final ProductRepository productRepository;
@Autowired // ์๋ต ๊ฐ๋ฅ
public ProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
}
Spring ํ๋ ์์ํฌ์์๋ ์์ฑ์ ์ฃผ์ ์ ์ ๊ทน ์ง์ํด ์์ฑ์๊ฐ 1๊ฐ๋ง ์์ ๊ฒฝ์ฐ์ @Autowired๋ฅผ ์๋ตํด๋ ์ฃผ์ ์ด ๊ฐ๋ฅํ๋๋ก ํธ์์ฑ์ ์ ๊ณตํ๊ณ ์๋ค.
๋ณด๋์ค ์ด๋์ final ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์์ฑ์ ์ฃผ์ ์, ์์กด์ฑ ์ฃผ์ ์ด ํด๋์ค ์ธ์คํด์คํ ์ค์ ์์๋๋ฏ๋ก final ์ ์ธ์ ํ ์ ์๋ค. ๋ฐ๋ฉด์, final๋ก ์ ์ธ๋ ๋ ํผ๋ฐ์ค ํ์ ๋ณ์๋ ๋ฐ๋์ ์ ์ธ๊ณผ ํจ๊ป ์ด๊ธฐํ๊ฐ ๋์ด์ผ ํ๋ฏ๋ก ํ๋ ์ฃผ์ ๋ฐฉ์์ด๋ ์์ ์ ์ฃผ์ ์ ์์กด๊ด๊ณ ์ฃผ์ ์ ๋ฐ์ ํ๋์ final์ ์ ์ธํ ์ ์๋ค.
2๏ธโฃ ์์ ์ ์ฃผ์ (Setter Injection)
์์ ์ ์ฃผ์ ๋ฐฉ๋ฒ์ ํ๋ ๊ฐ์ ๋ณ๊ฒฝํ๋ Setter ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์์กด์ฑ์ ์ฃผ์ ํ๋ค.
Setter ์ฃผ์ ์ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ฅผ ํตํด ์ฌ๋ฌ๋ฒ ํธ์ถ์ด ๊ฐ๋ฅํ๋ฏ๋ก ์์ฑ์ ์ฃผ์ ๊ณผ ๋ค๋ฅด๊ฒ 1ํ ํธ์ถ์ ๋ณด์ฅํ์ง ๋ชปํ๋ค.
์ฃผ์ ๋ฐ๋ ๊ฐ์ฒด๊ฐ ๋ณ๊ฒฝ๋ ๊ฐ๋ฅ์ฑ์ด ์๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค. (์ค์ ๋ก ๋ณ๊ฒฝ์ด ํ์ํ ๊ฒฝ์ฐ๋ ๊ทนํ ๋๋ฌผ๋ค.)
@Service
public class ProductService {
private ProductRepository productRepository;
@Autowired
public setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
}
์์ ์ ์ฃผ์ ๋ฐฉ์์ @Autowired๋ก ์ฃผ์ ํ ๋์์ด ์๋ ๊ฒฝ์ฐ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
์ฃผ์ ํ ๋์์ด ์์ด๋ ๋์ํ๋๋ก ํ๋ ค๋ฉด @Autowired(required = false)๋ฅผ ํตํด ์ค์ ํ ์ ์๋ค.
3๏ธโฃ ํ๋ ์ฃผ์ (Field Injection)
ํ๋ ์ฃผ์ ๋ฐฉ๋ฒ์ ํ๋์ ์ง์ ์์กด์ฑ์ ๋ถ์ฌํ๋ ๋ฐฉ๋ฒ์ด๋ค.
ํ๋ ์ฃผ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง์ง๋ง ์ธ๋ถ์์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ๋จ์ ์ด ์๋ค.
์ด๋ ํ ์คํธ ์ฝ๋์ ์ค์์ฑ์ด ๋ถ๊ฐ๋จ์ ๋ฐ๋ผ ํ๋์ ๊ฐ์ฒด๋ฅผ ์์ ํ ์ ์๋ ํ๋ ์ฃผ์ ์ ๊ฑฐ์ ์ฌ์ฉ๋์ง ์๊ฒ ๋์๋ค.
(ํ๋ ์ฃผ์ ์ ์์กด์ฑ์ ์ง์ ์ ์ผ๋ก ์ฃผ์ ๋ฐ๊ธฐ ๋๋ฌธ์, ํ ์คํธ ์ ๋ชจ์(mock) ๊ฐ์ฒด๋ฅผ ์ฝ๊ฒ ์ฃผ์ ํ ์ ์์ด ํ ์คํธ ์์ฑ์ด ์ด๋ ค์์ง๋ค.)
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
}
IntelliJ์์ ํ๋ ์ฃผ์ ์ ์ฌ์ฉํ๋ฉด Field injection is not recommended ์ด๋ผ๋ ๊ฒฝ๊ณ ๋ฌธ๊ตฌ๊ฐ ๋ฐ์ํ๋ค.
Spring Team recommends: “Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies”.
ํต์ฌ์ ์์ฑ์ ์ฃผ์ ๋ฐฉ์์ ์ด์ฉํ๋ผ๋ ๊ฒ์ธ๋ฐ, Spring Team์์ ์์ฑ์ ์ฃผ์ ๋ฐฉ์์ ๊ถ์ฅํ๋ ์ด์ ๋ ๋ฌด์์ผ๊น?
๐ ์ ์์ฑ์ ์ฃผ์ ๋ฐฉ์์ด ๊ถ์ฅ๋๋๋ฐ?
- ์ํ ์ฐธ์กฐ ๋ฐฉ์ง
- ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌ๋ ์์ ์ ์ํ ์ฐธ์กฐ ์๋ฌ ์๋ฐฉ ๊ฐ๋ฅ
- ํ
์คํธ ์ฉ์ด
- ๋จ์ POJO๋ฅผ ์ด์ฉํ ํ ์คํธ ์ฝ๋ ์์ฑ ๊ฐ๋ฅ
- ์คํ๋ง์ ๋ ๋ฆฝ์ ์ธ ์ฝ๋ ์์ฑ
- ๊ฐ์ฒด ๋ถ๋ณ์ฑ(Immutability) ํ๋ณด
- ์คํ ์ค์ ๊ฐ์ฒด๊ฐ ๋ณํ๋ ๊ฒ ๋ฐฉ์ง
→ ์ค๋ฅ ์ฌ์ ๋ฐฉ์ง
- ์คํ ์ค์ ๊ฐ์ฒด๊ฐ ๋ณํ๋ ๊ฒ ๋ฐฉ์ง
- final ํค์๋ ์์ฑ ๋ฐ Lombok๊ณผ์ ๊ฒฐํฉ
๐ฑ ์ํ ์ฐธ์กฐ ๋ฐฉ์ง
์์ฑ์ ์ฃผ์ ์ฌ์ฉ ์ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌ๋ ์์ (๊ฐ์ฒด์ ์์ฑ ์ง์ )์ ์ํ ์ฐธ์กฐ ์๋ฌ๋ฅผ ์๋ฐฉํ ์ ์๋ค.
์๋ฅผ๋ค์ด ๋ค์๊ณผ ๊ฐ์ด ํ๋ ์ฃผ์ ์ ์ฌ์ฉํด ์๋ก ํธ์ถํ๋ ์ฝ๋๊ฐ ์๋ค.
@Service
public class ProductService {
@Autowired
private ReviewService reviewService;
@Override
public List<Review> getReview(Long productId) {
return reviewService.getReview(productId);
}
}
ProductService๊ฐ ์ด๋ฏธ ReviewService์ ์์กดํ๊ณ ์๋๋ฐ, ReviewService๋ ProductService์ ์์กดํ๊ณ ์๋ค.
@Service
public class ReviewService {
@Autowired
private ProductService productService;
@Override
public List<Review> getProductReview(Long productId) {
return productService.getReview(productId);
}
}
์์ ๋ ๋ฉ์๋๋ ์๋ก๋ฅผ ๊ณ์ ํธ์ถํ ๊ฒ์ด๊ณ , ๋ฉ๋ชจ๋ฆฌ์ ํจ์์ CallStack์ด ๊ณ์ ์์ฌ StackOverflow ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค.
Caused by: java.lang.StackOverflowError: null
at com.example.ProductService.getReview(ProductService.java:20) ~[main/:na]
at com.example.ReviewService.getProductReview(ReviewService.java:14) ~[main/:na]
at com.example.ProductService.getReview(ProductService.java:20) ~[main/:na]
at com.example.ReviewService.getProductReview(ReviewService.java:14) ~[main/:na]
๋ฌธ์ ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์๋ฌด ์ค๋ฅ๊ฐ ๊ฒฝ๊ณ ์์ด ๊ตฌ๋๋ผ ์ค์ ์ฝ๋๊ฐ ํธ์ถ๋๊ธฐ ์ ๊น์ง ๋ฌธ์ ๋ฅผ ๋ฐ๊ฒฌํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
๊ทธ๋ ๋ค๋ฉด ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํ ๊ฒฝ์ฐ์๋ ์ด๋ป๊ฒ ๋ ๊น?
@Service
public class ProductService {
private final ReviewService reviewService;
public ReviewService(ReviewService reviewService) {
this.reviewService = reviewService;
}
}
@Service
public class ReviewService {
private final ProductService productService;
public ProductService(ProductService productService) {
this.productService = productService;
}
}
์ ํ๋ฆฌ์ผ์ด์ ์คํ ์ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌ๋ ์์ (๊ฐ์ฒด์ ์์ฑ ์ง์ )์ BeanCurrentlyInCreationException์ด ๋ฐ์ํ์ฌ ์ค๋ฅ๋ฅผ ์ฌ์ ์ ์ ์ ์๋ค.
Description:
The dependencies of some of the beans in the application context form a cycle:
โโโโโโโ
| reviewService defined in file [~~~\ReviewService.class]
↑ ↓
| productService defined in file [~~~\ProductService.class]
โโโโโโโ
๐ค ์ ์ด๋ฐ ์ฐจ์ด๊ฐ ๋ ๊น?
์ด๋ฌํ ์คํ ๊ฒฐ๊ณผ์ ์ฐจ์ด๋ ์์ฑ์ ์ฃผ์ ์ Bean์ ์ฃผ์ ํ๋ ์์๊ฐ ํ๋ ์ฃผ์ ๊ณผ ์์ ์ ์ฃผ์ ๊ณผ๋ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
@Autowired๋ฅผ ์ด์ฉํ ํ๋ ์ฃผ์ ์ ํ์ ๋ ์ ํ๋ฆฌ์ผ์ด์ ๊ตฌ๋ ์์ ์ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋ ์ด์ ๋ ๋น์ ์์ฑ๊ณผ ์กฐ๋ฆฝ(@Autowired) ์์ ์ด ๋ถ๋ฆฌ๋์ด ์๊ธฐ ๋๋ฌธ์ด๋ค. ์์ฑ์ ์ฃผ์ ์ ๊ฐ์ฒด์ ์์ฑ๊ณผ ์กฐ๋ฆฝ(์์กด๊ด๊ณ ์ฃผ์ )์ด ๋์์ ์คํ๋ผ ์์ ๊ฐ์ ์๋ฌ๋ฅผ ์ฌ์ ์ ์ก์ ์ ์๋ค. ํ์ง๋ง @Autowired๋ ๋ชจ๋ Bean ์์ฑ์ด ์๋ฃ๋ ํ์ ์กฐ๋ฆฝ(์์กด๊ด๊ณ ์ฃผ์ )์ด ์ฒ๋ฆฌ๋ผ ํธ์ถ์ด ๋๊ณ ๋์์ผ ์ํ ์ด์๋ฅผ ํ์ธํ ์ ์๋ ๊ฒ์ด๋ค.
์ฐธ๊ณ ๋ก Spring Boot 2.6 ๋ถํฐ๋ ๊ธฐ๋ณธ์ ์ผ๋ก Bean ๊ฐ์ ์ํ ์ฐธ์กฐ๊ฐ ๊ธ์ง๋๋๋ก ๋ณ๊ฒฝ๋์๋ค.
๐ฑ ํ ์คํธ์ ์ฉ์ด
์์ฑ์ ์ฃผ์ ์ฌ์ฉ ์ ํ ์คํธ ์ฝ๋๋ฅผ ๋ ํธ๋ฆฌํ๊ฒ ์์ฑํ ์ ์๋ค.
ํ ์คํธ๊ฐ ํน์ ํ๋ ์์ํฌ์ ์์กดํ๋ ๊ฒ์ ํ ์คํธ ๊ฒฉ๋ฆฌ๊ฐ ์ด๋ ต๊ณ ์ ์ฐ์ฑ์ด ๊ฐ์ํด ์ข์ง ์๋ค. ๋ฐ๋ผ์ ๋ ๋ฆฝ์ ์ผ๋ก ์ธ์คํด์คํ๊ฐ ๊ฐ๋ฅํ POJO(Plain Old Java Object)๋ก ์์ฑํ๋ ๊ฒ์ด ์ข์๋ฐ, ์์ฑ์ ์ฃผ์ ์ด ์๋ ๋ค๋ฅธ ์ฃผ์ ์ผ๋ก ์์ฑ๋ ์ฝ๋๋ ์์ ์๋ฐ ์ฝ๋๋ก ๋จ์ ํ ์คํธ๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์ด๋ ต๋ค.
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public void createProduct(String name) {
productRepository.create(name);
}
}
์๋ฅผ๋ค์ด ์์ ๊ฐ์ ํ๋ ์ฃผ์ ๋ฐฉ์ ์ฝ๋์ ๋ํด ์์ ์๋ฐ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์๋ค.
public class ProductServiceTest {
@Test
public void createProductTest() {
ProductService productService = new ProductService();
productService.createProduct("์ํ1");
}
}
์์ ํ ์คํธ ์ฝ๋๋ Spring ์์์ ๋์ํ์ง ์์ผ๋ฏ๋ก ์์กด๊ด๊ณ ์ฃผ์ ์ด ๋์ง ์๋๋ค. ๋ฐ๋ผ์ postRepository๊ฐ null์ด ๋์ด findById ํธ์ถ ์ NPE๊ฐ ๋ฐ์ํ ๊ฒ์ด๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด Setter ์ฌ์ฉ ์ ๋ณ๊ฒฝ ๊ฐ๋ฅ์ฑ์ ์ด์ด๋๊ฒ ๋๋ ๋จ์ ์ ๊ฐ๊ฒ ๋๋ค.
๋ฐ๋๋ก ํ ์คํธ ์ฝ๋์์ @Autowired๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์คํ๋ง์ ์ฌ์ฉํ๋ฉด ๋จ์ ํ ์คํธ๊ฐ ์๋ ๋ฟ ์๋๋ผ, ์ปดํฌ๋ํธ๋ค์ ๋ฑ๋กํ๊ณ ์ด๊ธฐํํ๋ ์๊ฐ ๋๋ฌธ์ ํ ์คํธ ๋น์ฉ์ด ์ฆ๊ฐํ๊ฒ ๋๋ค.
๋ฐ๋ฉด์ ์์ฑ์ ์ฃผ์ ์ฌ์ฉ ์ ์ปดํ์ผ ์์ ์ ๊ฐ์ฒด๋ฅผ ์ฃผ์ ๋ฐ์ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ผ๋ฉฐ, ์ฃผ์ ํ๋ ๊ฐ์ฒด๊ฐ ๋๋ฝ๋ ๊ฒฝ์ฐ ์ปดํ์ผ ์์ ์ ์ค๋ฅ๋ฅผ ๋ฐ๊ฒฌํ ์ ์๋ค.
๐ฑ ์คํ๋ง์ ๋ ๋ฆฝ์ ์ธ ์ฝ๋ ์์ฑ
ํ๋ ์ฃผ์ ์ฌ์ฉ ์ @Autowired๋ฅผ ์ด์ฉํด์ผ ํ๋๋ฐ, ์ด๊ฒ์ ์คํ๋ง์ด ์ ๊ณตํ๋ ์ด๋ ธํ ์ด์ ์ด๋ค.
๊ทธ๋ฌ๋ฏ๋ก @Autowired๋ฅผ ์ฌ์ฉํ๋ฉด ProductService์ ์คํ๋ง ์์กด์ฑ์ด ์๊ธด๋ค.
import org.springframework.beans.factory.annotation.Autowired;
// ์คํ๋ง ์์กด์ฑ์ด ProductService์ import๋์ด ์ฝ๋๋ก ๋ฐํ
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
}
์ฌ์ฉํ๋ ํ๋ ์์ํฌ๋ ์ธ์ ๋ฐ๋์ง ๋ชจ๋ฅด๊ณ , ํ๋ ์์ํฌ๋ ๋น์ฆ๋์ค ๋ก์ง์ ์์ฑํ๋ ์๋น์ค ๊ณ์ธต์์ ์์์ผ ํ ๋์์ด ์๋๋ค.
์ด๋ ํ์ํ ์๋ฐ ํ์ผ์ importํด์ผํ๋ ์ ์ ์ธ์ด์ธ ์๋ฐ์ ํ๊ณ์ด๊ธฐ๋ ํ๋, ๊ฐ๋ฅํ ์คํ๋ง ์์ด ์ฝ๋๊ฐ ์์ฑ๋๋ฉด ๋์ฑ ์ ์ฐํ ์ฝ๋ ํ๋ณด๊ฐ ๊ฐ๋ฅํ๋ค.
๐ฑ ๊ฐ์ฒด ๋ถ๋ณ์ฑ(Immutability) ํ๋ณด
์ค์ ๊ฐ๋ฐ์ ํ๋ค๋ณด๋ฉด ์์กด ๊ด๊ณ์ ๋ณ๊ฒฝ์ด ํ์ํ ์ํฉ์ ๊ฑฐ์์๋ค. ํ์ง๋ง ์์ ์ ์ฃผ์ ์ด๋ ์ผ๋ฐ ๋ฉ์๋ ์ฃผ์ ์ ์ด์ฉํ๋ฉด ๋ถํ์ํ๊ฒ ์์ ์ ๊ฐ๋ฅ์ฑ์ ์ด์ด๋์ด ์ ์ง๋ณด์์ฑ์ ๋จ์ด๋จ๋ฆฐ๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์์ฑ์ ์ฃผ์ ์ ํตํด ๋ณ๊ฒฝ์ด ๊ฐ๋ฅ์ฑ์ ๋ฐฐ์ ํ๊ณ ๋ถ๋ณ์ฑ์ ๋ณด์ฅํ๋๊ฒ์ด ์ข๋ค.
์ด๋ ์ค๋ฅ๋ฅผ ์ฌ์ ์ ๋ฐฉ์งํ ์ ์๋ ์ฅ์ ์ ๊ฐ์ง๊ธฐ๋ ํ๋ค.
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public void someMethod() {
productRepository = null;
productRepository.call();
}
}
ํ๋ ์ฃผ์ ์ ์ฌ์ฉํ ์ฝ๋๋ final ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๋ฐ๋ผ์ ๋ฐํ์ ์์ ์ ๋ณ๊ฒฝ๋ ์ ์๋ค.
์ ์ฝ๋์์๋ null์ ์ฐธ์กฐํ๋๋ก ๋ณ๊ฒฝํ๊ธฐ ๋๋ฌธ์ NullPointerException์ด ๋ฐ์ํ ๊ฒ์ด๋ค.
ํ์ง๋ง ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํ๋ค๋ฉด ์ด์ ๊ฐ์ ์ํฉ์ ์ปดํ์ผ ์์ ์ ๋ฐฉ์งํ ์ ์๋ค.
@Service
public class ProductService {
private final ProductRepository productRepository;
public ProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public void someMethod() {
// final ๋ณ์์ ๊ฐ์ ์ฌํ ๋น ํ ์ ์์
productRepository = null;
}
}
๐ฑ final ํค์๋ ์์ฑ ๋ฐ Lombok๊ณผ์ ๊ฒฐํฉ
์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํ๋ฉด ํ๋ ๊ฐ์ฒด์ final ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ ์ปดํ์ผ ์์ ์ ๋๋ฝ๋ ์์กด์ฑ์ ํ์ธํ ์ ์๋ค.
๋ฐ๋ฉด์ ๋ค๋ฅธ ์ฃผ์ ๋ฐฉ๋ฒ๋ค์ ๊ฐ์ฒด์ ์์ฑ(์์ฑ์ ํธ์ถ) ์ดํ์ ํธ์ถ๋๋ฏ๋ก final ํค์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
final ํค์๋๋ก ํ๋๋ฅผ ๋ช ์์ ์ผ๋ก ์ ์ธํ๋ฉด ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์์ ์ฑ์ ๋์ผ ์ ์์ผ๋ฉฐ, ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค์ด ํด๋น ํ๋๊ฐ ๋ถ๋ณ์ด๋ผ๋ ๊ฒ์ ์ ์ ์์ด ์ฝ๋์ ์ดํด๋ฅผ ๋์ธ ์ ์๋ ์ฅ์ ์ด ์๋ค.
๋ํ final ํค์๋๋ฅผ ๋ถ์ด๋ฉด Lombok๊ณผ ๊ฒฐํฉ๋์ด ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ์์ฑํ ์ ์๋ค. Lombok์๋ final ๋ณ์๋ฅผ ์ํ ์์ฑ์๋ฅผ ๋์ ์์ฑํด์ฃผ๋ @RequiredArgsConstructor๊ฐ ์๋๋ฐ ์์ฑ์ ์ฃผ์
์ฝ๋๋ฅผ Lombok๊ณผ ๊ฒฐํฉ์ํค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๊ฐํธํ๊ฒ ์์ฑํ ์ ์๋ค.
@Service
@RequiredArgsConstructor
public class ProductService {
private final ProductRepository productRepository;
private final UserRepository userRepository;
public void createProduct(String name) {
productRepository.create(name);
}
}
[Spring] ๋ค์ํ ์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ๊ณผ ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด์ผ ํ๋ ์ด์ - (2/2)
Spring ํ๋ ์์ํฌ์ ํต์ฌ ๊ธฐ์ ์ค ํ๋๊ฐ ๋ฐ๋ก DI(Dependency Injection, ์์กด์ฑ ์ฃผ์ )์ด๋ค. Spring ํ๋ ์์ํฌ์ ๊ฐ์ DI ํ๋ ์์ํฌ๋ฅผ ์ด์ฉํ๋ฉด ๋ค์ํ ์์กด์ฑ ์ฃผ์ ์ ์ด์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ๊ฐ๊ฐ์ ๋ฐฉ
mangkyu.tistory.com
์์ฑ์ ์ฃผ์ ์ @Autowired๋ฅผ ์ฌ์ฉํ๋ ํ๋ ์ฃผ์ ๋ณด๋ค ๊ถ์ฅํ๋ ํ๋ ์ด์
@Autowired๋ฅผ ์ฌ์ฉํ๋ ์์กด์ฑ ์ฃผ์ ๋ณด๋ค ์์ฑ์ ์ฃผ์ (Constructor Injection)์ ๋ ๊ถ์ฅํ๋ ์ด์ ๋ ๋ฌด์์ผ๊น?
madplay.github.io
์์กด ๊ด๊ณ ์ฃผ์ (DI) - spring
์๋ฅผ๋ค์ด, ์ฝ๋์์ ์์ฃผ ์ฌ์ฉํ๋ ๋ฉ์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ปค๋ฅ์ ์ ํ๋ํ๋ ๋น์ด ์๊ณ , ์ฝ๋์์ ํน๋ณํ ๊ธฐ๋ฅ์ผ๋ก ๊ฐ๋ ์ฌ์ฉํ๋ ์๋ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ปค๋ฅ์ ์ ํ๋ํ๋ ์คํ๋ง ๋น์ด ์๋ ๊ฒฝ
dahye-jeong.gitbook.io
Field injection is not recommended – Spring IOC - Marc Nuri
When running a static code analysis tool or inspecting/analyzing your code from your IDE, you may have encountered the following warning regarding your @Autowired fields: Field injection is not recommended. This post shows the alternatives to field injecti
blog.marcnuri.com
๐Dependency Injection in Spring: Constructor, Property, or Setter? Which one should I choose?
Dependency injection is a programming technique in which objects are created with all the dependencies they need to function properly…
medium.com
[Spring] ๋ค์ํ ์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ๊ณผ ์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด์ผ ํ๋ ์ด์ - (2/2)
Spring ํ๋ ์์ํฌ์ ํต์ฌ ๊ธฐ์ ์ค ํ๋๊ฐ ๋ฐ๋ก DI(Dependency Injection, ์์กด์ฑ ์ฃผ์ )์ด๋ค. Spring ํ๋ ์์ํฌ์ ๊ฐ์ DI ํ๋ ์์ํฌ๋ฅผ ์ด์ฉํ๋ฉด ๋ค์ํ ์์กด์ฑ ์ฃผ์ ์ ์ด์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ๊ฐ๊ฐ์ ๋ฐฉ
mangkyu.tistory.com
'Back-end' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] EntityManager ํต์ฌ ๊ธฐ๋ฅ (0) | 2024.04.19 |
---|---|
[Spring] Spring REST Docs ๊ถํ ์๋ฌ (2) | 2024.02.29 |
[JPA] ์์์ฑ ์ปจํ ์คํธ์ ๋์์๋ฆฌ์ ์ด์ (0) | 2024.02.14 |
[JPA] OSIV(Open Session In View)๋? (0) | 2023.09.21 |
[JPA] Hibernate Cache๋? (0) | 2023.09.03 |