Spring Annotation

Bean Annotations

@Component

  • ๋ชจ๋“  Spring ๊ด€๋ฆฌ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.

@Service

  • ์„œ๋น„์Šค ๊ณ„์ธต์˜ ํด๋ž˜์Šค๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.

  • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์ž…๋‹ˆ๋‹ค.

@Repository

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ €์žฅ์†Œ ์—ญํ• ์„ ํ•˜๋Š” ์ง€์†์„ฑ ๊ณ„์ธต์˜ ํด๋ž˜์Šค์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

  • ์—ญํ• ์€ ์ง€์†์„ฑ ๊ด€๋ จ ์˜ˆ์™ธ๋ฅผ ํฌ์ฐฉํ•˜์—ฌ Spring์˜ ํ†ตํ•ฉ๋œ unchecked exceptions๋กœ ๋‹ค์‹œ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

@Component Scanning

  • ์Šค์บ”ํ•  annotation configuration ํด๋ž˜์Šค์˜ ํŒจํ‚ค์ง€๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค

  • basePackages ๋˜๋Š” ๊ฐ’ ์ธ์ˆ˜(value๋Š” basePackages์˜ ๋ณ„์นญ) ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธฐ๋ณธ ํŒจํ‚ค์ง€ ์ด๋ฆ„์„ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Configuration
@ComponentScan(basePackages = "com.baeldung.annotations")
class VehicleFactoryConfig {}

@Configuration
@ComponentScan(basePackageClasses = VehicleFactoryConfig.class)
class VehicleFactoryConfig {}
  • ์ธ์ˆ˜๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด @ComponentScan annotation ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ๋™์ผํ•œ ํŒจํ‚ค์ง€์—์„œ ์Šค์บ”์ด ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

@Controller

  • Controller ๊ณ„์ธต์˜ ํด๋ž˜์Šค๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์—๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.

@Configuraion

  • @Bean์ด ๋‹ฌ๋ ค์žˆ๋Š” dean definition ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Web Annotations

@RequestMapping

  • @Controller ํด๋ž˜์Šค ๋‚ด๋ถ€์— request handler ๋ฉ”์„œ๋“œ๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

    • path, or its aliases, name, and value: which URL the method is mapped to

    • method: compatible HTTP methods

    • params: filters requests based on presence, absence, or value of HTTP parameters

    • headers: filters requests based on presence, absence, or value of HTTP headers

    • consumes: which media types the method can consume in the HTTP request body

    • produces: which media types the method can produce in the HTTP response body

@RequestBody

  • maps the body of the HTTP request to an object:

    • ์—ญ์ง๋ ฌํ™”๋Š” ์ž๋™์ด๋ฉฐ ์š”์ฒญ์˜ ์ฝ˜ํ…์ธ  ์œ ํ˜•์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

@PathVariable

  • ์ด annotation์€ ๋ฉ”์„œ๋“œ ์ธ์ˆ˜๊ฐ€ URI ํ…œํ”Œ๋ฆฟ ๋ณ€์ˆ˜์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

@RequsetParam

  • accessing HTTP request parameters

@ResponseBody

  • Spring์€ ๋ฉ”์†Œ๋“œ์˜ ๊ฒฐ๊ณผ๋ฅผ ์‘๋‹ต์œผ๋กœ ์ทจ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

@ExceptionHandler

  • ์ง€์ •๋œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์ง€์ •ํ•œ ์˜ค๋ฅ˜ ์ฒ˜๋ฆฌ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@ResponseStatus

  • ์›ํ•˜๋Š” ์‘๋‹ต HTTP ์ƒํƒœ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@RestController

  • combines @Controller and @ResponseBody.

@ModelAttribute

  • ์ด๋ฏธ ๋ชจ๋ธ์— ์žˆ๋Š” ์š”์†Œ์— ์•ก์„ธ์Šค ํ•œ๋‹ค.

Core Annotations

@Autowired

  • Spring์ด ์ฃผ์ž…ํ•  ์˜์กด์„ฑ์„ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

@Bean

  • @Bean marks a factory method which instantiates a Spring bean

@Qualifier

  • ๋ชจํ˜ธํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋Š” bean id ๋˜๋Š” bean ์ด๋ฆ„์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด @Autowired์™€ ํ•จ๊ป˜ @Qualifier๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

@Required

  • XML์„ ํ†ตํ•ด ์ฑ„์šฐ๋ ค๋Š” ์˜์กด์„ฑ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•œ setter ๋ฉ”์„œ๋“œ์— @Required์„ ์‚ฌ์šฉํ•œ๋‹ค.

@Value

  • ์†์„ฑ ๊ฐ’์„ Bean์— ์ฃผ์ž… ํ•  ์ˆ˜ ์žˆ๋‹ค.

@DependsOn

  • annotation์—์„œ ์„ค์ •ํ•œ bean์„ ๋จผ์ € ์ดˆ๊ธฐํ™” ํ›„ annotation ๋‹ฌ๋ฆฐ bean์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

@Lazy

  • ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘ ์‹œ๊ฐ€ ์•„๋‹ˆ๋ผ ์š”์ฒญ ์‹œ bean์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Lookup

  • @Lookup ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ์œ ํ˜• ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฆฌํ„ดํ•˜๋„๋ก Spring์— ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

@Component
@Scope("prototype")
public class SchoolNotification {
    // ... prototype-scoped state
}
@Component
public class StudentServices {

    // ... member variables, etc.

    @Lookup
    public SchoolNotification getNotification() {
        return null;
    }

    // ... getters and setters
}

@Primary

  • ๋™์ผํ•œ ์œ ํ˜•์˜ ์—ฌ๋Ÿฌ bean์ค‘ ํŠน์ • bean์„ ์‚ฌ์šฉ์‹œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

@Scope

  • @Scope๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ @Component ํด๋ž˜์Šค ๋˜๋Š” @Bean ์ •์˜์˜ ๋ฒ”์œ„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

@Profile

  • ํŠน์ • ํ”„๋กœํ•„์ด ํ™œ์„ฑํ™”๋œ ๊ฒฝ์šฐ์—๋งŒ Spring์ด @Component ํด๋ž˜์Šค ๋˜๋Š” @Bean ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜๋ ค๋ฉด @Profile๋กœ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Import

  • ์ด annotation์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ์„ฑ component scanning ์—†์ด ํŠน์ • @Configurationํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@ImportResource

  • XML configurations์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@PropertySource

  • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •์— ๋Œ€ํ•œ ์†์„ฑ ํŒŒ์ผ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@PropertySources

  • @PropertySource configurations์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Last updated

Was this helpful?