[Spring] ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ 3๊ฐ์ง ๋ฐฉ๋ฒ, @RequiredArgsConstructor
์์กด์ฑ ์ฃผ์ (DI, Dependency Injection)์ด๋?
์์กด์ฑ ์ฃผ์ ์ ํ์ํ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒ์ด ์๋๋ผ ์ธ๋ถ๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ๋ฐ์์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
์ด๋ฅผ ํตํด ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ์ค์ด๊ณ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ผ ์ ์๋ค.
์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ
Spring์ @Autowired ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
@Autowired ์ด๋ ธํ ์ด์ ์ Spring์๊ฒ ์์กด์ฑ์ ์ฃผ์ ํ๋ ์ง์์ ์ญํ ๋ก ์ฐ์ธ๋ค.
1. ์์ฑ์ ์ฃผ์ (Constructor Injection)
@Service
public class SpaceWallFindService {
private final SpaceWallRepository spaceWallRepository;
private final BlockStrategyFactory blockStrategyFactory;
private final BlockJsonProcessor jsonProcessor;
@Autowired
public SpaceWallFindService(final SpaceWallRepository spaceWallRepository, final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor jsonProcessor) {
this.spaceWallRepository = spaceWallRepository;
this.blockStrategyFactory = blockStrategyFactory;
this.jsonProcessor = jsonProcessor;
}
}
์คํ๋ง 4.3 ๋ฒ์ ์ดํ์๋ ํด๋์ค์ ์์ฑ์๊ฐ ํ๋๋ง ์๊ณ , ๊ทธ ์์ฑ์์ ํ๋ผ๋ฏธํฐ๋ค์ด ์ด๋ฏธ Bean์ผ๋ก ๋ฑ๋ก๋์ด ์์ผ๋ฉด @Autowired ์ด๋ ธํ ์ด์ ์ ์๋ต ๊ฐ๋ฅํ๋ค.
1-1) @RequiredArgsConstructor
@RequiredArgsConstructor ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ์ด๊ธฐํ๋์ง ์์ final ํ๋๋, @NotNull ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํ๋์ ๋ํด ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํด ์ค๋ค.
@RequiredArgsConstructor
@Service
public class SpaceWallFindService {
private final SpaceWallRepository spaceWallRepository;
private final BlockStrategyFactory blockStrategyFactory;
private final BlockJsonProcessor jsonProcessor;
}
2. ํ๋ ์ฃผ์ (Field Injection)
@Service
public class SpaceWallFindService {
@Autowired
private final SpaceWallRepository spaceWallRepository;
@Autowired
private final BlockStrategyFactory blockStrategyFactory;
@Autowired
private final BlockJsonProcessor jsonProcessor;
}
ํ๋์ @Autowired ์ด๋ ธํ ์ด์ ๋ง ๋ถ์ฌ์ฃผ๋ฉด ์๋์ผ๋ก ์์กด์ฑ ์ฃผ์ ์ด ๋๋ค.
3. ์์ ์ ์ฃผ์ (Setter Injection)
@Service
public class SpaceWallFindService {
private final SpaceWallRepository spaceWallRepository;
private final BlockStrategyFactory blockStrategyFactory;
private final BlockJsonProcessor jsonProcessor;
@Autowired
public void setSpaceWallFindService(final SpaceWallRepository spaceWallRepository, final BlockStrategyFactory blockStrategyFactory,
final BlockJsonProcessor jsonProcessor) {
this.spaceWallRepository = spaceWallRepository;
this.blockStrategyFactory = blockStrategyFactory;
this.jsonProcessor = jsonProcessor;
}
}
Setter ๋ฉ์๋์ @Autowired ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด ์ค Spring Framework reference์์ ๊ถ์ฅํ๋ ๋ฐฉ๋ฒ์ ์์ฑ์๋ฅผ ํตํ ์ฃผ์ ์ด๋ค.
์ด์ ๋ 1. ์ํ ์ฐธ์กฐ ๋ฐฉ์ง 2. ๋ถ๋ณ์ฑ ๋ณด์ฅ 3. ํ ์คํธ์ ์ฉ์ดํ๊ธฐ ๋๋ฌธ์ด๋ค.
์์ฑ์ ์ฃผ์ ์ ์ฌ์ฉํด์ผ ํ๋ ์ด์
1. ์ํ ์ฐธ์กฐ ๋ฐฉ์ง
์ํ ์ฐธ์กฐ๋๋ ์ฝ๋๊ฐ ์์ ๊ฒฝ์ฐ ํ๋ ์ฃผ์ ๊ณผ ์ธํฐ ์ฃผ์ ์ ๋น์ด ์์ฑ๋ ํ(๊ฐ์ฒด ์์ฑ ํ)์ ์ฐธ์กฐ๋ฅผ ํ๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์์ ์ ์ค๋ฅ๊ฐ ๊ฐ์ง๋์ง ์๊ณ , ์ฝ๋๊ฐ ํธ์ถ๋์ด์ผ๋ง ์ ์ ์๋ค.
๋ฐ๋ฉด ์์ฑ์๋ฅผ ํตํด ์ฃผ์ ํ ๊ฒฝ์ฐ ์ปดํ์ผ ์์ ์ ์ค๋ฅ๊ฐ ๊ฐ์ง๋์ด ์ํ ์ฐธ์กฐ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
๊ทธ ์ด์ ๋ ์์ฑ์ ์ฃผ์ ์ ํตํ ์์กด์ฑ ์ฃผ์ ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ๋ชจ๋ ์์กด์ฑ์ด ๋ช ์์ ์ผ๋ก ์ ์ธ๋์ด ์์ด์ผ ํ๋๋ฐ
์ํ ์ฐธ์กฐ๋๋ ๊ตฌ์กฐ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ์์ ์ ์ค๋ฅ๊ฐ ๊ฐ์ง๋ ์ ์๋ค. BeanCurrentlyInCreationException ์ด ๋ฐ์ํ๋ค.
2. ๊ฐ์ฒด์ ๋ถ๋ณ์ฑ ๋ณด์ฅ
์์ฑ์๋ ๊ฐ์ฒด ์์ฑ ์ดํ์ ๋ค์ ํธ์ถ๋ ์ผ์ด ์๊ธฐ ๋๋ฌธ์ ํด๋น ๊ฐ์ฒด๊ฐ ๋ถ๋ณ ๊ฐ์ฒด์์ ๋ณด์ฅํ๋ค.
๊ฐ์ฒด์ ์์ฑ์๋ ๊ฐ์ฒด ์์ฑ ์ 1ํ๋ง ํธ์ถ๋๋ค๋ ํน์ง์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋ํ final ํ๋๋ ๊ฐ์ฒด ์์ฑ ์ ๋ฐ๋์ ์ด๊ธฐํ๊ฐ ๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์ final ํ๋๋ฅผ ์์ฑ์๋ฅผ ํตํด ์์กด์ฑ ์ฃผ์ ์ผ๋ก ์ด๊ธฐํํ์ฌ ํ๋์ ๋ํ ๋ถ๋ณ์ฑ ๋ณด์ฅ๋ ํ ์ ์๋ค.
๐ ์ฐธ๊ณ ๋งํฌ
[Spring] ์์กด์ฑ ์ฃผ์ 3๊ฐ์ง ๋ฐฉ๋ฒ - (์์ฑ์ ์ฃผ์ , Field ์ฃผ์ , Setter ์ฃผ์ )
Spring์ @Autowired ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ๋ค์ํ ์์กด์ฑ ์ฃผ์ (DI; Dependency Injection) ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค. ์์กด์ฑ ์ฃผ์ ์ ํ์ํ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒ์ด ์๋ ์ธ๋ถ๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ๋ฐ์ ์ฌ์ฉํ๋ ๊ฒ์
dev-coco.tistory.com