ทุกวันนี้ enterprise application server กลายเป็นของที่หาใช้ได้ทั่วไปตามท้องตลาด ดูเหมือนจะไม่ค่อยมีนวัตกรรมใหม่ๆ ทั้งในตัวเซิร์ฟเวอร์เองและใน component container ที่รันอยู่ภายใน เราสามารถใช้ Enterprise JavaBeans (EJB) เป็น Session Facades [Crupi et al.] หรือใช้ JavaBeans ทั่วไปที่ถูกโฮสต์โดย inversion-of-control container เช่น Spring เพื่อทำให้การใช้ Application Services สะดวกขึ้น มีการถกเถียงกันว่าวิธีไหนดีกว่ากัน แต่ framework ต่างๆ ก็ได้หลอมรวมเข้าหากันเป็นอย่างมากแล้ว ที่จริงแล้ว ถ้าเราแอบดูใต้ฝาเซิร์ฟเวอร์ JEE บางตัว ก็จะพบว่าบางตัวถูก implement ด้วย Spring
Is It WebLogic or Spring? (นี่คือ WebLogic หรือ Spring?)
ถ้าคุณลองเปิดดู stack trace จาก Oracle WebLogic Server คุณก็มักจะเห็นการอ้างอิงถึง class ต่างๆ จาก Spring Framework ซึ่งไม่ได้เป็นส่วนหนึ่งของดีพลอยเมนต์ของแอปพลิเคชันของคุณ ในกรณีนี้คุณใช้แค่ JEE มาตรฐาน กับ EJB Session Beans ส่วน class Spring ที่คุณเห็นเป็นส่วนหนึ่งของ implementation ของ EJB container ใน WebLogic นี่เป็นกรณีของคำกล่าวที่ว่า “if you can’t beat them, join them” (ถ้าสู้เขาไม่ได้ก็ต้องเข้าร่วมกับเขา) หรือเปล่า?
ผมเลือกที่จะ implement Bounded Context ตัวอย่างทั้งสามที่ให้ไว้ในหนังสือเล่มนี้ด้วย Spring Framework อย่างไรก็ตาม ตัวอย่างเหล่านี้ย้ายไปใช้กับแพลตฟอร์ม enterprise container อื่นๆ ได้โดยง่าย ดังนั้นถ้าโปรเจกต์ ของคุณไม่ได้ใช้ Spring ก็ไม่เสียหายอะไร และคุณควรจะยังรู้สึกสบายใจที่จะอ่านตัวอย่างเหล่านี้ต่อไปเรื่อยๆ เพราะความแตกต่างเชิงตรรกะระหว่างคอนเทนเนอร์ต่างๆ มีน้อยมาก
ในบท Repositories (12) เราได้เห็น Spring configuration ที่ใช้ต่อสาย (wire) transactional support ให้กับ Application Services ซึ่งใช้ในการ persist domain object ต่อไปเรามาดูส่วนอื่นๆ ของ Spring configuration กันบ้าง ไฟล์สองไฟล์ที่น่าสนใจคือ
config/spring/applicationContext-application.xml
config/spring/applicationContext-domain.xml
ตามที่ชื่อไฟล์บอกไว้ ไฟล์เหล่านี้ใช้สำหรับต่อสาย (wire) Application Services และคอมโพเนนต์ของ domain model เข้าด้วยกัน ลองดูตัวอย่างบางส่วนจากการต่อสายฝั่งแอปพลิเคชันกัน:
<beans ...>
<aop:aspectj-autoproxy/>
<tx:annotation-driven transaction-manager="transactionManager"/>
...
<bean
id="applicationServiceRegistry"
class="com.saasovation.identityaccess.application ↵
.ApplicationServiceRegistry"
autowire="byName">
</bean>
...
<bean
id="tenantIdentityService"
class="com.saasovation.identityaccess.application ↵
.TenantIdentityService"
autowire="byName">
</bean>
...
</beans>
Bean tenantIdentityService คือ bean ที่เราได้พูดถึงไปก่อนหน้านี้ bean ตัวนี้สามารถต่อสาย (wire) เข้าไปใน Spring bean ตัวอื่นๆ ได้ เช่นใน user interface ถ้าคุณชอบ Service Factory มากกว่าการ inject instance ของ bean เข้าไปใน bean อื่น เราก็สามารถใช้ bean อีกตัวหนึ่งในการคอนฟิกนี้ได้ นั่นคือ applicationServiceRegistry ซึ่งให้การเข้าถึง Application Services ทั้งหมดผ่าน lookup คุณจะใช้มันแบบนี้:
...
ApplicationServiceRegistry
.tenantIdentityService()
.deactivateTenant(tenantId);
เราสามารถทำเช่นนั้นได้ เพราะตัวมันเองถูก inject ด้วย Spring Application-Context เมื่อ bean ถูกสร้างขึ้นมาใหม่
bean แบบ registry ในลักษณะเดียวกันนี้ยังถูกจัดเตรียมไว้สำหรับเข้าถึงคอมโพเนนต์ของ domain model เช่น Repositories และ Domain Services ต่อไปนี้คือคอนฟิกของ bean Registry, Repository และ Domain Service สำหรับ domain model:
<beans ...>
...
<bean
id="authenticationService"
class="com.saasovation.identityaccess.infrastructure ↵
.services.DefaultEncryptionAuthenticationService"
autowire="byName">
</bean>
<bean
id="domainRegistry"
class="com.saasovation.identityaccess.domain.model ↵
.DomainRegistry"
autowire="byName">
</bean>
<bean
id="encryptionService"
class="com.saasovation.identityaccess.infrastructure ↵
.services.MessageDigestEncryptionService"
autowire="byName">
</bean>
<bean
id="groupRepository"
class="com.saasovation.identityaccess.infrastructure ↵
.persistence.HibernateGroupRepository"
autowire="byName">
</bean>
<bean
id="roleRepository"
class="com.saasovation.identityaccess.infrastructure ↵
.persistence.HibernateRoleRepository"
autowire="byName">
</bean>
<bean
id="tenantProvisioningService"
class="com.saasovation.identityaccess.domain.model ↵
.identity.TenantProvisioningService"
autowire="byName">
</bean>
<bean
id="tenantRepository"
class="com.saasovation.identityaccess.infrastructure ↵
.persistence.HibernateTenantRepository"
autowire="byName">
</bean>
<bean
id="userRepository"
class="com.saasovation.identityaccess.infrastructure ↵
.persistence.HibernateUserRepository"
autowire="byName">
</bean>
</beans>
เมื่อใช้ DomainRegistry เราสามารถเข้าถึง bean ใดๆ ก็ได้ที่ลงทะเบียนกับ Spring ไว้ bean ทั้งหมดยังพร้อมสำหรับการ dependency injection เข้าไปใน Spring bean ตัวอื่นๆ ด้วย ดังนั้น Application Services จึงเลือกได้ว่าจะใช้ Service Factory หรือ Dependency Injection สำหรับการพูดคุยเชิงลึกเกี่ยวกับการใช้สองแนวทางนี้เทียบกับการตั้งค่า dependency แบบ constructor-based โปรดดูบท Services (7)