더북(TheBook)

이제 좀 더 나아 보인다. 데이터를 추출하는 방법을 분리하고 있는데, 어떻게 분리하고 있을까? 기억해보면 정보를 추출하기 위해 어떤 종류의 storage 메서드를 사용하고 있었다. 그렇지 않은가(예제 3-2 참고)? 그 다음, 데이터를 얻는 새로운 방법을 추가하여 SearchEngine 인터페이스를 다시 구현하기로 결정했다. 그 결과로 구현체에 무관하게 주입될 인터페이스를 생성하여 클래스를 더 견고하고 유지보수하기 쉽게 만들었다. 이번에는 스프링 컨테이너가 이러한 새로운 변경 사항을 알 수 있게 XML 파일을 수정하는 방법을 살펴보자. 예제 3-6은 새로 수정한 mydocuments-context.xml 설정 파일이다. 이 설정은 새로운 DocumentDAO 구현체(DocumentRepository)와 이 구현체가 SearchEngine 구현체에 주입되는 방법에 관한 모든 정보를 담는다.

 예제 3-6 mydocuments-context.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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.xsd">

 

<bean id="engine" class="com.apress.isf.spring.service.SearchEngineService">

<property name="documentDAO" ref="documentDAO" />

</bean>

 

<bean id="documentDAO" class="com.apress.isf.spring.data.DocumentRepository">

<property name="doc1" ref="doc1" />

<property name="doc2" ref="doc2" />

<property name="doc3" ref="doc3" />

<property name="doc4" ref="doc4" />

</bean>

 

<bean id="doc1" class="com.apress.isf.java.model.Document">

<property name="name" value="Book Template" />

<property name="type" ref="pdfType" />

<property name="location"

value="/Users/felipeg/Documents/Random/Book Template.pdf" />

</bean>

 

<bean id="doc2" class="com.apress.isf.java.model.Document">

<property name="name" value="Sample Contract" />

<property name="type">

<bean id="pdfType" class="com.apress.isf.java.model.Type">

<property name="name" value="PDF" />

<property name="desc" value="Portable Document Format" />

<property name="extension" value=".pdf" />

</bean>

</property>

<property name="location"

value="/Users/felipeg/Documents/Contracts/Sample Contract.pdf" />

</bean>

 

<bean id="doc3" class="com.apress.isf.java.model.Document">

<property name="name" value="Clustering with RabbitMQ" />

<property name="type" ref="noteType" />

<property name="location"

value="/Users/felipeg/Documents/Random/Clustering with RabbitMQ.txt" />

</bean>

 

<bean id="doc4" class="com.apress.isf.java.model.Document">

<property name="name" value="Pro Spring Security Book" />

<property name="type" ref="webType" />

<property name="location" value="http://www.apress.com/9781430248187" />

</bean>

 

<bean id="webType" class="com.apress.isf.java.model.Type">

<property name="name" value="WEB" />

<property name="desc" value="Web Link" />

<property name="extension" value=".url" />

</bean>

 

<bean id="pdfType" class="com.apress.isf.java.model.Type">

<property name="name" value="PDF" />

<property name="desc" value="Portable Document Format" />

<property name="extension" value=".pdf" />

</bean>

 

<bean id="noteType" class="com.apress.isf.java.model.Type">

<property name="name" value="NOTE" />

<property name="desc" value="Text Notes" />

<property name="extension" value=".txt" />

</bean>

 

</beans>


예제 3-6을 분석하면, 값을 대입하기 위해 ref 속성과 같은 몇몇 레퍼런스(참조)를 사용한다는 사실을 알 수 있다. 예제에서는 ServiceSearchEngine의 새로운 선언이 있고, documentDAO 프로퍼티를 설정하며, “documentDAO”라는 id로 또 다른 빈에 대한 값을 참조하고 있다.

또한, “doc2”라는 id를 가진 빈을 살펴보자. 이 부분은 새로운 빈을 값으로 포함하는 것으로, 스프링 설정 규칙에서 이를 허용한다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.