์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- docker
- redis ์กฐํ
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- ํ์ดํผ๋ฐ์ด์
- aws ์ฟ ํฐ
- JPA
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- nGrinder
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- docker compose
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- ์๋ฐ
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- s3 log ์ ์ฅ
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- docker ps -a
- ํ๋ก๊ทธ๋๋จธ์ค
- docker-compose kafka
- AWS Certified Solutions Architect - Associate
- ๋ค์ค ์ปจํ ์ด๋
- aws saa ํฉ๊ฒฉ
- Entity
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- ์คํํ๋ ๋ฏธ์ค
- Codedeploy ์ค๋ฅ
- private subnet ec2 ๋ก์ปฌ ์ ์
- prod docker-compose
- redis ํ ์คํธ์ฝ๋
- Kafka
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[Spring] Bean, Component ๋ณธ๋ฌธ
[Spring] Bean, Component
๐คRyusun๐ค 2022. 12. 9. 17:37โญ ์คํ๋ง ๋น(Spring Bean)
- Spring IoC ์ปจํ ์ด๋๊ฐ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด
- Class๋ฅผ ์์ฑํ๊ณ new ๋ฅผ ํตํด ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ ํ ์ฌ์ฉํ๋ ๋์ Spring์ด ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด
- ์ฆ, new ์ฐ์ฐ์๋ก ์ด๋ค ๊ฐ์ฒด๋ฅผ ์์ฑํ์ ๋ ๊ทธ ๊ฐ์ฒด๋ Bean ์ด ์๋๋ฉฐ ApplicationContext.getBean()์ผ๋ก ์ป์ด์ง ์ ์๋ ๊ฐ์ฒด๋ฅผ Bean์ด๋ผ๊ณ ํฉ๋๋ค.
- Spring์์์ ๋น์ ApplicationContext๊ฐ ์๊ณ ์๋ ๊ฐ์ฒด, ์ฆ ApplicationContext๊ฐ ๋ง๋ค์ด์ ๊ทธ ์์ ๋ด๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํฉ๋๋ค.
์ด๋ฌํ @Component, @Service, @Repository, @Controller, @Configuration ๋ฑ๋ฑ ์ด๋ฌํ Bean ๋ค์ ์ด๋ป๊ฒ ๋ฑ๋กํ ๊น์?
๋ฑ๋กํ๋ ๋ฐฉ๋ฒ์๋ 2๊ฐ์ง ๋ฑ์ด ์์ต๋๋ค.
โญSpring IoC Container์ Bean์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
- @Component , @ComponentScan ์ฌ์ฉ
- ๋น ์ค์ ํ์ผ์ ์ง์ ๋น์ ๋ฑ๋ก
1. @Component , @ComponentScan ์ฌ์ฉ
- @Component
- @component๋ ํด๋์ค์ ๋ถ์ด์ ํด๋์ค๋ฅผ custom bean(๊ฐ๋ฐ์๊ฐ ๋ง๋ ๋น)์ผ๋ก ๋ง๋ค์ด์ค๋๋ค.
- ์ค์ ๋ก Bean์ผ๋ก ๋ฑ๋กํ ํด๋์ค ์์ ๋ถ์ ๋๋ค. ๊ทธ๋ผ @ComponentScan์ด base package์์๋ถํฐ ๋ชจ๋ @Component๋ฅผ ๊ฒ์ํํ, ํด๋น ์ด๋ ธํ ์ด์ ์๋ ํด๋์ค๋ฅผ ์คํ๋ง ์ปจํ ์ด๋์ Bean์ผ๋ก ๋ฑ๋กํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋์ค์ ํ์ํ๊ณณ์ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์ฃผ์ ์ ํด์ค๋๋ค.
- @component๋ ํด๋์ค์ ๋ถ์ด์ ํด๋์ค๋ฅผ custom bean(๊ฐ๋ฐ์๊ฐ ๋ง๋ ๋น)์ผ๋ก ๋ง๋ค์ด์ค๋๋ค.
์์์๋ Spring์๊ฒ Controller์์ ์๋ ค์ฃผ๊ธฐ ์ํด @Controller์ ์ฌ์ฉํ์ต๋๋ค.
@Controller Annotation์ ์์ค๋ฅผ ๋ณด๋ฉด @Component Annotation์ด ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค. Spring์ @Component Annotation์ ํตํด ํด๋น Controller์ Bean์ผ๋ก ๋ฑ๋กํฉ๋๋ค.
- @ComponentScan
- ComponentScan์ ์ฐ๋ฆฌ๊ฐ ๋ฑ๋กํ๊ณ ์ถ๋ค๊ณ ์ค์ ํ Bean ๋ค์ ์ฐพ์์ Bean์ผ๋ก ๋ฑ๋กํฉ๋๋ค.
- @ComponentScan์ ํด๋น ์ ๋ํ ์ด์ ์ด ์์ฑ๋ ํจํค์ง ์ดํ์ ํด๋์ค๋ค์ ์ํํ๋ฉด Bean ๋ฑ๋กํ ๊ฐ์ฒด๋ค์ ํ์ํฉ๋๋ค.
Spring Boot ํ๋ก์ ํธ์์๋ @SpringBootApplication์ด @ComponentScan์ ๊ฐ์ง๊ณ ์์ด ๋ฐ๋ก ์ค์ ํ์ง ์๋ ์ด์ @ComponentScan Annotation์ด ๋ณด์ด์ง ์์ต๋๋ค.
SpringBoot ์์๋ฅผ ๋ณด๋ฉด
HelloApplication ํด๋์ค ์์ @SpringBootApplication Annotation์ด ๋ถ์ด์๋๋ฐ ์ด ์์ค๋ฅผ ๋ค์ฌ๋ค๋ณด๋ฉด
@ComponentScan ์ ์ฐพ์์์์ต๋๋ค. ์ด๋ @SpringBootApplication์ ๋ด๋ถ์ ์ผ๋ก @ComponentScan์ ๋ค๊ณ ์์์ ๋ปํฉ๋๋ค. @ComponentScan์ @ComponentScan Annotation์ด ๋ถ์ ํด๋น ํจํค์ง์์๋ถํฐ ๋ชจ๋ ํ์ ํจํค์ง์ ๋ชจ๋ ํด๋์ค๋ฅผ ํ์ด๋ณด๋ฉฐ @Component ์ ๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ฐพ์ ํ Spring IoC ์ปจํ ์ด๋์ Bean์ผ๋ก ๋ฑ๋กํฉ๋๋ค.
2. ๋น ์ค์ ํ์ผ์ ์ง์ ๋น์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ
@Component Annotation์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ๋ง๊ณ ๋ XML๊ณผ ์๋ฐ ์ค์ ํ์ผ์์ ์ง์ Bean์ ๋ฑ๋กํ ์๋ ์์ต๋๋ค.
์๋ฐ ์ค์ ํ์ผ์ ์๋ฐ ํด๋์ค๋ฅผ ์์ฑํด์ XMLํ์ผ์ ๋์ฒดํ๋ Bean ์ค์ ํ์ผ์์ ์๋ ค์ฃผ๋ @Configuration Annotation์ ํด๋์ค ์์ ๋ถ์ด๊ณ ๋ฉ์๋ ์์ @Bean Annotaion์ ์ฌ์ฉํ์ฌ ์ง์ Bean์ ์ ์ํฉ๋๋ค.
* @Configuration
- "์ด ํด๋์ค๋ ๊ฐ์ข ๋น ์ค์ ์ ๋ด๊ณ ์๋ค"
- @SpringBootApplication์ด ์ปดํฌ๋ํธ ์ค์บ์ ํตํด @Configuration์ ์ฐพ์๋ธ๋ค.
- ์์ ๋น ์ค์ (๋ฉ์๋)๋ฅผ ์ฝ์ด์ ์คํ๋ง ์ปจํ ์ด๋์ ๋ฑ๋กํ๋ค.
- ํ์ํ ๊ณณ์ ์ฃผ์ ํ๋ค.
- ๋๋ ๊ฐ์ข ์คํ๋ง ์ธํฐํ์ด์ค์ ๊ตฌํ์ ํจ๊ป ํ์ฉํ๋ค.
๊ทธ๋ผ return ๋๋ PageController() ๊ฐ์ฒด๊ฐ IoC ์ปจํ ์ด๋ ์์ ๋น์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
@Bean ๊ณผ @Component์ ์ฐจ์ด
์คํ๋ง์ ๊ณต๋ถํ๋ฉด์ @Bean ๊ณผ @Component ์ด ๋น์ทํ๊ฒ๊ฐ์ ์ฐจ์ด์ ์ ์ ๋ฆฌํด๋ณด์์ต๋๋ค.
@Bean
- @Bean์ ๊ฒฝ์ฐ ๊ฐ๋ฐ์๊ฐ ์ปจํธ๋กค์ด ๋ถ๊ฐ๋ฅํ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ Bean์ผ๋ก ๋ฑ๋กํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉ๋ฉ๋๋ค.
- ๋ค์ ๋งํด, ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋๋ ์ด๋ฏธ ์ ์๋์ด ์๋ ๊ฐ์ฒด๋ค์ ๋น์ผ๋ก ๋ฑ๋กํ๊ณ ์ถ์๋ ์ฌ์ฉํ๋ ์ด๋ ธํ ์ด์ ์ ๋๋ค
- ๋ฉ์๋ ๋จ์์์ ๊ฐ๋ฐ์๊ฐ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ๊ฐ๋ฐ์๊ฐ ์ ์ํ์ง ์์ ๊ฐ์ฒด๋ฅผ ์ ์ฐํ๊ฒ ๋๊ธฐ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค.
- ๋ํ ํด๋์ค์ ๋ถ์ผ์์๋ @Component์ ๋ฌ๋ฆฌ @Bean์ ๋ฉ์๋์๋ง ๋ถ์ผ์์์ต๋๋ค.
@Component
- ๊ฐ๋ฐ์๊ฐ ์ง์ ์ ์ํ ๊ฐ์ฒด ํด๋์ค ์์ฒด๋ฅผ ๋น ๋ฑ๋กํ ๋ ์ฌ์ฉํ๋ ์ด๋ ธํ ์ด์ ์ ๋๋ค.
- @Componet ์ด๋ ธํ ์ด์ ์ ํ์ผ์ Type์ผ๋ก ํด๋์ค, ์ธํฐํ์ด์ค, ์ด๊ฑฐํ์ ๋ถ์ผ ์ ์์ต๋๋ค.
[์ฐธ์กฐ]
- https://melonicedlatte.com/2021/07/11/232800.html
- https://atoz-develop.tistory.com/entry/Spring-%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B9%88Bean%EC%9D%98-%EA%B0%9C%EB%85%90%EA%B3%BC-%EC%83%9D%EC%84%B1-%EC%9B%90%EB%A6%AC
- https://mungto.tistory.com/448
- https://effectivesquid.tistory.com/entry/Bean-%EA%B3%BC-Component%EC%9D%98-%EC%B0%A8%EC%9D%B4
'๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป > ๐๐ฉ๐ซ๐ข๐ง๐ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] TestCode, TDD, JUnit, assertJ (0) | 2022.12.19 |
---|---|
[Spring] AOP (2) | 2022.12.11 |
[Spring] IoC (0) | 2022.12.09 |
[Spring] Spring MVC ๊ตฌ์กฐ (0) | 2022.11.02 |
[Spring] Forward, Redirect, Session (2) | 2022.11.01 |