Spring Annotation

Spring Bean์„ ๋“ฑ๋กํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  1. ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž๋™ ์˜์กด๊ด€๊ณ„ ์„ค์ •

  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

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

    /**
     * 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 "";

}

์ด๋ ‡๊ฒŒ @Component ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ณด๊ณ  ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜๋Š” ๊ฒƒ์„ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด๋ผ๊ณ  ํ•œ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ

  • Controller

package dh0023.springmvc.member.controller;

import dh0023.springmvc.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class MemberController {


    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService){
        this.memberService = memberService;
    }

}
  • Service

package dh0023.springmvc.member.service;

import dh0023.springmvc.member.domain.Member;
import dh0023.springmvc.member.repository.MemberRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MemberService {


    private final MemberRepository memberRepository;

    @Autowired
    public MemberService(MemberRepository memberRepository){
        this.memberRepository = memberRepository;
    }
    ...
}
  • Repository

@Repository
public class MemoryMemberRepository implements MemberRepository{

    // ์‹ค๋ฌด์—์„œ๋Š” ConcurrentHashMap์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
    private static Map<Long, Member> store = new HashMap<>();
    private static long sequence = 0L;

        ...
}

์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ง์ ‘ ์ŠคํŠธ๋ง ๋นˆ ๋“ฑ๋กํ•˜๊ธฐ

@Service, @Repository ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋นˆ์„ ๋“ฑ๋กํ•˜์ง€ ์•Š๊ณ  ์ง์ ‘ @Bean ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋นˆ์„ ๋“ฑ๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Controller

@Controller
public class MemberController {


    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService){
        this.memberService = memberService;
    }

}
  • Service

public class MemberService {


    private final MemberRepository memberRepository;

    public MemberService(MemberRepository memberRepository){
        this.memberRepository = memberRepository;
    }
  // ...
}
  • Repository

public class MemoryMemberRepository implements MemberRepository{
    ...
}
  • Configuration ์„ค์ •

@Configuration
public class SpringConfig {

    @Bean
    public MemberService memberService(){
        return new MemberService(memberRepository());
    }

    @Bean
    public MemberRepository memberRepository(){
        return new MemoryMemberRepository();
    }
}

์–ธ์ œ ์‚ฌ์šฉํ•ด์•ผํ• ๊นŒ?

์ •ํ˜•ํ™”๋œ Controller, Servcie, Repository๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, ์ •ํ˜•ํ™” ๋˜์ง€ ์•Š๊ฑฐ๋‚˜, ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ตฌํ˜„ ํด๋ž˜์Šค๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•˜๋ฉด ์„ค์ •์„ ํ†ตํ•ด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

Last updated