๐ก @Autowired ์ด๋
ธํ
์ด์
์ Type์ ํตํ DI๋ฅผ ํ ๋ ์ฌ์ฉํ๋ค. ์คํ๋ง ์ปจํ
์ด๋๊ฐ ์์์ ํด๋น ํ์
์ Bean์ ์ฐพ์์ ์ฃผ์
ํด์ค๋ค.
์๋ ์ฝ๋๋ ํ
์คํธ์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ ํ BookDTO, BookDAO, BookDAOImpl ํด๋์ค์ด๋ค.
BookDTO
@Data
@AllArgsConstructor
public class BookDTO {
private int sequence; //๋์๋ฒํธ
private int isbn; //isbn
private String title; //์ ๋ชฉ
private String author; //์ ์
private String publisher; //์ถํ์ฌ
private Date createdDate; //์ถํ์ผ
}
BookDAO
public interface BookDAO {
/* ๋์ ๋ชฉ๋ก ์ ์ฒด ์กฐํ */
List<BookDTO> selectBookList();
/* ๋์ ๋ฒํธ๋ก ๋์ ์กฐํ */
BookDTO selectOneBook(int sequence);
}
BookDAOImpl
/* @Repository : @Component์ ์ธ๋ถํ ์ด๋
ธํ
์ด์
์ ํ ์ข
๋ฅ๋ก DAO ํ์
์ ๊ฐ์ฒด์ ์ฌ์ฉํ๋ค. */
@Repository("bookDAO")
public class BookDAOImpl implements BookDAO {
private Map<Integer, BookDTO> bookList;
public BookDAOImpl() {
bookList = new HashMap<>();
bookList.put(1, new BookDTO(1, 123456, "์๋ฐ์ ์ ์", "๋จ๊ถ์ฑ", "๋์ฐ์ถํ", new Date()));
bookList.put(2,
new BookDTO(2, 654321, "์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค", "๊ณ ๋", "๊ณ ๋์ถํ", new Date()));
}
@Override
public List<BookDTO> selectBookList() {
return new ArrayList<>(bookList.values());
}
@Override
public BookDTO selectOneBook(int sequence) {
return bookList.get(sequence);
}
}
/* @Service : @Component์ ์ธ๋ถํ ์ด๋
ธํ
์ด์
์ ํ ์ข
๋ฅ๋ก Service ๊ณ์ธต์์ ์ฌ์ฉํ๋ค. */
@Service("bookServiceField")
public class BookService {
/* BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ ์ด ํ๋กํผํฐ์ ์๋์ผ๋ก ์ฃผ์
ํด์ค๋ค. */
@Autowired
private BookDAO bookDAO;
/* ๋์ ๋ชฉ๋ก ์ ์ฒด ์กฐํ */
public List<BookDTO> selectAllBooks(){
return bookDAO.selectBookList();
}
/* ๋์ ๋ฒํธ๋ก ๋์ ์กฐํ */
public BookDTO searchBookBySequence(int sequence) {
return bookDAO.selectOneBook(sequence);
}
}
private BookDAO bookDAO = new BookDAOImpl(); ์ ๊ฐ์ด ํ๋๋ฅผ ์ ์ธํ๋ค๋ฉด BookService ํด๋์ค๋ BookDAOImpl ํด๋์ค์ ๋ณ๊ฒฝ์ ์ง์ ์ ์ผ๋ก ์ํฅ์ ๋ฐ๋ ๊ฐํ ๊ฒฐํฉ์ ๊ฐ์ง๊ฒ ๋๋ค. ๊ฐ์ฒด๊ฐ์ ๊ฒฐํฉ์ ๋์จํ๊ฒ ํ๊ธฐ ์ํด new BookDAOImpl() ์ ๊ฐ์ ์ง์ ์ ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์์ฑ์ ๊ตฌ๋ฌธ์ ์ ๊ฑฐํ๊ณ ํ๋์ @Autowired ์ด๋
ธํ
์ด์
์ ์์ฑํ ์ ์๋ค. ๊ทธ๋ฌ๋ฉด ์คํ๋ง ์ปจํ
์ด๋๋ BookService ๋น ๊ฐ์ฒด ์์ฑ ์ BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์์กด์ฑ์ ์ฃผ์
ํด์ค๋ค.
์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ์ฌ @Repository, @Service ๋ฑ์ ์ด๋
ธํ
์ด์
์ด ์์ฑ ๋ ํด๋์ค๊ฐ ๋น ์ค์บ๋์ ํตํด ์ ๋ฑ๋ก ๋์๋์ง, ๋ํ ๊ฐ์ฒด์ ์์กด ๊ด๊ณ์ ๋ฐ๋ผ @Autowired ์ด๋
ธํ
์ด์
์ ํตํด ์์กด์ฑ ์ฃผ์
์ด ๋์๋์ง๋ฅผ ํ
์คํธํ๋ค.
/* AnnotationConfigApplicationContext ์์ฑ์์ basePackages ๋ฌธ์์ด์ ์ ๋ฌํ๋ฉฐ ApplicationContext ์์ฑํ๋ค. */
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section01");
BookService bookService = context.getBean("bookServiceField", BookService.class);
/* ์ ์ฒด ๋์ ๋ชฉ๋ก ์กฐํ ํ ์ถ๋ ฅ ํ์ธ */
bookService.selectAllBooks().forEach(System.out::println);
/* ๋์๋ฒํธ๋ก ๊ฒ์ ํ ์ถ๋ ฅ ํ์ธ*/
System.out.println(bookService.searchBookBySequence(1));
System.out.println(bookService.searchBookBySequence(2));
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)/* @Service : @Component์ ์ธ๋ถํ ์ด๋
ธํ
์ด์
์ ํ ์ข
๋ฅ๋ก Service ๊ณ์ธต์์ ์ฌ์ฉํ๋ค. */
@Service("bookServiceConstructor")
public class BookService {
private final BookDAO bookDAO;
/* BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ ์์ฑ์์ ์๋์ผ๋ก ์ฃผ์
ํด์ค๋ค. */
@Autowired
public BookService(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
public List<BookDTO> selectAllBooks(){
return bookDAO.selectBookList();
}
public BookDTO searchBookBySequence(int sequence) {
return bookDAO.selectOneBook(sequence);
}
}
์์ฑ์์๋ @Autowired ์ด๋
ธํ
์ด์
์ ์์ฑํ ์ ์๋ค. ๊ทธ๋ฌ๋ฉด ์คํ๋ง ์ปจํ
์ด๋๋ BookService ๋น ๊ฐ์ฒด ์์ฑ ์ BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์์กด์ฑ์ ์ฃผ์
ํด์ค๋ค.
Spring 4.3 ๋ฒ์ ์ดํ๋ก๋ ์์ฑ์๊ฐ ํ ๊ฐ ๋ฟ์ด๋ผ๋ฉด @Autowired ์ด๋
ธํ
์ด์
์ ์๋ตํด๋ ์๋์ผ๋ก ์์ฑ์ ์ฃผ์
์ด ๋์ํ๋ค. ๋จ, ์์ฑ์๊ฐ 1๊ฐ ์ด์์ผ ๊ฒฝ์ฐ์๋ ๋ช
์์ ์ผ๋ก ์์ฑ์ ํด์ฃผ์ด์ผ ํ๋ค. ์์ ์ฝ๋์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ถ๊ฐ๋ก ์์ฑํ๊ณ ๋งค๊ฐ๋ณ์ ์์ฑ์์ @Autowired ์ด๋
ธํ
์ด์
์ ์๋ตํ๊ฒ ๋๋ฉด ์์ฑ์ ์ฃผ์
์ด ๋์ํ์ง ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
์์ฑ์ ์ฃผ์
์ ์ฅ์
์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ์ฌ @Repository, @Service ๋ฑ์ ์ด๋
ธํ
์ด์
์ด ์์ฑ ๋ ํด๋์ค๊ฐ ๋น ์ค์บ๋์ ํตํด ์ ๋ฑ๋ก ๋์๋์ง, ๋ํ ๊ฐ์ฒด์ ์์กด ๊ด๊ณ์ ๋ฐ๋ผ @Autowired ์ด๋
ธํ
์ด์
์ ํตํด ์์กด์ฑ ์ฃผ์
์ด ๋์๋์ง๋ฅผ ํ
์คํธํ๋ค.
/* AnnotationConfigApplicationContext ์์ฑ์์ basePackages ๋ฌธ์์ด์ ์ ๋ฌํ๋ฉฐ ApplicationContext ์์ฑํ๋ค. */
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section01");
BookService bookService = context.getBean("bookServiceConstructor", BookService.class);
/* ์ ์ฒด ๋์ ๋ชฉ๋ก ์กฐํ ํ ์ถ๋ ฅ ํ์ธ */
bookService.selectAllBooks().forEach(System.out::println);
/* ๋์๋ฒํธ๋ก ๊ฒ์ ํ ์ถ๋ ฅ ํ์ธ*/
System.out.println(bookService.searchBookBySequence(1));
System.out.println(bookService.searchBookBySequence(2));
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)/* @Service : @Component์ ์ธ๋ถํ ์ด๋
ธํ
์ด์
์ ํ ์ข
๋ฅ๋ก Service ๊ณ์ธต์์ ์ฌ์ฉํ๋ค. */
@Service("bookServiceSetter")
public class BookService {
private BookDAO bookDAO;
/* BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ setter์ ์๋์ผ๋ก ์ฃผ์
ํด์ค๋ค. */
@Autowired
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
public List<BookDTO> selectAllBooks(){
return bookDAO.selectBookList();
}
public BookDTO searchBookBySequence(int sequence) {
return bookDAO.selectOneBook(sequence);
}
}
setter ๋ฉ์๋์๋ @Autowired ์ด๋
ธํ
์ด์
์ ์์ฑํ ์ ์๋ค. ๊ทธ๋ฌ๋ฉด ์คํ๋ง ์ปจํ
์ด๋๋ BookService ๋น ๊ฐ์ฒด ์์ฑ ์ BookDAO ํ์
์ ๋น ๊ฐ์ฒด๋ฅผ ์ฐพ์ ์์กด์ฑ์ ์ฃผ์
ํด์ค๋ค.
์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ์ฌ @Repository, @Service ๋ฑ์ ์ด๋
ธํ
์ด์
์ด ์์ฑ ๋ ํด๋์ค๊ฐ ๋น ์ค์บ๋์ ํตํด ์ ๋ฑ๋ก ๋์๋์ง, ๋ํ ๊ฐ์ฒด์ ์์กด ๊ด๊ณ์ ๋ฐ๋ผ @Autowired ์ด๋
ธํ
์ด์
์ ํตํด ์์กด์ฑ ์ฃผ์
์ด ๋์๋์ง๋ฅผ ํ
์คํธํ๋ค.
/* AnnotationConfigApplicationContext ์์ฑ์์ basePackages ๋ฌธ์์ด์ ์ ๋ฌํ๋ฉฐ ApplicationContext ์์ฑํ๋ค. */
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section01");
BookService bookService = context.getBean("bookServiceSetter", BookService.class);
/* ์ ์ฒด ๋์ ๋ชฉ๋ก ์กฐํ ํ ์ถ๋ ฅ ํ์ธ */
bookService.selectAllBooks().forEach(System.out::println);
/* ๋์๋ฒํธ๋ก ๊ฒ์ ํ ์ถ๋ ฅ ํ์ธ*/
System.out.println(bookService.searchBookBySequence(1));
System.out.println(bookService.searchBookBySequence(2));
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=1, isbn=123456, title=์๋ฐ์ ์ ์, author=๋จ๊ถ์ฑ, publisher=๋์ฐ์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)
BookDTO(sequence=2, isbn=654321, title=์นญ์ฐฌ์ ๊ณ ๋๋ ์ถค์ถ๊ฒ ํ๋ค, author=๊ณ ๋, publisher=๊ณ ๋์ถํ,
createdDate=Sun May 28 20:19:12 KST 2023)@Autowired ์ด๋
ธํ
์ด์
์ ๊ฐ์ฅ ๋ณดํธ์ ์ผ๋ก ์ฌ์ฉ ๋๋ ์์กด์ฑ ์ฃผ์
Annotation์ด๋ค. @Autowired ์ ํจ๊ป ์ฌ์ฉํ๊ฑฐ๋ ๋๋ ๋์ฒดํด์ ์ฌ์ฉํ ์ ์๋ ์ด๋
ธํ
์ด์
์ ํ์ตํ๋ค.
์๋ ์ฝ๋๋ ํ
์คํธ์ ๊ณตํต์ ์ผ๋ก ์ฌ์ฉ ํ Pokemon, Charmander, Pikachu, Squirtle ํด๋์ค์ด๋ค.
Pokemon
public interface Pokemon {
/* ๊ณต๊ฒฉํ๋ค */
void attack();
}
Charmander
@Component
public class Charmander implements Pokemon {
@Override
public void attack() {
System.out.println("ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ");
}
}
Pikachu
@Component
public class Pikachu implements Pokemon {
@Override
public void attack() {
System.out.println("ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก");
}
}
Squirtle
@Component
public class Squirtle implements Pokemon {
@Override
public void attack() {
System.out.println("๊ผฌ๋ถ๊ธฐ ๋ฌผ๋ํฌ ๋ฐ์ฌ๐");
}
}
๐ก @Primary ์ด๋
ธํ
์ด์
์ ์ฌ๋ฌ ๊ฐ์ ๋น ๊ฐ์ฒด ์ค์์ ์ฐ์ ์์๊ฐ ๊ฐ์ฅ ๋์ ๋น ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ ์ด๋
ธํ
์ด์
์ด๋ค.
์์ฑ์๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
@Service("pokemonServicePrimary")
public class PokemonService {
private Pokemon pokemon;
@Autowired
public PokemonService(Pokemon pokemon) {
this.pokemon = pokemon;
}
public void pokemonAttack() {
pokemon.attack();
}
}
Charmander, Pikachu, Squirtle, PokemonService ๋ฅผ ๋น ์ค์บ๋ ํ ์ ์๋ basePackages๋ฅผ ์ค์ ํ์ฌ ์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ค.
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section02");
PokemonService pokemonService = context.getBean("pokemonServicePrimary", PokemonService.class);
pokemonService.pokemonAttack();
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'pokemonServicePrimary' defined in file ํ์ผ ๊ฒฝ๋ก :
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'com.ohgiraffers.section02.common.Pokemon' available:
expected single matching bean but found 3: charmander,pikachu,squirtle
...์๋ต์คํ๋ง ์ปจํ
์ด๋ ๋ด๋ถ์ Pokemon ํ์
์ ๋น ๊ฐ์ฒด๊ฐ charmander,pikachu,squirtle 3๊ฐ๊ฐ ์์ด 1๊ฐ์ ๊ฐ์ฒด๋ฅผ PokemonService์ ์์ฑ์๋ก ์ ๋ฌํ ์ ์์ด ์ค๋ฅ๊ฐ ๋ฐ์ํ์์ ํ์ธํ ์ ์๋ค.
Charmander, Pikachu, Squirtle ์ค์์ Charmander ๋น ๊ฐ์ฒด๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฃผ์
๋ฐ๋๋ก @Primary ์ด๋
ธํ
์ด์
์ ์ค์ ํ๋ค.
@Component
@Primary
public class Charmander implements Pokemon {
@Override
public void attack() {
System.out.println("ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ");
}
}
@Primary ์ด๋
ธํ
์ด์
์ ์ค์ ํ๋ฉด @Autowired๋ก ๋์ผํ ํ์
์ ์ฌ๋ฌ ๋น์ ์ฐพ๊ฒ ๋๋ ๊ฒฝ์ฐ ์๋์ผ๋ก ์ฐ๊ฒฐ ์ฐ์ ์ ํ ํ์
์ผ๋ก ์ค์ ๋๋ค.
๋์ผํ ํ์
์ ํด๋์ค ์ค ํ ๊ฐ๋ง @Primary ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ ์ ์๋ค.
Charmander ๋น ๊ฐ์ฒด์ @Primary ์ด๋
ธํ
์ด์
์ด ์ค์ ๋์ด ์์ผ๋ฏ๋ก, PokemonService์ ์์ฑ์๋ก Pokemon ๊ฐ์ฒด๋ฅผ ์ฃผ์
๋ฐ์ผ๋ฉด Charmander ๋น ๊ฐ์ฒด๊ฐ ์ฐ์ ์ ์ผ๋ก ์ฃผ์
๋๋ค.
ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ๐ก @Qualifier ์ด๋
ธํ
์ด์
์ ์ฌ๋ฌ ๊ฐ์ ๋น ๊ฐ์ฒด ์ค์์ ํน์ ๋น ๊ฐ์ฒด๋ฅผ ์ด๋ฆ์ผ๋ก ์ง์ ํ๋ ์ด๋
ธํ
์ด์
์ด๋ค.
ํ๋๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค. @Autowired ์ด๋
ธํ
์ด์
๊ณผ ํจ๊ป @Qualifier ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ ๋น ์ด๋ฆ์ ํตํด ์ฃผ์
ํ ๋น ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ค.
@Service("pokemonServiceQualifier")
public class PokemonService {
/* @Qualifier ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ pikachu ๋น ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ค. */
@Autowired
@Qualifier("pikachu")
private Pokemon pokemon;
public void pokemonAttack() {
pokemon.attack();
}
Charmander, Pikachu, Squirtle, PokemonService ๋ฅผ ๋น ์ค์บ๋ ํ ์ ์๋ basePackages๋ฅผ ์ค์ ํ์ฌ ์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ค.
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section02");
PokemonService pokemonService = context.getBean("pokemonServiceQualifier", PokemonService.class);
pokemonService.pokemonAttack();
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก@Primary ์ด๋
ธํ
์ด์
๊ณผ @Qualifier ์ด๋
ธํ
์ด์
์ด ํจ๊ป ์ฐ์์ ๋ @Qualifier ๊ฐ ์ฐ์ ํ๋ค๋ ๊ฒ๋ ๊ฒฐ๊ณผ๋ฅผ ํตํด ํ์ธํ ์ ์๋ค.
์์ฑ์ ์ฃผ์
์ ๊ฒฝ์ฐ @Qualifier ์ด๋
ธํ
์ด์
์ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ ์์ ๊ธฐ์ฌํ๋ค. ์ญ์ ๋น ์ด๋ฆ์ ํตํด ์ฃผ์
ํ ๋น ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ค.
@Service("pokemonServiceQualifier")
public class PokemonService {
private Pokemon pokemon;
/* @Qualifier ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ฌ squirtle ๋น ๊ฐ์ฒด๋ฅผ ์ง์ ํ๋ค. */
@Autowired
public PokemonService(@Qualifier("squirtle") Pokemon pokemon) {
this.pokemon = pokemon;
}
public void pokemonAttack() {
pokemon.attack();
}
}
๊ผฌ๋ถ๊ธฐ ๋ฌผ๋ํฌ ๋ฐ์ฌ๐๊ฐ์ ํ์
์ ๋น์ ์ฌ๋ฌ ๊ฐ ์ฃผ์
๋ฐ๊ณ ์ถ๋ค๋ฉด Collection ํ์
์ ํ์ฉํ ์ ์๋ค.
List<Pokemon> ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
@Service("pokemonServiceCollection")
public class PokemonService {
/* 1. List ํ์
์ผ๋ก ์ฃผ์
*/
private List<Pokemon> pokemonList;
@Autowired
public PokemonService(List<Pokemon> pokemonList) {
this.pokemonList = pokemonList;
}
public void pokemonAttack() {
pokemonList.forEach(Pokemon::attack);
}
}
Charmander, Pikachu, Squirtle, PokemonService ๋ฅผ ๋น ์ค์บ๋ ํ ์ ์๋ basePackages๋ฅผ ์ค์ ํ์ฌ ์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ค.
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section02");
PokemonService pokemonService = context.getBean("pokemonServiceCollection", PokemonService.class);
pokemonService.pokemonAttack();
ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก
๊ผฌ๋ถ๊ธฐ ๋ฌผ๋ํฌ ๋ฐ์ฌ๐ bean ์ด๋ฆ์ ์ฌ์ ์์ผ๋ก List์ ์ถ๊ฐ ๋์ด ๋ชจ๋ Pokemon ํ์
์ ๋น์ด ์ฃผ์
๋๋ค.๋๋ Map<String, Pokemon> ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
@Service("pokemonServiceCollection")
public class PokemonService {
/* 2. Map ํ์
์ผ๋ก ์ฃผ์
*/
private Map<String, Pokemon> pokemonMap;
@Autowired
public PokemonService(Map<String, Pokemon> pokemonMap) {
this.pokemonMap = pokemonMap;
}
public void pokemonAttack() {
pokemonMap.forEach((k, v) -> {
System.out.println("key : " + k);
System.out.print("๊ณต๊ฒฉ : ");
v.attack();
});
}
}
key : charmander
๊ณต๊ฒฉ : ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ
key : pikachu
๊ณต๊ฒฉ : ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก
key : squirtle
๊ณต๊ฒฉ : ๊ผฌ๋ถ๊ธฐ ๋ฌผ๋ํฌ ๋ฐ์ฌ๐ bean ์ด๋ฆ์ ์ฌ์ ์์ผ๋ก Map์ ์ถ๊ฐ ๋์ด ๋ชจ๋ Pokemon ํ์
์ ๋น์ด ์ฃผ์
๋๋ค.๐ก @Resource ์ด๋
ธํ
์ด์
์ ์๋ฐ์์ ์ ๊ณตํ๋ ๊ธฐ๋ณธ ์ด๋
ธํ
์ด์
์ด๋ค. @Autowired์ ๊ฐ์ ์คํ๋ง ์ด๋
ธํ
์ด์
๊ณผ ๋ค๋ฅด๊ฒ name ์์ฑ ๊ฐ์ผ๋ก ์์กด์ฑ ์ฃผ์
์ ํ ์ ์๋ค.
ํด๋น ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๊ธฐ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์กด์ฑ ์ถ๊ฐ๊ฐ ํ์ํ๋ฏ๋ก Maven Repository์์ javax annoataion์ ๊ฒ์ํ์ฌ build.gradle.kts ํ์ผ์ ์๋์ ๊ฐ์ ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ค.
dependencies {
implementation("javax.annotation:javax.annotation-api:1.3.2")
...์๋ต
}
ํ๋๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค. @Resource ์ด๋
ธํ
์ด์
์ name ์์ฑ์ ์ฃผ์
ํ ๋น ๊ฐ์ฒด์ ์ด๋ฆ์ ์ง์ ํ๋ค.
@Service("pokemonServiceResource")
public class PokemonService {
/* pikachu ์ด๋ฆ์ ๋น ์ง์ */
@Resource(name = "pikachu")
private Pokemon pokemon;
public void pokemonAttack() {
pokemon.attack();
}
}
Charmander, Pikachu, Squirtle, PokemonService ๋ฅผ ๋น ์ค์บ๋ ํ ์ ์๋ basePackages๋ฅผ ์ค์ ํ์ฌ ์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ค.
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section02");
PokemonService pokemonService = context.getBean("pokemonServiceResour", PokemonService.class);
pokemonService.pokemonAttack();
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโกList<Pokemon> ํ์
์ผ๋ก ๋ณ๊ฒฝํ ๋ค name ์์ฑ์ ๋ฐ๋ก ๊ธฐ์ฌํ์ง ์๊ณ ๋์์ํฌ ์ ์๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก๋ name ์์ฑ์ ํตํด ์ฃผ์
ํ์ง๋ง name ์์ฑ์ด ์์ ๊ฒฝ์ฐ Type์ ํตํด ์์กด์ฑ ์ฃผ์
ํ๋ค.
@Service("pokemonServiceResource")
public class PokemonService {
@Resource
private List<Pokemon> pokemonList;
public void pokemonAttack() {
pokemonList.forEach(Pokemon::attack);
}
}
ํ์ด๋ฆฌ ๋ถ๊ฝ ๊ณต๊ฒฉ๐ฅ
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก
๊ผฌ๋ถ๊ธฐ ๋ฌผ๋ํฌ ๋ฐ์ฌ๐ bean ์ด๋ฆ์ ์ฌ์ ์์ผ๋ก List์ ์ถ๊ฐ ๋์ด ๋ชจ๋ Pokemon ํ์
์ ๋น์ด ์ฃผ์
๋๋ค.์ฌ์ฉ ์ ์ ์ํ ์ ์ ํ๋ ์ฃผ์
๊ณผ ์ธํฐ ์ฃผ์
์ ๊ฐ๋ฅํ์ง๋ง ์์ฑ์ ์ฃผ์
์ ๋ถ๊ฐ๋ฅํ๋ค๋ ์ ์ด๋ค.
ํด๋น ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ๊ธฐ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์กด์ฑ ์ถ๊ฐ๊ฐ ํ์ํ๋ฏ๋ก Maven Repository์์ javax inject์ ๊ฒ์ํ์ฌ build.gradle.kts ํ์ผ์ ์๋์ ๊ฐ์ ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ค.
dependencies {
implementation("javax.inject:javax.inject:1")
...์๋ต
}
ํ๋๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค. @Inject ์ด๋
ธํ
์ด์
์ Type ์ผ๋ก ์์กด์ฑ ์ฃผ์
ํ๋ฏ๋ก 3๊ฐ์ ๋์ผํ ํ์
์ ๋น์ด ์๋ ํ์ฌ ์ํฉ์์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค. ๋ฐ๋ผ์ @Named ์ด๋
ธํ
์ด์
์ ํจ๊ป ์ฌ์ฉํด์ ๋น์ ์ด๋ฆ์ ์ง์ ํ๋ฉด ํด๋น ๋น์ ์์กด์ฑ ์ฃผ์
ํ ์ ์๋ค.
@Service("pokemonServiceInject")
public class PokemonService {
/* 1. ํ๋ ์ฃผ์
*/
@Inject
@Named("pikachu")
private Pokemon pokemon;
public void pokemonAttack() {
pokemon.attack();
}
}
Charmander, Pikachu, Squirtle, PokemonService ๋ฅผ ๋น ์ค์บ๋ ํ ์ ์๋ basePackages๋ฅผ ์ค์ ํ์ฌ ์คํ๋ง ์ปจํ
์ด๋๋ฅผ ์์ฑํ๋ค.
ApplicationContext context = new AnnotationConfigApplicationContext("com.ohgiraffers.section02");
PokemonService pokemonService = context.getBean("pokemonServiceInject", PokemonService.class);
pokemonService.pokemonAttack();
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก์์ฑ์๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
@Service("pokemonServiceInject")
public class PokemonService {
private Pokemon pokemon;
/* 2. ์์ฑ์ ์ฃผ์
*/
@Inject
public PokemonService(@Named("pikachu") Pokemon pokemon) {
this.pokemon = pokemon;
}
public void pokemonAttack() {
pokemon.attack();
}
}
@Named ์ด๋
ธํ
์ด์
์ ๊ฒฝ์ฐ ๋ฉ์๋ ๋ ๋ฒจ, ํ๋ผ๋ฏธํฐ ๋ ๋ฒจ์์ ๋ ๋ค ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก์ธํฐ๋ก Pokemon ํ์
์ ๊ฐ์ฒด๋ฅผ ์์กด์ฑ ์ฃผ์
๋ฐ๋ PokemonService ํด๋์ค๋ฅผ ์ ์ธํ๋ค.
@Service("pokemonServiceInject")
public class PokemonService {
private Pokemon pokemon;
/* 3. ์ธํฐ ์ฃผ์
*/
@Inject
public void setPokemon(@Named("pikachu") Pokemon pokemon) {
this.pokemon = pokemon;
}
public void pokemonAttack() {
pokemon.attack();
}
}
ํผ์นด์ธ ๋ฐฑ๋ง๋ณผํธโก@Inject๋ ํ๋ ์ฃผ์
, ์์ฑ์ ์ฃผ์
, ์ธํฐ ์ฃผ์
์ด ๋ชจ๋ ๊ฐ๋ฅํ๋ค.
DI๋ ์คํ๋ง ํ๋ ์์ํฌ์์ ๋งค์ฐ ์ค์ํ ๊ฐ๋ ์ค ํ๋๋ก, ๊ฐ๋ฐ์๋ ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ ์ง์ ๊ด๋ฆฌํ์ง ์๊ณ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ ์ฃผ์ ํด์ฃผ๋ ๋ฐฉ์์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค.
๋ค์ํ DI ์ด๋ ธํ ์ด์ ์ด ์๋๋ฐ ๊ฐ๊ฐ์ ํน์ง๊ณผ ์ฌ์ฉ ๋ฐฉ์์ด ๋ค๋ฅด๋ค.
| @Autowried | @Resource | @Inject | |
|---|---|---|---|
| ์ ๊ณต | Spring | Java | Java |
| ์ง์ ๋ฐฉ์ | ํ๋, ์์ฑ์, ์ธํฐ | ํ๋, ์ธํฐ | ํ๋, ์์ฑ์, ์ธํฐ |
| ๋น ๊ฒ์ ์ฐ์ ์์ | ํ์ โ ์ด๋ฆ | ์ด๋ฆ โ ํ์ | ํ์ โ ์ด๋ฆ |
| ๋น ์ง์ ๋ฌธ๋ฒ | @Autowired | ||
| @Qualifier(โnameโ) | @Resource(name=โnameโ) | @Inject | |
| @Named(โnameโ) |