"Guice"의 두 판 사이의 차이

오픈소스 비즈니스 컨설팅
둘러보기로 가기 검색하러 가기
잔글
잔글
 
(같은 사용자의 중간 판 4개는 보이지 않습니다)
1번째 줄: 1번째 줄:
 
Google Guice를 정리 합니다.
 
Google Guice를 정리 합니다.
 +
 +
*홈페이지 : [https://github.com/google/guice https://github.com/google/guice]
 +
**Getting Started : [https://github.com/google/guice/wiki/GettingStarted https://github.com/google/guice/wiki/GettingStarted]
 +
**사용자 가이드 : [https://github.com/google/guice/wiki/Motivation https://github.com/google/guice/wiki/Motivation]
 +
**Java Doc : [http://google.github.io/guice/api-docs/3.0/javadoc/packages.html http://google.github.io/guice/api-docs/3.0/javadoc/packages.html]
 +
*다운로드 : 
 +
*라이선스 : [[Apache 2.0|Apache 2.0]]
 +
*플랫폼 : 
  
 
== Guice 개요 ==
 
== Guice 개요 ==
31번째 줄: 39번째 줄:
 
{| 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);" | Injection
+
| style="text-align: center; background-color: rgb(241, 241, 241);" | 구분
| style="text-align: center; background-color: rgb(241, 241, 241);" | DI 적용
+
| style="text-align: center; background-color: rgb(241, 241, 241);" | 상세
| style="text-align: center; background-color: rgb(241, 241, 241);" | DI 테스트
 
 
|-
 
|-
 
| style="text-align: center;" | Construction Injection
 
| style="text-align: center;" | Construction Injection
39번째 줄: 46번째 줄:
 
public class PersonService {<br/>&nbsp; &nbsp; private Person person;<br/>&nbsp; &nbsp;&nbsp;<br/>&nbsp; &nbsp; <span style="color:#0000FF;">'''@Inject'''</span><br/>&nbsp; &nbsp; public PersonService(Person person) {<br/>&nbsp; &nbsp; &nbsp; &nbsp; this.person = person;<br/>&nbsp; &nbsp; }<br/>}
 
public class PersonService {<br/>&nbsp; &nbsp; private Person person;<br/>&nbsp; &nbsp;&nbsp;<br/>&nbsp; &nbsp; <span style="color:#0000FF;">'''@Inject'''</span><br/>&nbsp; &nbsp; public PersonService(Person person) {<br/>&nbsp; &nbsp; &nbsp; &nbsp; this.person = person;<br/>&nbsp; &nbsp; }<br/>}
  
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">'''&nbsp; &nbsp; protected void configure() {<br/>&nbsp; &nbsp; &nbsp; &nbsp; bind(Person.class);<br/>&nbsp; &nbsp; }'''</span><br/>}
+
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">'''&nbsp; &nbsp; protected void configure() {<br/>&nbsp; &nbsp; &nbsp; &nbsp; bind(Person.class).to(PersonImpl.class);<br/>&nbsp; &nbsp; }'''</span><br/>}
 
 
bind(Person.class).to(PersonImpl.class).in(Singleton.class);
 
 
 
| <span style="line-height: 20.7999992370605px;"></span>
 
public class PersonServiceTest {<br/>&nbsp; &nbsp; private <span style="color:#0000FF;">'''Inject'''</span> injector = null;<br/><br/>&nbsp;&nbsp; &nbsp;@Before<br/>&nbsp;&nbsp; &nbsp;public void setup() {<br/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;injector = <span style="color:#0000FF;">'''Guice.createInjector(new PersonModule())'''</span>;<br/>&nbsp;&nbsp; &nbsp;}<br/><br/>&nbsp;&nbsp; &nbsp;@Test<br/>&nbsp; &nbsp; public void getPersonService() {<br/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;PersonService ps;<br/>&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;ps = <span style="color:#0000FF;">'''injector.getInstance(PersonService.class)'''</span>;<br/>&nbsp;&nbsp; &nbsp;}<br/>}
 
  
 
|-
 
|-
 
| style="text-align: center;" | Setter Injection
 
| style="text-align: center;" | Setter Injection
 
|  
 
|  
public class PersonService {<br/>&nbsp; &nbsp; private Person person;<br/>&nbsp; &nbsp;&nbsp;<br/>&nbsp; &nbsp; <span style="color:#0000FF;">'''@Inject'''</span><br/>&nbsp; &nbsp; public setPerson(Person person) {<br/>&nbsp; &nbsp; &nbsp; &nbsp; this.person = person;<br/>&nbsp; &nbsp; }<br/>}
 
  
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">'''&nbsp; &nbsp; protected void configure() {<br/>&nbsp; &nbsp; &nbsp; &nbsp; bind(Person.class);<br/>&nbsp; &nbsp; }'''</span><br/>}
 
  
| <br/>
 
 
|-
 
|-
 
| style="text-align: center;" | Method Injection
 
| style="text-align: center;" | Method Injection
 
|  
 
|  
public class PersonService {<br/>&nbsp; &nbsp; private Person person;<br/>&nbsp; &nbsp;&nbsp;<br/>&nbsp; &nbsp; <span style="color:#0000FF;">'''@Inject'''</span><br/>&nbsp; &nbsp; public myMethod(Person person) {<br/>&nbsp; &nbsp; &nbsp; &nbsp; this.person = person;<br/>&nbsp; &nbsp; }<br/>}
 
  
public class PersonModule extends <span style="color:#0000FF;">'''AbstractModule'''</span> {<br/><span style="color:#0000FF;">'''&nbsp; &nbsp; protected void configure() {<br/>&nbsp; &nbsp; &nbsp; &nbsp; bind(Person.class);<br/>&nbsp; &nbsp; }'''</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;">&nbsp; &nbsp; private&nbsp;</span><span style="line-height: 20.7999992370605px; color: rgb(0, 0, 255);">'''Inject'''</span><span style="line-height: 20.7999992370605px;">&nbsp;injector = null;</span><br/><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp;@Before</span><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp;public void setup() {</span><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;injector =&nbsp;</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;">&nbsp;&nbsp; &nbsp;}</span><br/><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp;@Test</span><br/><span style="line-height: 20.7999992370605px;">&nbsp; &nbsp; public void getPersonService() {</span><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;PersonService personService</span><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp;&nbsp;</span><br/><span style="line-height: 20.7999992370605px;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;personService =&nbsp;</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;">&nbsp;&nbsp; &nbsp;}</span><br/><span style="line-height: 20.7999992370605px;">}</span><br/>
 
|}
 
|}
  
69번째 줄: 68번째 줄:
 
*[http://www.mimul.com/pebble/default/2009/10/16/1255700940000.html http://www.mimul.com/pebble/default/2009/10/16/1255700940000.html]
 
*[http://www.mimul.com/pebble/default/2009/10/16/1255700940000.html http://www.mimul.com/pebble/default/2009/10/16/1255700940000.html]
 
*[http://www.nextree.co.kr/p11247/ http://www.nextree.co.kr/p11247/]
 
*[http://www.nextree.co.kr/p11247/ http://www.nextree.co.kr/p11247/]
*http://isstory83.tistory.com/91
+
*[http://isstory83.tistory.com/91 http://isstory83.tistory.com/91]
 
[[Category:Java|Category:Java]]
 
[[Category:Java|Category:Java]]

2014년 10월 30일 (목) 15:26 기준 최신판

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 {
    private Person person;
    
    @Inject
    public PersonService(Person person) {
        this.person = person;
    }
}

public class PersonModule extends AbstractModule {
    protected void configure() {
        bind(Person.class).to(PersonImpl.class);
    }

}

Setter Injection


Method Injection


DI 테스트 public class PersonServiceTest {
    private Inject injector = null;

    @Before
    public void setup() {
        injector = Guice.createInjector(new PersonModule());
    }

    @Test
    public void getPersonService() {
        PersonService personService
   
        personService = injector.getInstance(PersonService.class);
    }
}

참고 문헌