์์ฆ์ OAuth2๋ฅผ ์ฌ์ฉํ์ฌ ์์
๋ก๊ทธ์ธ์ ํตํด ๊ฐ์
์ด ๊ฐ๋ฅํ ์๋น์ค๋ค์ด ๊ต์ฅํ ๋ง๋ค. ์คํ๋ง ์ํ๋ฆฌํฐ์์๋ OAuth2๋ฅผ ์ด์ฉํ ์์
๋ก๊ทธ์ธ ๋ฐฉ์์ ์ง์ํ๋๋ฐ, ํ์๋ ๋งจ ์ฒ์ ์ด๋ฅผ ์ฌ์ฉํ ๋ ๋๋ฌด ์ด๋ ค์ ์๋ค. ์ง๊ธ๋ถํฐ ์ํ๋ฆฌํฐ์์ OAuht2๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ์์๋ณด์. ์์กด์ฑ ๊ด๋ฆฌ ์ฐ์ OAuth2๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด OAuth2-client ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด์ผ ํ๋ค. build.gradle ์ ๋ค์์ ์ถ๊ฐํ์. implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' oauth2Login() ์ ์๋๋ฐฉ์ WebSecurityConfigurerAdapter ๋ฅผ ์์๋ฐ์ Security Config ํ์ผ์์ oauth2 ๋ก๊ทธ์ธ..
@Autowired ์ฌ์ฉ ์ ์กฐํ๋ ๋น์ด 2๊ฐ ์ด์์ผ ๊ฒฝ์ฐ @Autowired๋ฅผ ํตํ ์์กด๊ด๊ณ ์๋ ์ฃผ์
๋ฐฉ๋ฒ์ ์ฌ์ฉํ ๊ฒฝ์ฐ์, ๋์ผํ ํ์
์ ๋น์ด 2๊ฐ ์ด์์ด๋ผ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด๋ ๋๊ฐ์ ๋น๋ค ์ค ์ด๋ ํ ๋น์ ์ฃผ์
ํด์ผ ํ ์ง ๊ฒฐ์ ํ์ง ๋ชปํด์ ๋ฐ์ํ๋ ๋ฌธ์ ์ธ๋ฐ, ๊ฐ๋จํ๊ฒ๋ ๊ตฌ์ฒด ํด๋์ค๋ฅผ ์ฌ์ฉํ๊ฑฐ๋, ๊ตฌ์ฒด ํด๋์ค์ ํด๋นํ๋ ํ๋๋ช
(ํน์ ํ๋ผ๋ฏธํฐ๋ช
)์ ์ฌ์ฉํจ์ผ๋ก์จ ํด๊ฒฐํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ DIP๋ฅผ ์๋ฐํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ผ๋ก @Qualifier์ @Primary์, ์ด๋
ธํ
์ด์
์ ์ง์ ๋ง๋ค์ด ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ํ๋ํ๋ ์ดํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. @Primary ์ฌ์ฉ @Primary๋ ์ฐ์ ์์๋ฅผ ์ ํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ์ฌ์ฉ๋ฐฉ๋ฒ์ ๋๊ฒ ๊ฐ๋จํฉ๋๋ค. @Componet @Prim..
ํน์ ํ์
์ ๋น์ด ์ฌ๋ฌ๊ฐ ๋ฑ๋ก๋์ด ์์ ๋, ๋ฑ๋กํ ๋ชจ๋ ๋น์ด ํ์ํ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค. ์ฌ์ฉ์์ ๋ฑ๊ธ์ ๋ฐ๋ผ ๋ค๋ฅธ ์๋น์ค๊ฐ ์ ์ฉ๋์ด์ผ ํ๊ฑฐ๋, ์ฌ๋ฌ ์๋น์ค๋ค ์ค ํ๋๋ฅผ ํด๋ผ์ด์ธํธ๊ฐ ์ ํํ ์ ์๋ ๊ฒฝ์ฐ ๋ฑ์ ๊ฒฝ์ฐ๊ฐ ๊ทธ ์์์
๋๋ค. ์ด๋ด ๋ List๋ Map, Set์ ์ฌ์ฉํ์ฌ ์ฝ๊ฒ ํด๊ฒฐํ ์ ์์ต๋๋ค. ๋ฑ๋กํ ๋น ๋ชจ๋ ์กฐํ ๋ฐฉ๋ฒ์ ์์๋ณด์. public class DiscountService { private final Map policyMap; private final List policiList; private final Set policieSet; @Autowired public DiscountService( Map policyMap, List policiList, Set policieSet ){ ..
@Qualifier์ ๋ฌธ์ ์ @Qualifier("mainDiscountPolicy") ์ฒ๋ผ, ๋ฌธ์๋ฅผ ์ง์ ์ ์ด์ฃผ๋ ๋ฐฉ์์ ์ปดํ์ผ ์์ ์ ์ฒดํฌ๊ฐ ๋์ง ์์ต๋๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋
ธํ
์ด์
์ ์ง์ ๋ง๋ค์ด์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ด๋
ธํ
์ด์
์ง์ ๋ง๋ค๊ธฐ ๋ค์๊ณผ ๊ฐ์ด ์ด๋
ธํ
์ด์
์ @Qualifier๋ฅผ ์ฌ์ฉํ์ฌ ์ง์ ์ด๋
ธํ
์ด์
์ ๋ง๋ค ์ ์์ต๋๋ค. @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Qualifier("mainDiscountPolicy") pu..
@Autowired @Autowired๋ ์ด๋ฆ ๊ทธ๋๋ก ์๋์ผ๋ก ์์กด ๊ด๊ณ๋ฅผ ์ฐ๊ฒฐ(์ฃผ์
) ์์ผ์ฃผ๋ ์ด๋
ธํ
์ด์
์
๋๋ค. @ComponentScan์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, ์์กด๊ด๊ณ์ ๋ํ ์ค์ ์ ํด์ฃผ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค. ํ์ํ ๊ฐ์ฒด์ "ํ์
"์ ํด๋นํ๋ ๋น์ ์ฐพ์์ ์๋์ผ๋ก ์ฃผ์
์์ผ์ค๋๋ค. ์์กด๊ด๊ณ ์๋ ์ฃผ์
์์กด๊ด๊ณ๋ฅผ ์ฃผ์
ํ๋ ๋ฐฉ๋ฒ์๋ ๋ค์๊ณผ ๊ฐ์ 3๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์์ฑ์ ์ฃผ์
์์ ์ ์ฃผ์
(Setter ์ฃผ์
) ํ๋ ์ฃผ์
์์ฑ์ ์ฃผ์
์์ฑ์๋ฅผ ํตํด์ ์์กด๊ด๊ณ๋ฅผ ์ฃผ์
๋ฐ๋ ๋ฐฉ๋ฒ์
๋๋ค. ํน์ง ์์ฑ์ ํธ์ถ์์ ์ ๋จ 1ํ๋ง ํธ์ถ๋๋ ๊ฒ์ด ๋ณด์ฅ๋ฉ๋๋ค. ๋ถ๋ณ, ํ์ ์์กด๊ด๊ณ์ ์ฌ์ฉํฉ๋๋ค. @Component public class OrderServiceImpl implements OrderService { private..
@ComponentScan @ComponentScan์ @Component ๋ฐ @Service, @Repository, @Controller, @Configuration๋ฑ๊ณผ ๊ฐ์ด @Component๊ฐ ๋ถ์ด์๋ Class๋ค์ ์๋์ผ๋ก Scanํ์ฌ ์คํ๋ง Bean์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ์ญํ ์ ์ํํฉ๋๋ค. @Component๋ ํด๋์ค์ ์ฌ์ฉ๋๋ฉฐ, ๋น์ผ๋ก ๋ฑ๋ก๋ ๋์๋ ํด๋์ค๋ช
์ ์ฌ์ฉํ์ฌ ๋ฑ๋กํฉ๋๋ค. ์ด๋, ํด๋์ค์ ๋งจ ์๊ธ์๋ง ์๋ฌธ์๋ก ๋ฐ๊ฟ์ ์ ์ฅํฉ๋๋ค. ๋ง์ฝ Member ํด๋์ค์ ๊ฒฝ์ฐ์๋ Bean์ผ๋ก ๋ฑ๋ก๋๋ ๊ฒฝ์ฐ member๋ก ๋ฑ๋ก๋๋ ๊ฒ์
๋๋ค. @ComponentScan์ ๋ฒ์ ๊ธฐ๋ณธ ๊ฐ์ผ๋ก๋ @ComponenetScan์ด ์ฌ์ฉ๋ ํจํค์ง๋ฅผ ์์ ์์น๋ก ํ์ฌ, ํด๋น ํจํค์ง์ ํ์ ๋๋ ํ ๋ฆฌ๋ฅผ ๋ชจ๋ ํ์ํฉ๋๋ค. ๋ง์ฝ ..
@Configuration @Configuration์ ์คํ๋ง ์ค์ ํด๋์ค๋ฅผ ์ ์ธํ๋ ์ ๋
ธํ
์ด์
์
๋๋ค. ํด๋น ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํจ์ผ๋ก์จ, ์คํ๋ง ๋น๊ณผ ๊ด๋ จ๋ ์ค์ ํด๋์ค์์ ๋ช
์ํ๋ ๊ธฐ๋ฅ๊ณผ ๋์์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ํ๋ ๋ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๋ฐ๋ก ์ฑ๊ธํค๊ณผ ๊ด๋ จ๋ ๊ธฐ๋ฅ์
๋๋ค. @Configuration๊ณผ ์ฑ๊ธํค @Configuration public class AppConfig { @Bean public MemberService memberService() { //์คํ๋ง ๋น์ ๋ฑ๋ก๋ ๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ฉ์๋ ์ด๋ฆ์ผ๋ก ๋ฑ๋ก return new MemberServiceImpl(memberRepository()); } @Bean public OrderService orderService() { return new Or..
์ฑ๊ธํค ์ฑ๊ธํค ํจํด์ ์ด๋ค ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์ค์ง ํ๋๋ง ์์ฑ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ฉฐ, ์ด ์ธ์คํด์ค์ ์ ๊ทผํ ์ ์๋ ์ ์ญ์ ์ธ ์ ์ด์ ์ ์ ๊ณตํ๋ ํจํด์
๋๋ค. ์ฑ๊ธํค์ ๊ตฌํํ๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋๋ฐ, ํ๋ํ๋ ์ดํด๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค. 1. Eager Initialization (์ด๋ฅธ ์ด๊ธฐํ, Thread - safe) Eager Initialization์ ๊ฐ์ฅ ๊ฐ๋จํ ํํ์ ์ฑ๊ธํค ๊ตฌํ ๋ฐฉ๋ฒ์
๋๋ค. public class Singleton { private static final Singleton instance = new Singleton();//ํด๋์ค ๋ก๋ฉ ์์ ์ ์์ฑ private Singleton(){} //private ์์ฑ์๋ก ์ธ๋ถ ํด๋์ค๋ก๋ถํฐ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋๊ฒ์ ์ฐจ๋จํ๋ค. public sta..