You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 26 Next »

Unable to render {include} The included page could not be found.
Unable to render {include} The included page could not be found.

<implementation.spring>

The Tuscany Java SCA runtime supports components implemented with Spring Framework by using the <implementation.spring> SCDL extension.

The Spring specification defines how Spring and SCA work with one another. The Spring Component implementation is one of the SCA extensions which is being formalized in the OASIS Open Composite Services Architecture with a published specifications document.

A Spring context is represented as an SCA Composite in the SCA world. An SCA composite can include one or more Spring type composites along with other component types. Therefore, a Spring type composite can be exposed as services or reference other SCA components. For example, you can have an SCA composite that includes Spring and Ruby components.

A Spring Application Context (an SCA composite) contains one or more Spring Beans. A Spring ApplicationContext is the main interface to the Spring IoC container. It is used to look up objects. It can be viewed as the Java object representation of the application-Context.xml file that contains the bean definitions.

How Spring Application Context is used as SCA Composite Implementation?

  • A Spring Application Context is used as an implementation within an SCA composite component.
  • A component that uses Spring for an implementation can wire SCA services and references without introducing SCA metadata into the Spring configuration. The Spring context knows very little about the SCA environment.
  • SCA runtime enforces SCA policies and Spring Application Context is unaware of it.
  • It should be possible to generate an SCA Composite from any Spring context and use that composite within an SCA assembly. This feature is under review by the OASIS Specs Team, yet to be implemented in Tuscany

How to Use Spring Component Implementation?

The Spring component implementation SCDL has the following format:

   <implementation.spring location="targetURI" />

Where the location attribute of that element specifies the target uri of an archive file or directory or the fully qualified path that contains the Spring application context files.

An example of all the three ways of specifying the target uri in the location attribute is shown below

a) Specifying Fully Qualified Path:

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

b) Specifying a Directory:

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

Here the target uri specifies the resource as a directory named "spring", where all the spring related files are available.

c) Specifying an Archive file:

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

Here the target uri specifies the resource as an archive file name "spring.jar", where all the spring related files are available.

(lightbulb) In case of b) and c), If the resource identified by the location attribute is an archive file then the file META-INF/MANIFEST.MF is read from the archive. If the location URI identifies a directory, then META-INF/MANIFEST.MF must exist underneath that directory.

If the manifest file contains a header "Spring-Context" of the format: Spring-Context ::= path ( ';' path )*

Where path is a relative path with respect to the location URI, then the set of paths specified in the header identify the context configuration files. If there is no MANIFEST.MF file or no Spring-Context header within that file, then the default behavior is to build an application context using application-context.xml file in the META-INF/spring directory.

How Spring Application Context is Aware of Beans used in SCA composition?

Your existing Spring Application context should define the http://www.springframework.org/schema/sca namespace in order to make the Spring aware of the SCA related beans. This is shown below.

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

Handling multiple Spring Application Contexts

Each <service> element used with <implementation.spring> should include the name of the Spring bean
that is to be exposed as an SCA service in its name attribute. So, for Spring, the name attribute of a
service plays two roles: it identifies a Spring bean, and it names the service for the component.

Tuscany supports the following configurations to handle multiple Spring Application Context XML files.

Using <import> Element:

Each import element points to an application context xml file.

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>

    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

Using ClassPathXmlApplicationContext Bean Definition:

Each list value points to an application context xml file.

<bean id="beanRefFactory" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>services.xml</value>
            <value>resources/messageSource.xml</value>
        </list>
    </constructor-arg>
</bean>

Some Examples:

Spring BigBank Sample

The spring-bigbank sample demonstrates most of the functionality that is specified in the specifications .

See the simple-bigbank-spring sample for a complete example.

Direct use of SCA references within a Spring configuration

See the spring-bigbank-calculator sample for a complete example of using SCA references.

Can the code below be removed if we are referring to the sample? If not, can we just have an add example?
The references like addService, subtractService, multiplyService and divideService defined by SCA composite as shown below

<composite>

    <component name="CalculatorServiceComponent">
		<implementation.spring location="META-INF/spring/CalculatorService-context.xml"/> 

        <reference name="addService" target="AddServiceComponent" />
        <reference name="subtractService" target="SubtractServiceComponent" />
        <reference name="multiplyService" target="MultiplyServiceComponent" />
        <reference name="divideService" target="DivideServiceComponent" />
    </component>

    <component name="AddServiceComponent">
        <tuscany:implementation.script script="calculator/AddServiceImpl.js"/>
    </component>

    <component name="SubtractServiceComponent">
        <tuscany:implementation.script script="calculator/SubtractServiceImpl.rb"/>
    </component>

    <component name="MultiplyServiceComponent">
        <tuscany:implementation.script script="calculator/MultiplyServiceImpl.py"/>
    </component>

    <component name="DivideServiceComponent">
        <tuscany:implementation.script script="calculator/DivideServiceImpl.groovy"/>
    </component>

</composite>

can be directly used by Spring Application Context as shown here...

<beans>

    <sca:service name="CalculatorService"
        type="bigbank.calculator.CalculatorService" target="CalculatorServiceBean"/>

    <bean id="CalculatorServiceBean" class="bigbank.calculator.CalculatorServiceImpl">
        <!-- Here are some implicit references - a property with a ref not satisifed within the
         * Spring application context.
         -->
        <property name="addService" ref="addService"/>
        <property name="subtractService" ref="subtractService"/>
        <property name="multiplyService" ref="multiplyService"/>
        <property name="divideService" ref="divideService"/>
    </bean>

</beans>

See the spring-bigbank-calculator sample for a complete example of using SCA references directly.

Explicit declaration of SCA related beans inside a Spring Application Context

It is also possible to explicitly declare SCA-related beans inside a Spring configuration to proxy SCA references. The primary reason you may do this is to enable the Spring container to decorate the bean (using Spring AOP for example).

The references checkingAccountService, calculatorService and stockQuoteService defined by SCA composite as shown below

<composite name="BigBank">

    <component name="AccountServiceComponent">
        <implementation.spring location="spring-context/Account-spring-context.xml"/>
        
        <service name="AccountService">
            <interface.java interface="bigbank.account.AccountService"/>
        </service>

        <reference name="savingsAccountService" target="SavingsAccountServiceComponent"/>
        
        <reference name="checkingAccountService">
          <interface.java interface="bigbank.account.checking.CheckingAccountService"/>
          <binding.jms initialContextFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
                       jndiURL="tcp://localhost:61619">
              <destination name="RequestQueue" create="always"/>
              <response>
                  <destination name="ResponseQueue" create="always"/>
              </response> 
          </binding.jms>
        </reference>
        
        <reference name="stockAccountService" target="StockAccountServiceComponent"/>
        
        <reference name="calculatorService">
            <tuscany:binding.rmi host="localhost" port="8099" serviceName="CalculatorRMIService"/>
        </reference>      
        
        <reference name="stockQuoteService">
            <binding.ws uri="http://localhost:8081/services/StockQuoteWebService"/>
        </reference>
    </component>

    <component name="SavingsAccountServiceComponent">
        <implementation.composite name="bbsv:SavingsAccountDept"/>
    </component>

    <component name="StockAccountServiceComponent">
        <implementation.composite name="bbst:StockAccountDept"/>
    </component>

</composite>

can be declared explicit as SCA beans in Spring Application Context as shown below using the Spring SCA Namespace schema..

<beans>
   <bean id="AccountServiceBean" class="bigbank.account.AccountServiceImpl">     
        <property name="calculatorService" ref="calculatorService"/>
        <property name="stockQuoteService" ref="stockQuoteService"/>
        <property name="checkingAccountService" ref="checkingAccountService"/>
        
        <!-- Here are some implicit references & properties - a property with a ref 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"/>
    
    <sca:reference name="calculatorService" type="bigbank.calculator.CalculatorService"/>
    
    <sca:reference name="stockQuoteService" type="bigbank.stockquote.StockQuoteService"/>
</beans>

See the simple-bigbank-spring sample for a complete example of using explicit declaration of SCA related beans.

Using SCA Bindings for Spring Implementation

We know that a component that uses Spring for an implementation can wire SCA services and references without introducing SCA metadata into the Spring configuration. The Spring context knows very little about the SCA environment. Hence the SpringComponent implementation remains the same as shown from some of the examples above but different bindings are chosen at the SCA Composite level as shown below.

NOTE: All kind of bindings supported by SCA can be used for Spring Implementation as the bindings are independent of Spring context. Few examples can be seen below.

Working with SCA WebServices Binding

Declaring Service

<composite name="StockQuote">
    
    <service name="StockQuoteService" promote="StockQuoteServiceComponent">
        <interface.java interface="bigbank.stockquote.StockQuoteService"/>
        <binding.ws uri="http://localhost:8081/services/StockQuoteWebService"/>
    </service>

    <component name="StockQuoteServiceComponent">
        <implementation.spring location="META-INF/spring/StockQuoteService-context.xml"/>
    </component>

</composite>

Declaring Reference in a component which consumes the Service declared above

<component name="AccountServiceComponent">
    <implementation.spring location="spring-context/Account-spring-context.xml"/>

    <reference name="stockQuoteService">
       <binding.ws uri="http://localhost:8081/services/StockQuoteWebService"/>
    </reference>
</component>

See the spring-bigbank-stockquote sample for a complete example of using SCA Web Service binding.

Working with SCA RMI Binding

Declaring Service

<composite name="Calculator">
    <service name="CalculatorService" promote="CalculatorServiceComponent">
        <interface.java interface="bigbank.calculator.CalculatorService"/>
        <tuscany:binding.rmi host="localhost" port="8099" serviceName="CalculatorRMIService"/>
    </service>

    <component name="CalculatorServiceComponent">
        <implementation.spring location="META-INF/spring/CalculatorService-context.xml"/>
        <reference name="addService" target="AddServiceComponent" />
        <reference name="subtractService" target="SubtractServiceComponent" />
        <reference name="multiplyService" target="MultiplyServiceComponent" />
        <reference name="divideService" target="DivideServiceComponent" />
    </component>
</composite>

Declaring Reference in a component which consumes the Service declared above

<component name="AccountServiceComponent">
    <implementation.spring location="spring-context/Account-spring-context.xml"/>
    
    <reference name="calculatorService">
        <tuscany:binding.rmi host="localhost" port="8099" serviceName="CalculatorRMIService"/>
    </reference>
</component>

See the spring-bigbank-calculator sample for a complete example of using SCA RMI binding.

Access the Spring-ApplicationContext from everywhere in your Application

Tuscany creates an custom Application context with an implementation logic for all SCA related beans like <sca:service>, <sca:reference> and <sca:properties> available in the Spring Application Context.

To access the application context in your application we recommend to use the suggested approach in this article.
http://blog.jdevelop.eu/2008/07/06/access-the-spring-applicationcontext-from-everywhere-in-your-application/

Spring SCA Namespace schema

The spring-sca.xsd can be found at the following location: http://www.osoa.org/xmlns/sca/1.0/spring-sca.xsd

why is this code needed here if there is a link to the spring-sca.xsd?

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/sca"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
	elementFormDefault="qualified" targetNamespace="http://www.springframework.org/schema/sca">
	<xsd:element name="composite">
		<xsd:complexType>
			<xsd:attribute name="component" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="sca-adapter-class" use="optional">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
		</xsd:complexType>
	</xsd:element>
	<xsd:element name="reference">
		<xsd:complexType>
			<xsd:attribute name="name" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="type" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="default" use="optional">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
		</xsd:complexType>
	</xsd:element>
	<xsd:element name="property">
		<xsd:complexType>
			<xsd:attribute name="id" use="optional">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="name" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="type" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
		</xsd:complexType>
	</xsd:element>
	<xsd:element name="service">
		<xsd:complexType>
			<xsd:attribute name="name" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="type" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
			<xsd:attribute name="target" use="required">
				<xsd:simpleType><xsd:restriction base="xsd:string" /></xsd:simpleType>
			</xsd:attribute>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>

References

Spring Framework - http://static.springframework.org/spring/docs/2.0.x/reference/index.html

  • No labels