"Guice"의 두 판 사이의 차이
잔글 |
잔글 |
||
31번째 줄: | 31번째 줄: | ||
{| border="1" cellspacing="0" cellpadding="2" style="width: 100%;" | {| border="1" cellspacing="0" cellpadding="2" style="width: 100%;" | ||
|- | |- | ||
− | | style="text-align: center; background-color: rgb(241, 241, 241);" | | + | | style="text-align: center; background-color: rgb(241, 241, 241);" | 구분 |
− | | style="text-align: center; background-color: rgb(241, 241, 241);" | | + | | style="text-align: center; background-color: rgb(241, 241, 241);" | 상세 |
− | |||
|- | |- | ||
| style="text-align: center;" | Construction Injection | | style="text-align: center;" | Construction Injection | ||
42번째 줄: | 41번째 줄: | ||
bind(Person.class).to(PersonImpl.class).in(Singleton.class); | bind(Person.class).to(PersonImpl.class).in(Singleton.class); | ||
− | |||
− | |||
− | |||
|- | |- | ||
53번째 줄: | 49번째 줄: | ||
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">''' protected void configure() {<br/> bind(Person.class);<br/> }'''</span><br/>} | public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">''' protected void configure() {<br/> bind(Person.class);<br/> }'''</span><br/>} | ||
− | |||
|- | |- | ||
| style="text-align: center;" | Method Injection | | style="text-align: center;" | Method Injection | ||
61번째 줄: | 56번째 줄: | ||
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">''' protected void configure() {<br/> bind(Person.class);<br/> }'''</span><br/>} | public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">''' protected void configure() {<br/> bind(Person.class);<br/> }'''</span><br/>} | ||
− | | <br/> | + | |- |
+ | | style="text-align: center;" | DI 테스트 | ||
+ | | <span style="line-height: 20.7999992370605px;">public class PersonServiceTest {</span><br/><span style="line-height: 20.7999992370605px;"> private </span><span style="line-height: 20.7999992370605px; color: rgb(0, 0, 255);">'''Inject'''</span><span style="line-height: 20.7999992370605px;"> injector = null;</span><br/><br/><span style="line-height: 20.7999992370605px;"> @Before</span><br/><span style="line-height: 20.7999992370605px;"> public void setup() {</span><br/><span style="line-height: 20.7999992370605px;"> injector = </span><span style="line-height: 20.7999992370605px; color: rgb(0, 0, 255);">'''Guice.createInjector(new PersonModule())'''</span><span style="line-height: 20.7999992370605px;">;</span><br/><span style="line-height: 20.7999992370605px;"> }</span><br/><br/><span style="line-height: 20.7999992370605px;"> @Test</span><br/><span style="line-height: 20.7999992370605px;"> public void getPersonService() {</span><br/><span style="line-height: 20.7999992370605px;"> PersonService ps;</span><br/><span style="line-height: 20.7999992370605px;"> </span><br/><span style="line-height: 20.7999992370605px;"> ps = </span><span style="line-height: 20.7999992370605px; color: rgb(0, 0, 255);">'''injector.getInstance(PersonService.class)'''</span><span style="line-height: 20.7999992370605px;">;</span><br/><span style="line-height: 20.7999992370605px;"> }</span><br/><span style="line-height: 20.7999992370605px;">}</span><br/> | ||
|} | |} | ||
2014년 10월 30일 (목) 14:58 판
Google Guice를 정리 합니다.
Guice 개요
Google에서 발표한 Java 5 이상에서 지원되는 가벼운 DI container으로 Annotation을 사용하여 DI를 구현
IoC (Inversion of Control, 제어의 역전)
프로그램에 의해서 객체(Bean)가 관리되는 것이 아니라, 위임을 통해서 IoC Container가 객체의 생성과 생명주기를 관리
특정 작업을 위한 객체를 프로그램이 직접 생성하는 것이 아니라, IoC Container를 통해서 공급 받음
DL (Dependency Lookup, 의존성 검색)
개발자가 Container에서 제공하는 API를 사용하여 사용하고자 하는 객체(Bean)을 찾는 것
DI (Dependency Injection, 의존성 주입)
의존성 주입 방법
- Constructor Injection
- Setter Injection
- Method Injection
구분 | 상세 |
Construction Injection |
public class PersonService { public class PersonModule extends AbstractModule { bind(Person.class).to(PersonImpl.class).in(Singleton.class); |
Setter Injection |
public class PersonService { public class PersonModule extends AbstractModule { |
Method Injection |
public class PersonService { public class PersonModule extends AbstractModule { |
DI 테스트 | public class PersonServiceTest { private Inject injector = null; @Before public void setup() { injector = Guice.createInjector(new PersonModule()); } @Test public void getPersonService() { PersonService ps; ps = injector.getInstance(PersonService.class); } } |