You can use the Service Component Architecture (SCA) programming
model to invoke beans that follow the Java 2
Platform, Standard Edition (J2SE) programming model in a Spring 2.5.5
container. The product supports components implemented with Spring
Framework that use <implementation.spring> in
composite definitions.
Before you begin
Before you start the procedure in this topic, do the following:
- Become familiar with the SCA Spring Component Implementation 1.0.0
specification and Spring 2.5.5 programming. You must use Spring 2.5.5
for the SCA composite component implementation. The product does not
support other levels of Spring.
The product only supports the Spring
Framework using the J2SE programming model. Local Java Naming and
Directory Interface (JNDI) lookups are not supported, thus the product
does not support the Spring Framework using the Java Platform, Enterprise
Edition (Java EE) 5 programming model. Any attempts by a Spring container
to perform a local JNDI lookup fail.
- Decide whether to use implementation.spring or implementation.jee for
your Spring application.
The procedure in this topic discusses using implementation.spring in
an SCA component implementation. If a Spring application is, in fact,
a specialized Java EE application, instead of using implementation.spring,
you can use implementation.jee to define the SCA
component implementation and enable the Spring application to be part
of an SCA component.
For example, if a Spring application is
packaged in a web module (WAR), then you can use an enterprise archive
(EAR) that contains the WAR file as a component implementation using implementation.jee.
The Spring application works like before and the Java EE context is
not affected. Spring beans are not available in an SCA domain; however,
other modules that are compatible with implementation.jee within
the EAR file can participate in SCA.
For information on deploying
a Spring application with implementation.jee, see
the topic on using existing Java EE modules and components as SCA
implementations.
- If you decide to use implementation.spring and
you want beans in your Spring application to perform JNDI lookups,
set com.ibm.websphere.naming.WsnInitialContextFactory as
a property for the beans.
You can use globally defined JNDI resources,
such as databases and connection factories, in a Spring bean definition
with implementation.spring. However, you must set
a Spring JNDI template bean that has been configured to reference
the WebSphere initial context factory, com.ibm.websphere.naming.WsnInitialContextFactory,
as a property for beans performing JNDI lookups. For example, the
following bean definitions specify that the myJndiResource bean
use the WebSphere initial context factory:
<bean id="myJndiResource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myJndiResource"/>
<property name="lookupOnStartup" value="true"/>
<property name="cache" value="true"/>
<property name="jndiTemplate" ref="wasJndiTemplate"/>
</bean>
<bean id="wasJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
</props>
</property>
</bean>
The SCA container for implementation.spring does
not run in a Java EE container, so it does not have access to the
Java EE local JNDI namespace. By specifying that your Spring beans
use the WebSphere initial context factory, you can prevent java:comp/ext from
being prepended to the JNDI name during lookup.
About this task
You can use a Spring application context as an implementation
within an SCA composite component. Define the implementation using
Spring 2.5.5. A Spring application context provides a complete composite,
exposing services and using references using SCA.
A component
that uses Spring for an implementation can wire SCA services and references
without introducing SCA metadata into the Spring configuration. Generally,
the Spring context knows little about the SCA environment.
Procedure
- Define a component implementation that uses the Spring
Framework in a composite definition.
The Spring component
implementation in a composite definition has the following format:
<implementation.spring location="targetURI"/>
The location attribute
of the element specifies the target uniform resource indicator (URI)
of a directory, or the fully qualified path that contains the Spring
application context files.
There are two ways that you can specify
the target URI in the location attribute:
- Specify a fully qualified path:
<implementation.spring location="./spring/application-context.xml"/>
- Specify a directory:
<implementation.spring location="./spring"/>
The
target URI specifies the resource as a directory, here named spring,
that has all the Spring-related files. To point to one application
context, a META-INF/MANIFEST.MF file in that
directory must contain a Spring-Context header of
the format Spring-Context: path,
where path is a relative path with respect to the
location URI. For example:
Spring-Context: META-INF/spring/application-context.xml
If
the META-INF/MANIFEST.MF file does not exist
or does not contain the Spring-Context header, then
the product builds an application context using the application-context.xml file
in the META-INF/spring directory. If the META-INF/spring/application-context.xml file
does not exist, then the application does not deploy.
- Optional: Override implicit mapping
of Spring resource to SCA resources.
By default, SCA
implicitly maps Spring resources to SCA resources as follows:
- Each <bean/> becomes <sca:service/>.
- Each unresolved <property/> becomes <sca:reference/> if
typed by an interface.
- Each unresolved <property/> becomes <sca:property/> if
not typed by an interface.
You can override this default mapping by using the SCA XML
extensions in the Spring context to create explicit declarations.
You typically do this to enable the Spring container to decorate the
bean using, for example, Spring Aspect-Oriented Programming (AOP).
The SCA XML extensions are defined as follows:
- <sca:service/> defines each service exposed
by the context.
- <sca:reference/> defines each reference
exposed by the context.
- <sca:property/> defines each property exposed
by the context.<sca:property/> must be a bean
class and not a primitive type.
For example, the properties checkingAccountService,
calculatorService, and stockQuoteService defined in the following
Spring configuration are declared explicitly as SCA beans in a Spring
application context using the <sca:reference> element:
<beans>
<bean id="AccountServiceBean" class="bigbank.account.AccountServiceImpl">
<property name="calculatorService" ref="calculatorService"/>
<property name="stockQuoteService" ref="stockQuoteService"/>
<property name="checkingAccountService" ref="checkingAccountService"/>
<!-- Some implicit references and properties follow. A property with a reference
not satisifed * within the Spring application context. -->
<property name="savingsAccountService" ref="savingsAccountService"/>
<property name="stockAccountService" ref="stockAccountService"/>
<property name="currency" value="EURO"/>
</bean>
<sca:reference name="checkingAccountService" type="bigbank.account.checking.CheckingAccountService"/>
<ca:reference name="calculatorService" type="bigbank.calculator.CalculatorService"/>
<sca:reference name="stockQuoteService" type="bigbank.stockquote.StockQuoteService"/>
</beans>
- If you completed step
2, to use the SCA XML extensions you must add the SCA schema
to the application context.
Add the SCA schema, http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd ,
to the application context. Specify a Spring application context that
defines the SCA schema namespace and makes the Spring application
aware of the SCA-related beans; for example:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sca="http://www.springframework.org/schema/sca"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/sca
http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd">
<sca:service name="StockQuoteService"
type="bigbank.stockquote.StockQuoteService" target="StockQuoteServiceBean"/>
<bean id="StockQuoteServiceBean" class="bigbank.stockquote.StockQuoteImpl">
</bean>
</beans>
For more information
about defining an application context, see the reference documentation
provided by the Spring source community. For more information about
the SCA schema, see the SCA Spring Implementation specification.
- Package the SCA application context file in your service
Java archive (JAR) file at the location specified in your composite
definition.
For example, package the SCA application
context file in a Spring service JAR file is named helloworld-spring.jar.
- Create a Spring runtime JAR file that contains Spring and
product SCA runtime files.
Because the Spring runtime
binary files are not shipped with the product, you must create an
asset that contains three Spring framework JAR files and one product
JAR file. Create the asset as follows:
- If you do not have the Spring 2.5.5 framework binary
files, go to Spring Community Downloads at http://www.springsource.com/download/community?project= and
download spring-framework-2.5.5.zip.
After
you download the compressed .zip file, extract
it to a temporary directory.
- Create an empty directory; for example, C:\SpringAsset.
- Copy the Spring 2.5.5 framework binary files spring-beans.jar, spring-context.jar,
and spring-core.jar to the empty directory.
If the Spring application requires transaction support, copy
all the Spring JAR files into the empty directory. The AspectJ Weaver
library files are also required for transaction support.
- Copy the app_server_root/optionalLibraries/sca/SCA-implementation-spring-runtime-1.0.1.jar file
to the same directory in which you placed the three Spring JAR files.
Avoid trouble: If you install a product maintenance
package, update the
SpringSharedLibAsset.jar with
the newer version of the
app_server_root/optionalLibraries/sca/SCA-implementation-spring-runtime-1.0.1.jar file.
gotcha
- Create a JAR file named SpringSharedLibAsset.jar that
contains the four JAR files you placed in the C:\SpringAsset directory.
For example, at a command prompt, enter the following commands:
- cd C:\SpringAsset
- jar cvf SpringSharedLibAsset.jar *.jar
For performances reasons, the JAR file must be named SpringSharedLibAsset.jar.
- Verify that the SpringSharedLibAsset.jar file
contains all four JAR files in the root directory.
- Import the Spring runtime JAR file as an asset.
To
import SpringSharedLibAsset.jar as an asset using
a wsadmin Jython command, enter the following command:
AdminTask.importAsset('-source SpringSharedLibAsset.jar -storageType FULL')
You
can also use the administrative console.
- Import the Spring service JAR file as an asset with a dependency
on the Spring runtime asset.
Suppose that your Spring
service JAR file is named helloworld-spring.jar.
Import your Spring service JAR file, helloworld-spring.jar,
and create a dependency on the Spring runtime asset, SpringSharedLibAsset.jar.
The dependency enables the product to access the necessary Spring
classes. You can import the Spring service JAR file using the following
wsadmin Jython command:
AdminTask.importAsset('-source helloworld-spring.jar -storageType FULL -AssetOptions
[[ helloworld-spring.jar helloworld-spring.jar "" "" "" spec=zip assetname=SpringSharedLibAsset.jar
"" false]]]')
Alternatively, you can
use the administrative console to import the asset with a dependency:
- On the administrative console, click .
- On the Assets page, click Import.
- On the Upload asset page, specify the full path name
of the service JAR file and click Next.
- On the Select options for importing an asset page, specify
a dependency.
- Click Manage Relationships. The button
is near the bottom of the page. The product detects asset relationships
automatically by matching the dependencies defined in the JAR manifest
with the assets that are already imported into the administrative
domain.
- Select your Spring runtime JAR file, such as SpringSharedLibAsset.jar,
click >> to move the JAR file to the Selected list.
- Click OK.
Under Current asset
relationships, the asset name is shown; for example:
WebSphere:assetname=SpringSharedLibAsset.jar
- Click Next.
- Click Finish.
- After the product imports the asset, click Save.
Results
The Spring runtime JAR file and Spring service JAR file
are imported assets available for use in a business-level application.
The Spring service JAR asset has a dependency on the Spring runtime
asset.
What to do next
Add the JAR assets that you created to an SCA business-level
application.
To learn about Spring implementation features that
the product supports but which are not defined in the SCA Spring Implementation
specification, see "Additional Spring component implementation features."