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.
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.
- 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
Feature Pack for 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 Feature
Pack for SCA 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 .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/feature_packs/sca/optionalLibraries/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 Feature Pack for SCA
maintenance package, update the
SpringSharedLibAsset.jar with
the newer version of the
app_server_root/feature_packs/sca/optionalLibraries/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.
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."