Spring Annotation
Spring Bean์ ๋ฑ๋กํ๋๋ฐ ์์ด์ 2๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค.
์ปดํฌ๋ํธ ์ค์บ๊ณผ ์๋ ์์กด๊ด๊ณ ์ค์
์๋ฐ ์ฝ๋๋ก ์ง์ ์คํธ๋ง ๋น ๋ฑ๋กํ๊ธฐ
์ปดํฌ๋ํธ ์ค์บ๊ณผ ์๋ ์์กด๊ด๊ณ ์ค์
Annotation
์ค๋ช
@Component
@Component ์ด๋
ธํ
์ด์
์ด ์์ผ๋ฉด, ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋๋ค.(@ComponentScan์ ์ํด์)
@Controller
์คํ๋ง MVC ์ปจํธ๋กค๋ฌ
@Service
ํน๋ณํ ์ฒ๋ฆฌ๋ฅผ ํ์ง ์๋๋ค. ๋์ ๊ฐ๋ฐ์๋ค์ด ํต์ฌ ๋น์ฆ๋์ค ๊ณ์ธต์ ์ธ์ํ๋๋ฐ ๋์์ด ๋๋ค.
@Repository
์คํ๋ง ๋ฐ์ดํฐ ์ ๊ทผ ๊ณ์ธต์ผ๋ก ์ธ์ํ๊ณ , ๋ฐ์ดํฐ ๊ณ์ธต์ ์์ธ๋ฅผ ์คํ๋ง ์์ธ๋ก ๋ณํํด์ค๋ค.
@Configuration
์คํ๋ง ์ค์ ์ ๋ณด๋ก ์ธ์ํ๊ณ , ์คํ๋ง ๋น์ด ์ฑ๊ธํค์ ์ ์งํ๋๋ก ์ถ๊ฐ ์ฒ๋ฆฌ
@Autowired
๊ฐ์ฒด ์์ฑ ์์ ์ ์คํ๋ง ์ปจํ ์ด๋์์ ํด๋น ์คํ๋ง ๋น์ ์ฐพ์์ ์ฃผ์ ํ๋ค. ์คํ๋ง์ด ๊ด๋ฆฌํ๋ ๊ฐ์ฒด์์๋ง ๋์ (์๋น์ค ๊ฐ์ ์ฐ๊ฒฐ ์ ์ญํ ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ์ดํดํ๊ธฐ ์ฝ๋ค.)
์์์ ๋ณด์ด๋ @Controller, @Repository, @Service ๋ชจ๋ ๋ด๋ถ์ @Component ์ด๋
ธํ
์ด์
์ด ํฌํจ๋์ด ์๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
@Controller
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";
}@Repository
์ด๋ ๊ฒ @Component ์ด๋
ธํ
์ด์
์ ๋ณด๊ณ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋๋ ๊ฒ์ ์ปดํฌ๋ํธ ์ค์บ์ด๋ผ๊ณ ํ๋ค.
์์ ์ฝ๋
Controller
Service
Repository
์๋ฐ ์ฝ๋๋ก ์ง์ ์คํธ๋ง ๋น ๋ฑ๋กํ๊ธฐ
@Service, @Repository ์ด๋
ธํ
์ด์
์ผ๋ก ๋น์ ๋ฑ๋กํ์ง ์๊ณ ์ง์ @Bean ์ด๋
ธํ
์ด์
์ผ๋ก ๋น์ ๋ฑ๋ก ํ ์ ์๋ค.
Controller
Service
Repository
Configuration ์ค์
์ธ์ ์ฌ์ฉํด์ผํ ๊น?
์ ํํ๋ Controller, Servcie, Repository๋ ์ปดํฌ๋ํธ ์ค์บ์ ์ฌ์ฉํ๋ค.
ํ์ง๋ง, ์ ํํ ๋์ง ์๊ฑฐ๋, ์ํฉ์ ๋ฐ๋ผ ๊ตฌํ ํด๋์ค๋ฅผ ๋ณ๊ฒฝํด์ผ ํ๋ฉด ์ค์ ์ ํตํด ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๋ค.
Last updated
Was this helpful?