In this post I will gradually develop a sample application build with Spring framework, BlazeDS integration and other SpringSource technologies. As an example it is linked with the previous post Architecting a Flex 4 component, building the backend services for the contact form.
To get started we need the Spring dm server and Spring Source Tools Suite. The Tool Suite includes the Spring IDE and dm Server Tools which will help you configure the dm server instances.The dm Server is a new generation of Java EE application servers. It brings together traditional Java EE and OSGi worlds.
1. Create the domain bundle:
I will create a new dm server Bundle Project called contactForm-domain. This project will contain our domain classes.
In the MANIFEST.MF file expose the com.ayone.examples.contactForm.domain package.
MANIFEST.MF file:
1 2 3 4 5 6 7 | Manifest-Version: 1.0 Bundle-Name: ContactForm Domain Bundle Bundle-ManifestVersion: 2 Bundle-Description: AYONE Bundle-SymbolicName: com.ayone.examples.contactForm.domain Bundle-Version: 1.0.0 Export-Package: com.ayone.examples.contactForm.domain;version="1.0.0" |
The Contact class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.ayone.examples.contactForm.domain; import java.io.Serializable; public class Contact implements Serializable { private Long id; private String email; private String message; public Contact() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
2. Create the Repository Bundle
This bundle contains the Hibernate DAO classes. It will be presented in details in another post.
3. Create the Service Bundle
We now create another bundle project with our service classes called contactForm-service.
We create a spring service with annotation @Service(”contactFormService”) register this in the service-context.xml. This file is the application context where we scan for our annotated spring beans.
The service bean must also be exposed in the OSGi context so we also have an osgi-context.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.ayone.examples.contactForm.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ayone.examples.contactForm.domain.Contact; import com.ayone.examples.contactForm.repository.ContactRepository; import com.ayone.examples.contactForm.service.ContactFormService; @Service("contactFormService") public class ContactFormServiceImpl implements ContactFormService { @Autowired private ContactRepository contactRepository; public void addContact(Contact contact) { contactRepository.save(contact); } } |
service-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.ayone.contactForm.service.impl" />
<context:annotation-config />
</beans>
osgi-context.xml
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<service ref="contactFormService"
interface="com.ayone.examples.contactForm.service.ContactFormService" />
<reference id="contactRepository"
interface="com.ayone.examples.contactForm.repository.ContactRepository" />
</beans:beans>
MANIFEST.MF
1 2 3 4 5 6 7 8 9 10 | Manifest-Version: 1.0 Bundle-Name: ContactForm Service Bundle Bundle-ManifestVersion: 2 Bundle-Description: AYONE Bundle-SymbolicName: com.ayone.examples.contactForm.service Bundle-Version: 1.0.0 Import-Package: com.ayone.examples.contactForm.domain;version="[1.0.0,1.0.0]", com.ayone.examples.contactForm.repository;version="[1.0.0,1.0.0]" Import-Library: org.springframework.spring;version="[2.5.6.A,2.5.6.A]" Export-Package: com.ayone.examples.contactForm.service;version="1.0.0" |
4. Create the Web Module:
Next we create our the Web Module called contactForm-web which is also a spring dm server bundle project.In addition
Web Modules benefit from reduced configuration via OSGi manifest headers such as Web-DispatcherServletUrlPatterns, Web-ContextPath and Web-FilterMappings.Based on web module metadata the dm Server will auto-generate the web.xml.
In this project we have the Spring BlazeDS Integration 1.0 configuration.
The contactFormService spring bean is exposed for remoting to the Flex client.
Similar to a standard WAR, MODULE-INF is also the directory in which you should place WEB-INF and related subdirectories like BlazeDS channel definitions. The basic setup for the BlazeDS MessageBroker is in the web-context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex" 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/flex
http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<!– Bootstraps and exposes the BlazeDS MessageBroker –>
<flex:message-broker>
<flex:message-service default-channels="my-streaming-amf,my-polling-amf" />
</flex:message-broker>
<!– Maps request paths at /* to the BlazeDS MessageBroker –>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/*=_messageBroker
</value>
</property>
</bean>
<!– Dispatches requests mapped to a MessageBroker –>
<bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter" />
<!– Expose the taskioService bean for BlazeDS remoting –>
<flex:remoting-destination destination-id="contactFormService"
ref="contactFormService" />
</beans>
osgi-context.xml
<beans:beans xmlns="http://www.springframework.org/schema/osgi"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<reference id="contactFormService"
interface="com.ayone.examples.contactForm.service.ContactFormService" />
</beans:beans>
MANIFEST.MF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Manifest-Version: 1.0 Bundle-Name: Web Web-DispatcherServletUrlPatterns: /messagebroker/* Bundle-Description: AYONE Web-ContextPath: contactForm Bundle-ManifestVersion: 2 Bundle-SymbolicName: com.ayone.contactForm.web Module-Type: Web Bundle-Version: 1.0.0 Import-Library: org.springframework.spring;version="[2.5.6.A,2.5.6.A]" Import-Bundle: org.springframework.flex;version="[1.0.0.RELEASE,1.0.0.RELEASE]", com.springsource.flex.messaging;version="[3.2.0.3978,3.2.0.3978]", com.springsource.flex.messaging.common;version="[3.2.0.3978,3.2.0.3978]", com.springsource.flex.messaging.services.http;version="[3.2.0.3978,3.2.0.3978]", com.springsource.flex.messaging.services.remoting;version="[3.2.0.3978,3.2.0.3978]", com.springsource.net.sf.cglib;version="[2.1.3,2.1.3]" Import-Package: com.ayone.examples.contactForm.service;version="[1.0.0,1.0.0]", javax.servlet;version="[2.5.0,2.5.0]", javax.servlet.http;version="[2.5.0,2.5.0]" |
5.Deploy the bundles
Add the projects to the Spring DM server. In order to deployed and run the sample, dependencies must be added to the server provisioning repository ($SERVER_HOME/repository/bundles/usr) or you can use the Repository Browser to install bundles from the SpringSource Enterprise Bundle Repository.
You can now call remote services from http://localhost:8080/contactForm/messagebroker/amf.
You can find more about Spring BlazeDS Integration at: http://www.springsource.org/spring-flex