Space: Apache Tuscany Docs 2.x 
(https://cwiki.apache.org/confluence/display/TUSCANYxDOCx2x)
Page: SCA Java implementation.spring 
(https://cwiki.apache.org/confluence/display/TUSCANYxDOCx2x/SCA+Java+implementation.spring)


Edited by Raymond Feng:
---------------------------------------------------------------------
h2. <implementation.spring>

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

The [Spring 
specification|http://www.osoa.org/download/attachments/35/SCA_SpringComponentImplementationSpecification-V100.pdf?version=1]
 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 
|http://www.oasis-opencsa.org/sca-spring] document.

(on) In [Spring 
Framework|http://static.springframework.org/spring/docs/2.5.5/reference] an 
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.

The integration with Spring will be at the SCA Composite level, where a Spring 
application context provides a complete composite, exposing services and using 
references via SCA. This means that a Spring application context defines the 
internal structure of a SCA composite implementation.

(on) Tuscany uses Spring Framework 3.0.x-RELEASE (requires Java 1.4+)

h3. 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_

h3. How to Use Spring Component Implementation?

The Spring component implementation SCDL has the following format:

{code}
   <implementation.spring location="targetURI" />
{code}

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:
{code}
   <implementation.spring location="./spring/application-context.xml" />
{code}

b) Specifying a Directory:
{code}
   <implementation.spring location="./spring" />
{code}

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:
{code}
   <implementation.spring location="./spring.jar" />
{code}

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

(on) 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.

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

{code}
<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>
{code}


h3. Handling multiple Spring Application Contexts

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

h4. Using <import> Element:

Each import element points to an application context xml file.
{code}
<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>

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

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

h4. Using ClassPathXmlApplicationContext Bean Definition:

Each list value points to an application context xml file.
{code}
<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>
{code}

h3. Some Examples:

h4. Spring BigBank Sample

The spring-bigbank sample demonstrates most of the functionality that is 
specified in the [specifications |http://www.oasis-opencsa.org/sca-spring].

See the [simple-bigbank-spring 
|http://svn.apache.org/repos/asf/tuscany/java/sca/samples/simple-bigbank-spring/]
 sample for a complete example.

h4. Direct use of SCA references within a Spring configuration

See the [spring-bigbank-calculator 
|http://svn.apache.org/repos/asf/tuscany/java/sca/samples/spring-bigbank-calculator/]
 sample for a complete example of using direct SCA references within Spring 
configuration.

The properties like addService, subtractService, multiplyService and 
divideService defined in the Spring configuration as shown below

{code}
<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>
{code}

are the direct representation of the SCA references defined in the composite 
file.

h4. 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 properties checkingAccountService, calculatorService and stockQuoteService 
defined in the Spring configuration as shown below

{code}
<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>
{code}

can be declared explicit as SCA beans in Spring Application Context using the 
<sca:reference> element.

See the [simple-bigbank-spring 
|http://svn.apache.org/repos/asf/tuscany/java/sca/samples/simple-bigbank-spring/]
 sample for a complete example of using explicit declaration of SCA related 
beans.

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

(on) 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.

h5. Working with SCA WebServices Binding

Declaring Service

{code}
<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>
{code}

Declaring Reference in a component which consumes the Service declared above

{code}
<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>
{code}

See the [spring-bigbank-stockquote 
|http://svn.apache.org/repos/asf/tuscany/java/sca/samples/spring-bigbank-stockquote/]
 sample for a complete example of using SCA Web Service binding.

h5. Working with SCA RMI Binding

See the [spring-bigbank-calculator 
|http://svn.apache.org/repos/asf/tuscany/java/sca/samples/spring-bigbank-calculator/]
 sample for a complete example of using SCA RMI binding.

h3. 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/]


h3. Non-Supported Features in Tuscany

Currently Tuscany does not support implicit SCA references and properties for 
scenario 1 & 2 in spring context as shown below.

h5. Scenario 1: Using implicit SCA References & Properties for Constructors
{code}
<constructor-arg><ref bean="mySCAService1"/></constructor-arg>
<constructor-arg><ref bean="mySCAService2"/></constructor-arg>
{code}

In the above scenario, particularly in cases where the spring bean has defined 
only one constructor, implicit references / properties can be supported when 
the contructor-arg element specifies the type of the SCA reference/property 
that its trying to consume by specifying a type attribute in the contructor-arg 
OR when the appropriate index attribute is specified in the constructor-arg 
element.

In cases where the spring bean has defined multiple contructors, its mandatory 
that all the SCA references / properties used by the constructors should be 
defined explicitly. And when the constructor-arg uses SCA reference/property 
within collections, those should be defined explicitly.

h5. Scenario 2: Using implicit SCA References & Properties in List, Map and Set 
of bean properties.

Using implicit SCA references and properties within collection as shown in the 
below code sample will not be supported by Tuscany. Instead we recommed to use 
explicit SCA references/properties in such cases.

{code}
<bean id="moreComplexObject" class="example.ComplexObject">
  <!-- results in a setSomeList(java.util.List) call -->
  <property name="someList">
    <list>
        <value>a list element followed by a reference</value>
        <ref bean="mySCAService1" />
    </list>
  </property>
  <!-- results in a setSomeMap(java.util.Map) call -->
  <property name="someMap">
    <map>
        <entry>
            <key>
                <value>an entry</value>
            </key>
            <value>just some string</value>
        </entry>
        <entry>
            <key>
                <value>a ref</value>
            </key>
            <ref bean="mySCAService2" />
        </entry>
    </map>
  </property>
  <!-- results in a setSomeSet(java.util.Set) call -->
  <property name="someSet">
    <set>
        <value>just some string</value>
        <ref bean="mySCAService3" />
    </set>
  </property>
</bean>
{code}

h5. Scenario 3: Exposing SCA Service from Spring Bean that implements multiple 
interfaces.

Raised as Specification Issue: [http://www.osoa.org/jira/browse/JAVA-59]


h3. Integration with Spring for Web Applications (including Spring MVC)



[Presentation|^Spring Tuscany.pptx]


h3. 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]


h3. References

Spring Framework - 
[http://static.springframework.org/spring/docs/2.5.5/reference/index.html]

Change your notification preferences: 
https://cwiki.apache.org/confluence/users/viewnotifications.action    

Reply via email to