Search

Spring DI

Spring DI

: μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬λ₯Ό μ‚¬μš©ν•˜μ—¬ μ˜μ‘΄μ„± μ£Όμž…μ„ ν•˜λŠ” 방법

빈 등둝 방법

1.
XML μ„€μ •
a.
root-context.xml
b.
<bean id=”” class=”” /> 빈 νƒœκ·Έλ‘œ 등둝
2.
μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ 등둝
a.
@Component, @Service, @Repository, @Controller
3.
μžλ°” 빈 μ„€μ • 파일둜 등둝
a.
@Configuration μ„€μ • 클래슀 μ •μ˜
b.
@Bean
객체λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό μ •μ˜

빈 등둝 - XML μ„€μ •

β€’
root-context.xml
<!-- BoardDAO λΉˆμ„ μ •μ˜ --> <bean id="boardDAO" class="com.aloha.spring.dao.BoardDAO"> </bean>
XML
볡사

빈 등둝 - @μ–΄λ…Έν…Œμ΄μ…˜

μ–΄λ…Έν…Œμ΄μ…˜
μ—­ν• 
@Component
일반적인 μŠ€ν”„λ§ 빈으둜 μ‚¬μš©λ¨.
@Controller
Spring MVC 컨트둀러둜 μ‚¬μš©λ˜λŠ” 클래슀λ₯Ό λ‚˜νƒ€λƒ„.
@Service
λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” μ„œλΉ„μŠ€ 클래슀λ₯Ό λ‚˜νƒ€λƒ„.
@Repository
데이터 μ•‘μ„ΈμŠ€ 객체 (DAO)λ₯Ό λ‚˜νƒ€λƒ„.
β€’
servlet-context.xml
<context:component-scan base-package="com.aloha.spring" />
Java
볡사
μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ λΉˆμ„ λ“±λ‘ν•˜λ €λ©΄, servlet-context.xml 에 μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” νƒœκ·Έλ₯Ό λ“±λ‘ν•΄μ•Όν•œλ‹€.
μŠ€ν”„λ§ λΉˆμ„ 검색할 base-package 속성에 μ§€μ •ν•˜λŠ” νŒ¨ν‚€μ§€ ν•˜μœ„μ˜ 경둜의 μžˆλŠ” μŠ€ν”„λ§ 빈 클래슀λ₯Ό μžλ™μœΌλ‘œ κ²€μƒ‰ν•˜κ³  빈으둜 λ“±λ‘ν•œλ‹€.
β€’
클래슀
@Component public class MyComponent { }
Java
볡사
μŠ€ν”„λ§ 빈으둜 등둝할 ν΄λž˜μŠ€λŠ” `@Component` μ–΄λ…Έν…Œμ΄μ…˜μ„ λΆ™μ—¬μ£Όλ©΄ μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ” μ‹œ, μΈμ‹λ˜μ–΄ Ioc μ»¨ν…Œμ΄λ„ˆμ— λ“±λ‘λœλ‹€.
β€’
BoardService.java
@Service public class BoardService { }
Java
볡사
`@Service` μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” μŠ€ν”„λ§ 빈 클래슀둜 μ§€μ •ν•˜μ—¬ Ioc μ»¨ν…Œμ΄ν„°μ— 빈으둜 λ“±λ‘λœλ‹€.
β€’
BoardDAO.java
@Repository public class BoardDAO { }
Java
볡사
`@Repository` μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ 데이터 μ•‘μ„ΈμŠ€ κ³„μΈ΅μ˜ ν΄λž˜μŠ€μ— μ§€μ •ν•˜μ—¬ Ioc μ»¨ν…Œμ΄ν„°μ— 빈으둜 λ“±λ‘λœλ‹€.
β€’
BoardControler.java
@Controller public class BoardController { }
Java
볡사
`@Controller` μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜λ©΄ μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 컨트둀러 클래슀둜 μ§€μ •ν•˜μ—¬ Ioc μ»¨ν…Œμ΄ν„°μ— 빈으둜 λ“±λ‘λœλ‹€.

빈 등둝 - μžλ°” μ„€μ • 파일둜 등둝(μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”)

β€’
Config.java
@Configuration // μŠ€ν”„λ§ μ„€μ • 빈으둜 등둝 public class Config { // 1. 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό μ •μ˜ // 2. λ©”μ†Œλ“œμ— @Bean μ–΄λ…Έν…Œμ΄μ…˜ μ •μ˜ @Bean // IoC μ»¨ν…Œμ΄λ„ˆμ— 빈으둜 등둝 public Board board() { return new Board("제λͺ©", "μž‘μ„±μž", "λ‚΄μš©"); } }
Java
볡사
β€’
`@Configuration` : 클래슀λ₯Ό μŠ€ν”„λ§μ˜ Java 기반 μ„€μ • 클래슀둜 지정
β€’
`@Bean` : @Configuration μ–΄λ…Έν…Œμ΄μ…˜μ„ 가진 클래슀 λ‚΄μ—μ„œ λ©”μ„œλ“œμ— λΆ€μ—¬ν•˜μ—¬ ν•΄λ‹Ή λ©”μ„œλ“œκ°€ μŠ€ν”„λ§ 빈(Bean)을 μƒμ„±ν•˜κ³  κ΄€λ¦¬ν•˜λ„λ‘ 지정
β€’
pom.xml
: @Configuration μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ”. CGLIB μ˜μ‘΄μ„±μ΄ ν•„μš”
<!-- CGLIB --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.1</version> </dependency>
XML
볡사

CGLIB

μžλ°” ν”„λ‘μ‹œ 객체λ₯Ό μƒμ„±ν•˜κ³  μˆ˜μ •ν•˜κΈ° μœ„ν•œ 라이브러리 AOP(Aspect-Oriented Programming)와 DI(Dependency Injection)λ₯Ό κ΅¬ν˜„ν•˜λŠ” 데 μ‚¬μš©

μ˜μ‘΄μ„± μžλ™ μ£Όμž…

μ˜μ‘΄μ„± μžλ™ μ£Όμž… 방법

β€’
κΈ°λ³Έ μ˜μ‘΄μ„± μžλ™ μ£Όμž…
β€’
νƒ€μž… 기반 μ£Όμž…
β€’
μƒμ„±μž μ£Όμž…
β€’
λ©”μ„œλ“œ μ£Όμž…

μ˜μ‘΄μ„± μžλ™ μ£Όμž… μ–΄λ…Έν…Œμ΄μ…˜

β€’
@Autowired
β€’
@Resource
β€’
@Inject

μ˜μ‘΄μ„± μžλ™ μ£Όμž… 방법

κΈ°λ³Έ μ˜μ‘΄μ„± μžλ™ μ£Όμž…

β€’
`@Autowired` : μ˜μ‘΄μ„± μžλ™ μ£Όμž… μ–΄λ…Έν…Œμ΄μ…˜
μŠ€ν”„λ§ μ»¨ν…Œμ΄λ„ˆκ°€ 빈의 μ˜μ‘΄μ„±μ„ μžλ™μœΌλ‘œ ν•΄κ²°ν•˜λ„λ‘ μ§€μ‹œ
@Autowired private MyBean myBean;
Java
볡사

νƒ€μž… 기반 μ£Όμž…

β€’
빈 이름을 μ§€μ •ν•΄μ„œ μ˜μ‘΄μ„± μ£Όμž…ν•˜κΈ°
β—¦
@Autowired
β—¦
@Qualifier("λΉˆμ΄λ¦„")
@Autowired @Qualifier("myBean") private MyBean myBean;
Java
볡사

μƒμ„±μž μ£Όμž…

@Service public class BoardService { private BoardDAO boardDAO; @Autowired public BoardService(BoardDAO boardDAO) { this.boardDAO = boardDAO; } }
Java
볡사

λ©”μ„œλ“œ μ£Όμž…

@Service public class BoardService { private BoardDAO boardDAO; @Autowired public void setBoardDAO(BoardDAO boardDAO) { this.boardDAO = boardDAO; } }
Java
볡사

μ˜μ‘΄μ„± μžλ™ μ£Όμž… μ–΄λ…Έν…Œμ΄μ…˜

β€’
@Autowired
β€’
@Resource
β€’
@Inject

@Autowired

@Autowired(required = [false/true])
Java
볡사
@Autowiredμ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ 빈 이름 μ§€μ •ν•΄μ„œ μ£Όμž…ν•˜λ €λ©΄, @Qualifier μ–΄λ…Έν…Œμ΄μ…˜κ³Ό ν•¨κ»˜ μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.

@Autowired μ‚¬μš©

β€’
ν•„λ“œ(λ³€μˆ˜)에 μ‚¬μš©
β€’
μƒμ„±μžμ— μ‚¬μš©
β€’
λ©”μ†Œλ“œμ— μ‚¬μš©
β€’
ν•„λ“œ(λ³€μˆ˜)에 μ‚¬μš©
@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { // ν•„λ“œμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… @Autowired private BoardDAO boardDAO; // 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. ... }
Java
볡사
β€’
μƒμ„±μžμ— μ‚¬μš©
@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { private final BoardDAO boardDAO; @Autowired // μƒμ„±μžμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… public BoardServiceImpl(BoardDAO boardDAO) { this.boardDAO = boardDAO; } ... }
Java
볡사
ν•„λ“œμ— μ‚¬μš©ν•˜μ—¬ μ˜μ‘΄μ„± μžλ™ μ£Όμž…μ„ ν•˜λŠ” 방식보닀, μƒμ„±μž μ£Όμž… 방식이 더 ꢌμž₯λ˜λŠ” λ°©μ‹μž…λ‹ˆλ‹€.
β€’
μˆœν™˜ μ˜μ‘΄μ„± 방지
β€’
ν•„μˆ˜ μ˜μ‘΄μ„± κ°•μ œ
β€’
λ©”μ†Œλ“œμ— μ‚¬μš©
@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { private final BoardDAO boardDAO; @Autowired // λ©”μ†Œλ“œμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… public setBoardDAO(BoardDAO boardDAO) { this.boardDAO = boardDAO; } ... }
Java
볡사
μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ—μ„œ μžλ™μœΌλ‘œ ν•΄λ‹Ή λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬, ν•„μš”ν•œ μ˜μ‘΄μ„±μ„ μ£Όμž…ν•©λ‹ˆλ‹€.

@Autowired - required μ˜΅μ…˜

@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { // ν•„λ“œμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… @Autowired(required = false) private BoardDAO boardDAO; // 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. ... }
Java
볡사
β€’
@Autowired(required = false)
β—¦
ν•„μˆ˜κ°’ μ•„λ‹ˆλ‹€.
β—¦
ν•΄λ‹Ή 객체가 빈으둜 λ“±λ‘λ˜μ–΄ μžˆμ§€ μ•ŠμœΌλ©΄ null 둜 지정
β—¦
λ“±λ‘λ˜μ§€ μ•ŠλŠ” λΉˆμ„ κ°€μ Έμ˜¬ λ•Œ, μ—λŸ¬ 방지할 수 μžˆλ‹€.
β€’
@Autowired(required = true) [κΈ°λ³Έκ°’]
β—¦
true μ˜Άμ…˜κ°’μ΄ κΈ°λ³Έκ°’μœΌλ‘œ, μƒλž΅ν•΄λ„ λ™μΌν•˜κ²Œ μ μš©λ©λ‹ˆλ‹€.
required : ν•„μˆ˜ (ν•„μˆ˜κ°’ μ—¬λΆ€λ₯Ό 지정할 λ•Œ 주둜 μ‚¬μš©ν•˜λŠ” ν‚€μ›Œλ“œ)

@Qualifier μ–΄λ…Έν…Œμ΄μ…˜

같은 νƒ€μž…(λΆ€λͺ¨ 클래슀, μΈν„°νŽ˜μ΄μŠ€)의 μ—¬λŸ¬ 빈이 μžˆλŠ” 경우, 빈 이름을 κΈ°μ€€μœΌλ‘œ μ˜μ‘΄μ„± μ£Όμž…μ„ ν•΄μ£ΌλŠ” μ–΄λ…Έν…Œμ΄μ…˜
β€’
PostService
public interface PostService { ... }
Java
볡사
β€’
BoardServiceImpl
@Service("boardService") // 빈 이름을 "boardService" 둜 빈 등둝 public class BoardServiceImpl implements PostService { }
Java
볡사
β€’
CommentServiceImpl
@Service("commentService") // 빈 이름을 "commentService" 둜 빈 등둝 public class CommentServiceImpl implements PostService { }
Java
볡사
β€’
Main
@Component public class Main { @Autowired @Qualifier("boardService") // "boardService" λΌλŠ” μ΄λ¦„μ˜ 빈 μ£Όμž…(boardServiceImpl) private PostService boardService; @Autowired @Qualifier("commentService") // "commentService" λΌλŠ” μ΄λ¦„μ˜ 빈 μ£Όμž…(commentServiceImpl) private PostService commentService; }
Java
볡사
boardServiceImpl, commentServiceImpl 두 빈(객체) λͺ¨λ‘ PostService νƒ€μž…μž…λ‹ˆλ‹€. μ˜μ‘΄μ„± μžλ™ μ£Όμž…μ„ ν•˜κΈ° μœ„ν•΄μ„œ μ•„λž˜μ™€ 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ μ–΄λ–€ λΉˆμ„ κ°€μ Έμ˜¬ 지 λͺ¨ν˜Έν•΄μ§€κ³ , λ˜ν•œ μ—λŸ¬κ°€ λ°œμƒν•©λ‹ˆλ‹€.
@Autowired private PostService boardService;
Java
볡사
λ”°λΌμ„œ, @Qualifier μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•΄μ„œ μ˜μ‘΄μ„± μ£Όμž… μ‹œ 빈 이름을 κΈ°μ€€μœΌλ‘œ μ •ν™•ν•œ λΉˆμ„ μ£Όμž…ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

@Resource

μ˜μ‘΄μ„± μžλ™ μ£Όμž…μ„ ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜ μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ˜ 기반이 μ•„λ‹ˆλΌ javax νŒ¨ν‚€μ§€μ— κΈ°λ°˜ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜
@Resource(name = "빈 이름")
Java
볡사
@Resource μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œλŠ” 빈 이름을 λ°”λ‘œ μ§€μ •ν•΄μ„œ μ£Όμž…ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
ν•„λ“œμ™€ λ©”μ†Œλ“œμ—λ§Œ μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€.
μƒμ„±μž μ£Όμž… 방식은 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
β€’
@Autowired
β—¦
νŒ¨ν‚€μ§€ : org.springframework.beans.factory.annotation
β—¦
Spring μ „μš©μ˜ μ–΄λ…Έν…Œμ΄μ…˜
β—¦
νƒ€μž…μ„ κΈ°μ€€μœΌλ‘œ μ˜μ‘΄μ„± μžλ™ μ£Όμž…
β€’
@Resource
β—¦
νŒ¨ν‚€μ§€ : javax.annotation
β—¦
Java ν‘œμ€€ μ–΄λ…Έν…Œμ΄μ…˜
β—¦
이름을 κΈ°μ€€μœΌλ‘œ μ˜μ‘΄μ„± μžλ™ μ£Όμž…

@Resouce μ–΄λ…Έν…Œμ΄μ…˜ μ‚¬μš©

β€’
ν•„λ“œμ— μ‚¬μš©
β€’
λ©”μ†Œλ“œ 에 μ‚¬μš©
β€’
ν•„λ“œμ— μ‚¬μš©
@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { // ν•„λ“œμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… @Resource(name = "boardDAOBean") // 빈 이름을 κΈ°μ€€μœΌλ‘œ μ˜μ‘΄μ„± μ£Όμž… private BoardDAO boardDAO; // 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. ... }
Java
볡사
@Repository("boardDAOBean") // @Repository("빈 이름") public class BoardDAO { ... }
Java
볡사
β€’
λ©”μ†Œλ“œ 에 μ‚¬μš©
@Service // μ˜μ‘΄μ„±μ„ μ£Όμž… λ°›λŠ” ν΄λž˜μŠ€λ„ 빈으둜 λ“±λ‘λ˜μ–΄μžˆμ–΄μ•Όν•œλ‹€. public class BoardServiceImpl implements BoardService { private final BoardDAO boardDAO; @Resource("boardDAOBean") // λ©”μ†Œλ“œμ— μ‚¬μš© - μ˜μ‘΄μ„± μžλ™ μ£Όμž… public setBoardDAO(BoardDAO boardDAO) { this.boardDAO = boardDAO; } ... }
Java
볡사

@Inject

μžλ°” 기반의 μ˜μ‘΄μ„± μ£Όμž… μ–΄λ…Έν…Œμ΄μ…˜
β€’
ν•„λ“œ μ£Όμž…, μƒμ„±μž μ£Όμž…, λ©”μ†Œλ“œ μ£Όμž… λͺ¨λ‘ κ°€λŠ₯ν•©λ‹ˆλ‹€.
β€’
BeanFactory
β—¦
Spring IoC μ»¨ν…Œμ΄λ„ˆμ— μ•‘μ„ΈμŠ€ν•˜κΈ° μœ„ν•œΒ λ£¨νŠΈΒ μΈν„°νŽ˜μ΄μŠ€
β—¦
빈(Bean) 객체의 생성, ꡬ성, 및 관리λ₯Ό λ‹΄λ‹Ή
β€’
ApplicationContext
β—¦
BeanFactoryλ₯Ό ν™•μž₯ν•œ μΈν„°νŽ˜μ΄μŠ€
β—¦
빈의 미리 μ΄ˆκΈ°ν™”, κ΅­μ œν™” 지원, 이벀트 λ°œν–‰, λ¦¬μ†ŒμŠ€ λ‘œλ”© λ“± 더 λ§Žμ€ κΈ°λŠ₯을 제곡
β—¦
AOP 톡합, 이벀트 κ²Œμ‹œ, λͺ¨λ“  μœ ν˜•μ˜ Bean λ²”μœ„ 등을 지원
β€’
XmlBeanFactory
β—¦
BeanFactory κ΅¬ν˜„ 쀑 ν•˜λ‚˜λ‘œ, XML νŒŒμΌμ—μ„œ 빈 μ„€μ • 정보λ₯Ό λ‘œλ“œν•˜λŠ” 데 μ‚¬μš©
β—¦
Spring 3.1λΆ€ν„° deprecated
β€’
AnnotationConfigApplicationContext
β—¦
μ–΄λ…Έν…Œμ΄μ…˜ 기반의 섀정을 μ‚¬μš©ν•˜μ—¬ μŠ€ν”„λ§ λΉˆμ„ κ΄€λ¦¬ν•˜λŠ” μ»¨ν…Œμ΄λ„ˆ
β—¦
@Configuration μ–΄λ…Έν…Œμ΄μ…˜κ³Ό @ComponentScan μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ—¬ μ„€μ • 정보λ₯Ό 지정
β€’
ClassPathXmlApplicationContext
β—¦
클래슀패슀(classpath)μ—μ„œ XML νŒŒμΌμ„ λ‘œλ“œν•˜μ—¬ μŠ€ν”„λ§ λΉˆμ„ μ»¨ν…Œμ΄λ„ˆ
β—¦
주둜 XML 파일 기반의 섀정을 μ‚¬μš©ν•  λ•Œ ν™œμš©
β€’
FileSystemXmlApplicationContext
β—¦
파일 μ‹œμŠ€ν…œμ—μ„œ XML νŒŒμΌμ„ λ‘œλ“œν•˜μ—¬ μŠ€ν”„λ§ λΉˆμ„ κ΅¬μ„±ν•˜λŠ” μ»¨ν…Œμ΄λ„ˆ
β—¦
클래슀패슀 μ΄μ™Έμ˜ κ²½λ‘œμ—μ„œ XML 파일 기반의 섀정을 μ‚¬μš©ν•  λ•Œ μ‚¬μš©
β€’
XmlWebApplicationContext
β—¦
μ›Ή ν™˜κ²½μ—μ„œ ν•„μš”ν•œ 빈 및 섀정을 κ΄€λ¦¬ν•˜λŠ” μ»¨ν…Œμ΄λ„ˆ
β—¦
주둜 μ„œλΈ”λ¦Ώ 기반의 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©

IoC μ»¨ν…Œμ΄λ„ˆ 객체λ₯Ό μƒμ„±ν•΄μ„œ 빈 κ°€μ Έμ˜€κΈ°

β€’
μ»΄ν¬λ„ŒνŠΈ νŒ¨ν‚€μ§€λ₯Ό μ§€μ •ν•΄μ„œ κ°€μ Έμ˜€κΈ°
β€’
@Configuration μ„€μ • ν΄λž˜μŠ€λ‘œλΆ€ν„° κ°€μ Έμ˜€κΈ°
β€’
XML μ„€μ •νŒŒμΌλ‘œλΆ€ν„° κ°€μ Έμ˜€κΈ°

μ»΄ν¬λ„ŒνŠΈ νŒ¨ν‚€μ§€λ₯Ό μ§€μ •ν•΄μ„œ κ°€μ Έμ˜€κΈ°

ApplicationContext context = new AnnotationConfigApplicationContext("com.aloha.spring"); // νŒ¨ν‚€μ§€ 이름 지정 Board board = context.getBean(Board.class);
Java
볡사

@Configuration μ„€μ • ν΄λž˜μŠ€λ‘œλΆ€ν„° κ°€μ Έμ˜€κΈ°

β€’
Config.java
@Configuration // μŠ€ν”„λ§ μ„€μ • 빈으둜 등둝 public class Config { // λΉˆμ„ λ“±λ‘ν•΄μ£ΌλŠ” 클래슀둜 지정 // μžλ°” μ½”λ“œλ‘œ λΉˆμ„ λ“±λ‘ν•˜κ² λ‹€. // 1. 객체λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œλ₯Ό μ •μ˜ // 2. λ©”μ†Œλ“œμ— @Bean μ–΄λ…Έν…Œμ΄μ…˜ μ •μ˜ @Bean // IoC μ»¨ν…Œμ΄λ„ˆμ— 빈으둜 등둝 public Board getBoard() { return new Board("제λͺ©", "μž‘μ„±μž", "λ‚΄μš©"); } }
Java
볡사
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); Board board = context.getBean(Board.class);
Java
볡사

XML μ„€μ •νŒŒμΌλ‘œλΆ€ν„° κ°€μ Έμ˜€κΈ°

ApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml"); // XML 파일 경둜 지정 Board board = context.getBean(Board.class);
Java
볡사
Spring Legacy ν”„λ‘œμ νŠΈμ˜ 경우, μžμ›(λ¦¬μ†ŒμŠ€) 파일과 μ„€μ • νŒŒμΌμ„ μ €μž₯ν•˜λŠ” κΈ°λ³Έ κ²½λ‘œλŠ” src/main/resources λ””λ ‰ν† λ¦¬μž…λ‹ˆλ‹€.

Spring