ํ ๋๋ง๋ค ํท๊ฐ๋ฆฌ๊ณ
ํ ๋๋ง๋ค ๊น๋จน๋
spring boot ๊ธฐ๋ณธ ๊ฐ๋
์๋ฆฌ ๊ธฐํ ๋ฑ๋ฑ ..
์์ฐ๋์ง ์๊ณ ์ฐ์ !!!
- ํ๋ ์ฃผ์ (@Autotiwerd) โ ๋ด๊ฐ ๊ฐ์ฅ ๋ง์ด ์ผ๋ ๋ฐฉ์
@Service
public class MyService {
@Autowired
private TestService testService;
}
- ์์ฑ์ ์ฃผ์ (@RequiredArgsConstructor) โ ์ข ์ข ์
@Service
@RequiredArgsConstructor
public class MyService {
private final TestService testService;
}
- ์์ ์ ์ฃผ์ (setter)
@Service
public class MyService {
private TestService testService;
@Autowired
public void setTestService(TestService testService) {
this.testService = testService;
}
}
1.ํ๋ ์ฃผ์ ์ ๊ฐ์ฒด๊ฐ ๋จผ์ ๋ง๋ค์ด์ง๊ณ ๋์ค์ ํ๋๊ฐ ์ฑ์์ง๋ค. private ์ธ๋ฐ๋ ๋ฆฌํ๋ ์ ์ผ๋ก ๋ซ๊ณ ๋ค์ด๊ฐ๋ค๊ณ ํ๋ค.
Spring ์ปจํ
์ด๋
โ ๊ฐ์ฒด ์์ฑ (๊ธฐ๋ณธ ์์ฑ์)
โ ๋ฆฌํ๋ ์
์ผ๋ก private ํ๋์ ๊ฐ์ ์ฃผ์
โ ์์ฑ
2.์์ฑ์ ์ฃผ์ ์ ๊ฐ์ฒด๋ฅผ ์์ฑํจ๊ณผ ๋์์ ์ฃผ์ ๋๋ค. final์ด๋ผ ์ดํ ๋ณ๊ฒฝ๋์ง ์๋๋ค.
@RequiredArgsConstructor
public class MyService {
private final FileService fileService;
}
// Lombok์ด ์๋ ์ฝ๋๋ฅผ ์๋ ์์ฑ
public MyService(TestService testService) {
this.testService = testService;
}
Spring ์ปจํ
์ด๋
โ TestService ๋จผ์ ์ค๋น
โ ์์ฑ์ ํธ์ถํ๋ฉด์ ์ฃผ์
โ ๊ฐ์ฒด ์์ฑ
Autowired๊ฐ ํธํ๋ค๊ณ ์๊ฐํด์ ์์ฃผ ์ผ์๋๋ฐ
spring ๊ณต์๋ฌธ์, InteliJ๋ ์์ฑ์ ์ฃผ์
์ ์ฐ๋ ๊ฒ์ ๊ถ์ฅํ๋ค๊ณ ํ๋ค.
final๋ก ๋ถ๋ณ์ฑ ๋ณด์ฅ
์ํ์ฐธ์กฐ๋ฅผ ์ปดํ์ผ/์์ ์์ ์ ์ก์
// A๊ฐ B๋ฅผ ์ฐธ์กฐ, B๊ฐ A๋ฅผ ์ฐธ์กฐ
@Service
public class A {
@Autowired B b; // ํ๋ ์ฃผ์
โ ๋ฐํ์์ ์ฑ ๋จ๊ณ ๋์์ผ ์ค๋ฅ ๋ฐ๊ฒฌ
}
@Service
public class B {
@Autowired A a;
}
// ์์ฑ์ ์ฃผ์
โ ์ฑ ์์ํ ๋ ๋ฐ๋ก ์ค๋ฅ ๋ฐ๊ฒฌ (Spring Boot 2.6+)
// BeanCurrentlyInCreationException ์ฆ์ ๋ฐ์
ํ ์คํธ ์ฝ๋ ์์ฑ์ด ํจ์ฌ ์ฌ์
// ํ๋ ์ฃผ์
- ํ
์คํธํ ๋ Mock ์ฃผ์
์ด ์ด๋ ค์
public class MyServiceTest {
// fileService๋ฅผ ์ด๋ป๊ฒ ๋ฃ์ง..? @Autowired๊ฐ ์์ผ๋ฉด null
MyService myService = new MyService();
}
// ์์ฑ์ ์ฃผ์
- ํ
์คํธํ ๋ ๊ทธ๋ฅ ๋ฃ์ผ๋ฉด ๋จ
public class MyServiceTest {
FileService mockFileService = mock(FileService.class);
MyService myService = new MyService(mockFileService); // ๊น๋
}
์ฌ์ค ์ ๋ชฐ๋ผ์๊ฐ ๊ฐ์ฅ ํฌ๋ค
@RequiredArgsConstructor๋ฅผ ๋ฌด๋ถ๋ณํ๊ฒ ์ฐ๋ฉด ์๋๋ค๋ ๊ฑธ ์ด๋์ ๋ดค๋ ๊ฒ ๊ฐ์๋ฐ(์๋ง Entity์ ์ฐ์ง ๋ง๋ ํฌ์คํธ๋ฅผ ๋ดค๋ ๋ฏ...) ๋๋ฌด ์ด๊นจ๋๋จธ๋ก ๋ด์ ๊ทธ๋ฅ ์ฝ๋๋ ๊ฐ๊ฒฐํ Autowired๋ฅผ ์ผ๋ ๊ฒ ๊ฐ๋ค.
ํ์ง๋ง ๊ทธ๋ ๋ค๊ณ ๋จ์ ์ด๋ ๋ฆฌ์คํฌ๊ฐ ์์ ์๋ ๊ฒ๋ ์๋๋ ์ฐธ๊ณ ํ์ฌ ์ฐ์!
// @AllArgsConstructor โ ๋ชจ๋ ํ๋ ์์ฑ์
// @NoArgsConstructor โ ๊ธฐ๋ณธ ์์ฑ์
// @RequiredArgsConstructor โ final or @NonNull ํ๋๋ง
1.์์กด์ฑ์ด ๋๋ฌด ๋ง์์ง
@RequiredArgsConstructor
public class MyController {
private final ServiceA a;
private final ServiceB b;
private final ServiceC c;
private final ServiceD d;
private final ServiceE e;
// final๋ง ๋ถ์ด๋ฉด ์๋์ผ๋ก ์์ฑ์์ ์ถ๊ฐ๋๋๊น
// ํด๋์ค๊ฐ ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ๊ณ ์๋ค๋ ๊ฑธ ๋์น์ฑ๊ธฐ ์ด๋ ค์
}
2.์์ฑ์ ์์ ์์กด ๋ฌธ์
@RequiredArgsConstructor
public class MyService {
private final AService aService;
private final BService bService;
}
// ์์ฑ์ ์์๊ฐ ํ๋ ์ ์ธ ์์๋ก ๊ฒฐ์ ๋จ
// ๋ค๋ฅธ ๊ณณ์์ ์ง์ ์์ฑ์ ํธ์ถํ ๋ ์์ ํท๊ฐ๋ฆด ์ ์์
new MyService(aService, bService) // โ ์์ ๋ฐ๊พธ๋ฉด ์ปดํ์ผ ์๋ฌ
3.JPA Entity ์ ์ฐ๋ฉด ์ํ
@Entity
@RequiredArgsConstructor // โ ์ํ
public class User {
private final String name;
}
// JPA๋ ๊ธฐ๋ณธ ์์ฑ์(NoArgsConstructor)๊ฐ ํ์ํ๋ฐ
// final ํ๋๋ฉด ๊ธฐ๋ณธ ์์ฑ์ ๋ง๋ค ์ ์์ด์ ์ถฉ๋
controller๋ service์ ์จ๋ ๋์ง๋ง ์์กด์ฑ 5๊ฐ ์ด์์ด ๋๋ฉด ํด๋์ค ๋ถ๋ฆฌ๋ฅผ ๊ณ ๋ คํ๋ ์ชฝ์ผ๋ก ํ์ฌ ์ ~~ ์ฌ์ฉํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค!