스프링 DI (2.5)

Spring 2009. 1. 6. 17:59

스프링 컨테이너의 주요 인터페이스


1.1 BeanFactory인터페이스 

org.springframework.beans.factory.BeanFactory는 빈 객체를 관리하고 각 빈 객체 간의 의존 관계를 설정해 주는 기능을 제공.
XmlBeanFactory클래스가 구현.
Resource인터페이스를 사용하여 다양한 종류의 자원을 동일한 방식으로 표현.
Resourcce res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
ParserFactory factory = (ParserFactory)factory.getBean("parserFactory");

특정 리소스로부터 설정 정보를 읽어와 XmlBeanFactory객체를 생성한 뒤에는 getBean()메소드를 이용하여 알맞은 빈을 가져와 사용하면 된다.

1.2 ApplicationContext , WebApplicationContext 인터페이스
빈팩토리인터페이스를 상속받은 인터페이스로서 빈팩토리가 제공하는 빈 관리 기능 이외에 파일과 같은 자원처리추상화, 메시지 지원 및 국제화지원, 이벤트 지원등 추가적인 기능을 제공하고 있다. BeanFactory보다 ApplicationContext 구현 클래스르 주로 사용한다.

빈(Bean)의 생성과 의존관계 설정

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">


<bean id = "writeArticleService"  class = "kame.spring.chap02.WriteArticleServiceImpl" >
    <constructor-arg>
        <ref bean = "articleDao" />
    </constructor-arg>

</bean>

<bean id = "articleDao" class = "kame.spring.chap02.OracleArticleDao" >
</bean>


bean의 id는 객체명이다. class는 이 객체를 참조하는 클래스가 있는 경로를 써주면 된다.
생성자를 통해 의존하는 객체르 전달받는 경우 <constructor-arg>태그를 이용하여 의존하는 객체를 전달할 수 있다.
위 코드는 WriteArticleServiceImpl클래스의 생성자에 식별 값이 "articleDao"인 빈 객체를 전달한다는 것을 의미한다.
코드로는....
OracleArticleDao articleDao = new OracleArticleDao();
WriteArticleServiceImpl writeArticleService = new WriteArticleServiceImpl(articleDao);

ref bean을 안쓰고 직접 constructor-arg ref="articleDao" 도 가능하다. 또한 <value>값도 설정할 수 있는데...
<constructor-arg>
        <value>10</value>
</constructor-arg>

이런식으로 가능핟. value값은 String타입으로 처리된다. 따라서 숫자 3000의 value를 전달해도 파라미터가 String타입의 파라미터가 정의되어있으면 매핑되고,, 만약 스트링이 없고 int타입이 있으면 int타입으로 매핑된다.

프로퍼티

프로퍼티는 익숙하지... setXXXXX()형태의 메소드를 매핑한다. 앞의 set을 빼고 XXXXX가 이름이 된다.
이렇게 생각하자... 
bean은 객체
property는 (set)메소드.. 


<bean name = "monitor" class = "kame.spring.chap02.SystemMonitor" >
    <property name = "periodTime" value = "10" />
    <property name = "sender" value = "smsSender" />
</bean>

setPeriodTime메소드에 파라미터값으로 10을 전달, setSender메소드에 smsSender값을 전달한다. 쉽지 않은가..ㅋ
이외에도 컬렉션타입(set,map,list,제네릭)으로도 의존관계 설정이 가능하니 책을 참조하자.

의존관계 자동 설정

byName : 프로퍼티의 이름과 같은 이름을 갖는 빈 객체를 설정한다.
byType : 프로퍼티의 타입과 같은 타입을 갖는 빈 객체를 설정한다.
constructor : 생성자 파라미터 타입과 같은 타입을 갖는 빈 객체를 생성자에 전달한다.
autodetect : 생성자방식을 먼저 적용하고 byType방식을 적용하여 의존객체 설정한다.


<bean id = "monitor" class = " ....SystemmMonitor" autowire = " byName" />

1)프로퍼티 이름을 이용한 의존 관계 자동 설정
<bean name = "writeArticleService"
         class = "kame.spring.chap02.WriteArticleServiceImpl" 
autowire = "byName" />
<bean name = "articleDao" class = "kame.spring.chap02.MysqlArticlDao" >
<bean name = "myArticleDao" class = "kame.spring.chap02.MysqlArticlDao" >

WriteArticleServiceImpl클래스가 이름이 "articlDao"인 프로퍼티를 갖고 있다면, 이름이 "articleDao"인 빈 객체가 프로퍼티의 값으로 전달된다.

2)타입을 이용한 의존 관계 자동 설정
<bean name = "writeArticleService"
         class = "kame.spring.chap02.WriteArticleServiceImpl"
autowire = "byType" >
<bean name = "myArticleDao" class = "kame.spring.chap02.MysqlArticlDao" >
<bean name = "oracleArticleDao" class = "kame.spring.chap02.OracleArticlDao" >

위코드에서 myArticleDao빈 객체와 oracleArticleDao 빈 객체는 둘 다 ArticleDao 타입이기 때문에 어떤걸 프로퍼티에 넣어야 할 지 결정할 수가 없다. 이럴때 예외를 발생시킨다.

AND