WAS v8.5 > Develop applications > Develop SCA composites

Use Spring 2.5.5 containers in SCA applications

We can use the SCA programming model to invoke beans that follow the Java 2 Platform, Standard Edition (J2SE) programming model in a Spring 2.5.5 container. WAS v8.5 supports components implemented with Spring Framework that use <implementation.spring> in composite definitions.

Before you start the procedure in this topic, do the following:

  1. 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. WAS v8.5 does not support other levels of Spring.

    WAS v8.5 only supports the Spring Framework using the J2SE programming model. Local JNDI lookups are not supported, thus the product does not support the Spring Framework using the Java EE 5 programming model. Any attempts by a Spring container to perform a local JNDI lookup fail.

  2. Decide whether to use implementation.spring or implementation.jee for the 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, we 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 we can use an EAR containing 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.

  3. 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.

    We can use globally defined JNDI resources, such as databases and connection factories, in a Spring bean definition with implementation.spring. However, 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 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, we can prevent java:comp/ext from being prepended to the JNDI name during lookup.

We 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.

  1. 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 containing the Spring application context files.

    There are two ways that we 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.

  2. 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.

    We can override this default mapping 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>

  3. If you completed step 2, to use the SCA XML extensions 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.

  4. Package the SCA application context file in your service 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.

  5. Create a Spring runtime JAR file containing Spring and product SCA runtime files.

    Because the Spring runtime binary files are not shipped with the product, create an asset containing three Spring framework JAR files and one product JAR file. Create the asset as follows:

    1. If we 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 we download the compressed .zip file, extract it to a temporary directory.

    2. Create an empty directory; for example, C:\SpringAsset.
    3. 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.

    4. 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.

      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.

    5. Create a JAR file named SpringSharedLibAsset.jar containing the four JAR files you placed in the C:\SpringAsset directory.

      For example, at a command prompt, enter the following commands:

      1. cd C:\SpringAsset
      2. jar –cvf SpringSharedLibAsset.jar *.jar

      For performances reasons, the JAR file must be named SpringSharedLibAsset.jar.

    6. Verify the SpringSharedLibAsset.jar file contains all four JAR files in the root directory.

  6. 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')

    We can also use the dmgr console.

  7. 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. We 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, we can use the dmgr console to import the asset with a dependency:

    1. On the dmgr console, click Applications > Application Types > Assets.

    2. On the Assets page, click Import.

    3. On the Upload asset page, specify the full path name of the service JAR file and click Next.

    4. On the Select options for importing an asset page, specify a dependency.

      1. Click Manage Relationships. The button is near the bottom of the page. WAS v8.5 detects asset relationships automatically by matching the dependencies defined in the JAR manifest with the assets that are already imported into the administrative domain.

      2. Select your Spring runtime JAR file, such as SpringSharedLibAsset.jar, click >> to move the JAR file to the Selected list.

      3. Click OK.

        Under Current asset relationships, the asset name is shown; for example:

          WebSphere:assetname=SpringSharedLibAsset.jar

      4. Click Next.

    5. Click Finish.
    6. 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.

Add the JAR assets created to an SCA business-level application.

To learn about Spring implementation features the product supports but which are not defined in the SCA Spring Implementation specification, see "Additional Spring component implementation features."


Subtopics


Related concepts:

SCA transaction intents
Importing assets


Related


Use existing Java EE modules and components as SCA implementations
Create SCA business-level applications


Reference:

Asset page
Additional Spring component implementation features


Related information:

Spring source community
Spring Community Downloads
SCA Spring Component Implementation 1.0.0


+

Search Tips   |   Advanced Search