Exploring the WebSphere Application Server Feature Pack for SCA, Part 6: Using Spring with Service Component Architecture

The Spring Framework open source project provides a framework for simple Java™ objects that enables these objects to use the Java EE container through wrapper classes and XML configuration. Part 6 of this series highlights how the IBM® WebSphere® Application Server V7.0 Feature Pack for Service Component Architecture (SCA) provides support for components implemented with Spring version 2.5.5. Part 1 of this series provides an overview of SCA bindings within the SCA assembly model.

Share:

Chao M Beck, Software Engineer, IBM

Chao Beck is a technical lead for the feature pack for Service Component Architecture (SCA) early program. She has long been a member of the Application Integration Middleware early programs team responsible for the execution of early programs for IBM WebSphere Application Server products. She handles the development and delivery of education for new product functions and the provision of customer support during pre-GA (early) programs and post-GA (customer acceleration) programs.



Jennifer A Thompson (jennthom@us.ibm.com), Software Engineer, IBM  

Jennifer A. Thompson joined IBM in 2005 and since that time she has worked on various development and test teams including WebSphere Application Server for z/OS, feature packs such as Web services and service component architecture. Her contributions in SCA feature include delivery of JMS binding, Spring, and interoperability support.



Anbumunee Ponniah (aponnia@us.ibm.com), Software Engineer, IBM

Anbumunee Ponniah is a developer/tester involved in implementing JavaEE support in SCA Feature pack. Anbu has a rich background covering many technical areas ranging from Unix internals to application programming. He has previously been a technical lead for Java class libraries in IBM's Java technology center.



Vijai Kalathur (kalathur@us.ibm.com), Software Engineer, IBM  

Vijai Kalathur is part of the SCA Feature Pack QoS team. Since joining IBM in 2005 he has worked on the WebSphere Application Server for z/OS security team and the SCA Feature Pack team. He has worked on various components of the SCA Feature Pack including admin, security, transactions, and JMS binding.



Hasan Muhammad (asunder@us.ibm.com), Software Engineer, IBM  

Hasan Muhammad is the technical lead for the SCA Feature Pack Admin component. He has worked on WebSphere Application Server product for a number of years. Prior to working on the SCA Feature Pack, he had worked on a number of development teams, including install and configuration, EJB Persistence Manager, and Service Data Objects.



20 January 2010

Also available in Chinese

Introduction

The Spring Framework is an open source project used for constructing Java applications that aims to reduce the complexity of the programming environment. Spring shares many of the same design principles as Service Component Architecture (SCA); in particular, it provides a run time container that provides dependency injection so that application components can avoid the need to program directly to middleware APIs. This is also one of the key principles of SCA.

SCA and Spring are not alternatives to each other, but rather SCA is a complimentary technology to Spring. This relationship enables local services (fine grained) created in Spring to be exposed for composition with other programming models, such as EJB. For Spring users, this means that SCA is a good choice for helping you compose heterogenous programming models and building SOA applications; that is, exposing business logic as services and composing applications from distributed services. Spring also utilizes an application context written in XML to instantiate, configure, and assemble the objects in your application.

Support for the Spring Framework in SCA is provided at a coarse-grained level. It is possible to use an existing Spring application context as a component implementation in SCA. An SCA runtime that supports Spring integration can use an application context as-is in an SCA assembly. For such a component, it is possible to wire Spring services and references without the need to introduce SCA metadata into the Spring configuration. The integration with Spring is at the SCA composite level, where a Spring application context provides a complete composite implementation, exposing services and wiring references through SCA. This means that a Spring application context defines the internal structure of a composite implementation. SCA enables you to either implicitly or explicitly define Spring resources as services, references, and properties. It does so by using SCA XML extensions.

Restrictions
You must use Spring 2.5.5 for the SCA composite component implementation. Other levels of Spring are not supported. In addition, only Spring applications that follow the J2SE (Java 2 Platform, Standard Edition) model are supported. Java EE local namespace is not available, so any attempts to access resources using java:comp in the application container will fail.

In terms of direct use of SCA references, the SCA runtime that hosts the Spring application context implementing a composite creates a parent application context; in this parent context, all SCA references are defined as beans using the SCA reference name as the bean name. These beans are automatically visible in the child (user application) context.

Be aware that WebSphere Application Server does not provide service for the Spring framework, and does not ship with Spring.


Spring component implementation

The Spring component implementation SCDL (Service Component Definition Language) has this format:

<implementation.spring location="targetURI" />

where the location attribute of that element specifies either the target URI of a directory, or the fully qualified path that contains the Spring application context files. There are two supported ways to specify the target URI:

  • Specify fully qualified path:

    <implementation.spring location="./spring/application-context.xml" />

  • Specify a directory:

    <implementation.spring location="./spring" />

In the second case, the target URI specifies the resource as a directory. In the example here, it is named "spring." This directory contains 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 context header is not defined, the default behavior is to build 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.


Spring and Java EE

If a Spring application is a specialized Java EE application that accesses Java EE resources through local namespace, and if the intention is not to expose Spring beans as SCA services, implementation.jee can be used (instead of using implementation.spring) to define the SCA component implementation and enable such a Spring application to be part of an SCA component.

For example, if a Spring application is packaged in a Web module (WAR), you can use an enterprise archive (EAR) file that contains the WAR file as an SCA component implementation using implementation.jee. The Spring application would continue to operate as a normal Spring application deployed as a Java EE application, and the Java EE context is not affected. Spring beans are not available in an SCA domain, but other modules that are compatible with implementation.jee within the EAR file can participate in SCA.


Spring component implementation features beyond SCA specification

The Feature Pack for SCA supports these features:

  • Constructor injection

    This is the injection of SCA references and properties within Spring bean constructors. Define <constructor-arg> elements that specify the appropriate type of the SCA references or properties to use. If the elements do not specify the type attribute, then at least specify the index attribute. The feature pack for SCA currently supports only constructor injection when the Spring bean has a single constructor.

  • Loading multiple application context files

    Spring permits the loading of multiple application context files using the ClassPathXmlApplicationContext bean definition. In the case of the SCA feature pack, when the ClassPathXMLApplicationContext bean definition is in an application context, only the beans and properties defined in the top-level application context can be mapped (either explicitly or implicitly) to SCA resources. Use a list value that points to an application context XML file in the <constructor-arg> element. Figure 1 shows a list value that points to an application context XML file in the <constructor-arg> element.

    Figure 1. Load multiple application context files
    Figure 1. Load multiple application context files
  • <import> elements in application context files

    The feature pack supports using <import> elements in application context files, with each <import> element pointing to an application context XML file (Figure 2).

    Figure 2. <import> elements in application context files
    Figure 2. <import> elements in application context files

Making Spring visible to an SCA service

To make Spring visible to an SCA service from an administrative standpoint, you first need to import SpringSharedLibAsset.jar as an asset using the Asset menu on the WebSphere Application Server administrative console. This is a JAR file that you would create, made up of these files:

  • From WebSphere Application Server V7.0: SCA-implementation-spring-runtime-1.0.1.jar
  • From Spring 2.5.5: spring-beans.jar, spring-context.jar, spring-core.jar.

During the JAR import, select Manage Relationships... > Current asset relationships to create a dependency on SpringSharedLibAsset.jar. Later, when you add the JAR file to a business level application, the administrative code will automatically pull in a copy of the SpringSharedLibAsset.jar file into the application. The next section provides further examples.


Spring 2.5.5 containers in SCA applications

If you have an existing Spring application that uses Java EE functionality, such as local JNDI namespace, <implentation.jee> might be a better choice for making your application visible to SCA rather than <implementation.spring>.

So what are the steps for using Spring application context as an implementation within an SCA composite component? This section steps through the process of creating a simple Hello World application using <implementation.spring> from an existing simple Spring application that follows the J2SE programming model. This example contains one Spring bean, HelloWorldBean.java, shown in Listing 1, and one application context file, SpringHelloWorld-context.xml, in Listing 2.

Figure 3. Steps for using Spring application context
Figure 3. Steps for using Spring application context
Listing 1. Figure 4: HelloWorldBean.java
package test.spring;

public class HelloWorldBean {
    public String sayHello(String name) {
        return ("Hello " + name);
    }
}
Listing 2. Figure 5: SpringHelloWorld-context.xml
<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-2.0.xsd">

    <bean id="HelloWorld" class="test.spring.HelloWorldBean">
    </bean>

</beans>
  1. You do not need to modify an existing application context file to use implicit mapping from Spring resources to SCA resources. To explicitly map resources, add the SCA schema (see Resources) to the application context.

    For this example, add the SCA schema to the existing application context and use the <sca:service> tag to explicitly define an SCA service called “HelloWorldXMLExt,” which is implemented by the HelloWorld bean. Your application context should now match Listing 3. (If the application context was left in its original format, the service would be exposed as the bean name, “HelloWorld,” instead.)

    Listing 3. Updated SpringHelloWorld-context.xml
    <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">
    
        <bean id="HelloWorld" class="test.spring.HelloWorldBean">
        </bean>
    
         <sca:service name="HelloWorldXMLExt" type="test.spring.HelloWorldBean" 
    		target="HelloWorld"/>
    
    </beans>
  2. Define a component implementation that uses the Spring Framework in a composite definition. The Spring component implementation in a composite definition has this format:

    <implementation.spring location="targetURI"/>

    When defining reference and service names in the component implementation, the names must be consistent with the SCA resources defined (either implicitly or explicitly) in the Spring application context. Listing 4 shows the default.composite file for the current example. Notice that the service name “HelloWorldXMLExt” matches the service name defined in the application context file shown in Listing 3. Since you started with a predefined bean, you are using the implementation class as the interface.

    Listing 4. default.composite
    <?xml version="1.0" encoding="UTF-8"?>
    <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" 
    	targetNamespace="http://spring.test/bindings" 
    	xmlns:wsdli="http://www.w3.org/2004/08/wsdl-instance" 
        name="SpringHelloWorld">
    
        <component name="SpringComponent">
            <implementation.spring location="META-INF/SpringHelloWorld-context.xml"/>
            <service name="HelloWorldXMLExt">
                <interface.java interface="test.spring.HelloWorldBean"/>
                <binding.sca />
            </service>
        </component>
    </composite>
  3. Package the SCA application context file in your service Java archive (JAR) file at the location specified in your composite definition. Continuing with the example, create a JAR file called “helloworld-spring.jar” with the following contents:

    • META-INF/SpringHelloWorld-context.xml
    • META-INF/sca-deployables/default.composite
    • test/spring/HelloWorldBean.class
  4. Create a Spring run time JAR file called "SpringSharedLibAsset.jar" that contains Spring and Feature Pack for SCA run time files. You must create an asset that minimally contains three Spring framework JAR files and one Feature Pack for SCA JAR file. To create this required library file:

    1. Create a temporary directory, such as C:\temp\spring.
    2. Download spring-framework-2.5.5.zip from the Spring Community Downloads and extract it to a temporary directory.
    3. Copy spring-beans.jar, spring-context.jar, and spring-core.jar, which were extracted to {DOWNLOAD_TEMP_DIR}\spring-framework-2.5.5\dist\modules to the directory created in step 1.
    4. Copy {WAS_HOME}\feature_packs\sca\optionalLibraries\SCA-implementation-spring-runtime-XYZ.jar to the directory created in step 1. (Replace XYZ with the appropriate value; for example, SCA-implementation-spring-runtime-1.0.1.jar.)
    5. Use the Java JAR utility or equivalent tool to create the SpringSharedLibAsset.jar; for example:

      jar cvf SpringSharedLibAsset.jar C:\temp\spring\*.jar

      Make sure your library file is named “SpringSharedLibAsset.jar” for performance reasons.

    Be aware that:

    • The SCA-implementation-spring-runtime-101.jar file might be updated when refresh packs are applied to the Feature Pack for SCA. When such a change occurs, SpringSharedLibAsset.jar should be recreated so that it contains the latest version of the JAR file, and the corresponding WebSphere asset should be updated.
    • If your application will be utilizing transactions, you will need to include all the Spring JAR files included in spring-framework-2.5.5.zip as well as the AspectJ Weaver library files.
  5. Import the Spring run time JAR file as an asset using the WebSphere Application Server administrative console or wsadmin commands. To import SpringSharedLibAsset.jar using the admin console:

    1. From the admin console, navigate to Applications > Applications Types > Assets and click Import.
    2. Specify the location of the SpringSharedLibAsset.jar (for example, c:\temp\spring\SpringSharedLibAsset.jar) and import the asset.
  6. On the administrative console, import the Spring service JAR file as an asset. During or after import -- but before you add to a business level application -- create a dependency on the Spring run time asset, SpringSharedLibAsset.jar. This dependency enables WebSphere Application Server to access the necessary Spring classes by deploying a shared library from the Spring run time asset.

    Continuing the example, import helloworld-spring.jar using the admin console:

    1. Navigate to Applications > Applications Types > Assets.
    2. Locate helloworld-spring.jar and import it as an asset.
    3. During import, click on the Manage Relationships button (Figure 4) to create the asset relationship.
      Figure 4. Import an asset
      Figure 4. Import an asset
    4. In Figure 5, select SpringSharedAssetLib.jar on the left side of the panel, then click the right arrow to move the JAR file to the right side of the panel, then click OK. The relationship should be created as shown in Figure 6.
      Figure 5. Current asset relationships
      Figure 5. Current asset relationships
      Figure 6. Import in repository
      Figure 6. Import in repository
    5. Click Next to finish importing the asset.
  7. Add the asset to a business level application using <implementation.spring>. Once the selected asset is added, the SpringSharedLibAsset.jar file will automatically be added to the application as a shared library. Continuing the example:

    1. In the admin console, navigate to Applications > Applications Types > Business Level Applications.
    2. Create a new HelloWorldSpring business level application.
    3. Add the helloworld-spring.jar asset to the application. You should now have a business level application that which contains two assets, SpringHelloWorld and SpringSharedLibAsset_0001.jar, as shown in Figure 7.
    4. Start the HelloWorldSpring application.
      Figure 7. Business level application
      Figure 7. Business level application

      The Result is an SCA component using implemention.spring that is visible to the SCA runtime.

For the purpose of this example, you can now use getService to retrieve and call the service from a JSP inside an enterprise application, as shown in Listing 5, or you can create a reference to the HelloWorldXMLExt service from another component implementation.

Listing 5. getService
compositeContext = CurrentCompositeContext.getContext();
          helloWorldExt = (HelloWorld)compositeContext.getService(HelloWorld.class, 
		" SpringComponent/HelloWorldXMLExt");
String result = helloWorldExt.sayHello(name);

Security

Authorization and security identity for implementation.spring components is handled by the SCA container. SCA policy sets, defined in the definitions.xml file, can be attached to the implementation to enforce authorization and security identity policies. This policy set is used in conjunction with the interaction policies on the bindings to authenticate and authorize access to the spring components. Administrative and application security needs to be enabled in order for security roles to be enforced.

The example in Listing 6 shows a policy set called allowRole1 being attached to the SpringComponentSCA component.

Listing 6. Example of policy set attachment on Spring component
<component name="SpringComponentSCA">
    <implementation.spring location="META-INF/SpringHelloWorld-context.xml" 
		policySets="policy:allowRole1" />
    <service name="testBean">
        <interface.java interface="test.spring.HelloWorld"/>
        <binding.sca />
    </service>
</component>

The policy set is defined in the definitions.xml file, as shown in Listing 7. Here, a policy set called allowRole1 (which applies to implementation.spring components) is created. It defines a new authorization role called administrator. Specific users or groups who should have access to this component will be mapped during deployment.

Listing 7. definitions.xml
<definitions
  xmlns="http://www.osoa.org/xmlns/sca/1.0"
  xmlns:sca="http://www.osoa.org/xmlns/sca/1.0"
  targetNamespace="http://testpolicy">
  <policySet name="allowRole1" appliesTo="sca:implementation.spring">
    <authorization>
      <allow roles="administrator"/>
    </authorization>
  </policySet>
</definitions>

Transactions

Transaction support for services defined in an implementation.spring component is handled by the Spring container. Transaction attributes for the service are specified in the Spring application context file. To propagate or suspend transactions for references in an implementation.spring component, the required SCA transaction intents are specified in the composite file.

The code segment in Listing 8 shows what needs to be added to the Spring application context file to configure transactions on the service. In this example, you have an SCA service called DataAccessService that requires a transaction context to be propagated. This would equate to specifying the managedTransaction.global intent on the implementation and the propagatesTransaction intent on the service. To find a description of how SCA intents map to Spring transaction attributes, refer to the Transaction section of the SCA Java EE Integration Specification (see Resources).

Listing 8. Configure transactions on the service
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<aop:config>
  <aop:pointcut id="readOperation" expression="execution(* 
	test.sca.service.tx.DataAccessServiceImpl.getValue(..))"/>
  <aop:advisor advice-ref="txAdvice" pointcut-ref="readOperation"/>
</aop:config>


<bean id="dataAccessDBGlobalBean" class="test.sca.service.tx.DataAccessServiceImpl" />

<sca:service name="DataAccessService" type="sca.service.DataAccessService" 
	target="dataAccessDBGlobalBean"/>

Since local JNDI lookups are not supported, the definition in Listing 9 needs to be added to the Spring application context file in order to obtain the WebSphere Application Server transaction manager.

Listing 9. Transaction manager definition
<bean id="WASTranMgr" class="com.ibm.wsspi.uow.UOWManagerFactory" 
	factory-method="getUOWManager"/>

<bean id="transactionManager" 
	class="org.springframework.transaction.jta.WebSphereUowTransactionManager">
    <property name="uowManager" ref="WASTranMgr"/>
    <property name="autodetectUserTransaction" value="false"/>
</bean>

To have Spring transaction support, the SpringSharedLibAsset.jar must contain all Spring libraries and the aspectjweaver.jar file.

Important limitations

In addition to the restrictions already described, the following apply when using the Feature Pack for SCA with Spring applications:

  • Exposing a Spring bean as a service is not supported when the bean implements multiple interfaces. To resolve this issue, explicitly define a <sca:service> element in the Spring application context file. If no explicit definition of <sca:service> is available, the problem remains by default when you expose all the beans as defined in the Spring context as services.
  • Callbacks are not supported.
  • Pass-by-reference is not supported.
  • The only supported bindings are Web services, EJB, JMS, and SCA default bindings.
  • Support is for Spring applications following the J2SE model. The Spring Framework in SCA uses the J2SE model and, like SCA applications, would not have access to the local namespace, which is required for elements accessed via java:comp. You must consider using explicit or direct JNDI lookup.

Summary

The Spring Framework is an open source project that provides a framework for simple Java objects. The WebSphere Application Server V7.0 Feature Pack for SCA provides support for components implemented with the Spring Framework version 2.5.5. Using <implementation.spring> enables SCA to compose Spring applications with other types of applications, and also exposes the Spring application through the various bindings supported by the SCA feature pack.

Resources

Learn

Get products and technologies

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere, SOA and web services
ArticleID=461202
ArticleTitle=Exploring the WebSphere Application Server Feature Pack for SCA, Part 6: Using Spring with Service Component Architecture
publish-date=01202010