SCA初探

   SCA其实有点象SPRING,只不过sca注入的是一个个实在的服务而已,快要成为工业标准了。下面笔记DEMO中的一个典型计算器

加法的接口:

package calculator;

/**
 * The Add service interface
 */
public interface AddService {

    double add(double n1, double n2);

}

加法的实现:

package calculator;

/**
 * An implementation of the Add service
 */
public class AddServiceImpl implements AddService {

    public double add(double n1, double n2) {
        return n1 + n2;
    }

}

减法的接口:

package calculator;

/**
 * The interface for the multiply service
 */
public interface SubtractService {

    double subtract(double n1, double n2);

}
减法的实现:

package calculator;

/**
 * An implementation of the subtract service.
 */
public class SubtractServiceImpl implements SubtractService {

    public double subtract(double n1, double n2) {
        return n1 - n2;
    }

}

乘法的接口:

package calculator;

/**
 * The interface for the multiply service
 */
public interface MultiplyService {

    double multiply(double n1, double n2);

}

乘法的实现:

package calculator;

/**
 * An implementation of the Multiply service.
 */
public class MultiplyServiceImpl implements MultiplyService {

    public double multiply(double n1, double n2) {
        return n1 * n2;
    }

}

除法的接口:

package calculator;

/**
 * The divide service interface
 */
public interface DivideService {

    double divide(double n1, double n2);

}


除法的实现:

package calculator;

/**
 * An implementation of the Divide service.
 */
public class DivideServiceImpl implements DivideService {

    public double divide(double n1, double n2) {
        return n1 / n2;
    }


}

计算器的接口

package calculator;


/**
 * The Calculator service interface.
 */
public interface CalculatorService {

    double add(double n1, double n2);

    double subtract(double n1, double n2);

    double multiply(double n1, double n2);

    double divide(double n1, double n2);

}

计算器的实现

package calculator;

import org.osoa.sca.annotations.Reference;


/**
 * An implementation of the Calculator service.
 */
public class CalculatorServiceImpl implements CalculatorService {

    private AddService addService;
    private SubtractService subtractService;
    private MultiplyService multiplyService;
    private DivideService divideService;

    @Reference
    public void setAddService(AddService addService) {
        this.addService = addService;
    }

    @Reference
    public void setSubtractService(SubtractService subtractService) {
        this.subtractService = subtractService;
    }

    @Reference
    public void setDivideService(DivideService divideService) {
        this.divideService = divideService;
    }

    @Reference
    public void setMultiplyService(MultiplyService multiplyService) {
        this.multiplyService = multiplyService;
    }

    public double add(double n1, double n2) {
        return addService.add(n1, n2);
    }

    public double subtract(double n1, double n2) {
        return subtractService.subtract(n1, n2);
    }

    public double multiply(double n1, double n2) {
        return multiplyService.multiply(n1, n2);
    }

    public double divide(double n1, double n2) {
        return divideService.divide(n1, n2);
    }

}

calculator.composite组装文件

<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
           targetNamespace="http://sample"
           xmlns:sample="http://sample"
           name="Calculator">

    <component name="CalculatorServiceComponent">
  <implementation.java class="calculator.CalculatorServiceImpl"/>
        <reference name="addService" target="AddServiceComponent" />
        <reference name="subtractService" target="SubtractServiceComponent" />
        <reference name="multiplyService" target="MultiplyServiceComponent" />
        <reference name="divideService" target="DivideServiceComponent" />
    </component>

    <component name="AddServiceComponent">
        <implementation.java class="calculator.AddServiceImpl"/>
    </component>

    <component name="SubtractServiceComponent">
        <implementation.java class="calculator.SubtractServiceImpl"/>
    </component>

    <component name="MultiplyServiceComponent">
        <implementation.java class="calculator.MultiplyServiceImpl"/>
    </component>

    <component name="DivideServiceComponent">
        <implementation.java class="calculator.DivideServiceImpl"/>
    </component>

</composite>

客户端使用

package calculator;

import org.apache.tuscany.sca.host.embedded.SCADomain;

/**
 * This client program shows how to create an SCA runtime, start it,
 * and locate and invoke a SCA component
 */
public class CalculatorClient {
    public static void main(String[] args) throws Exception {

        SCADomain scaDomain = SCADomain.newInstance("Calculator.composite");
       
        CalculatorService calculatorService =
            scaDomain.getService(CalculatorService.class, "CalculatorServiceComponent");

        // Calculate
        System.out.println("3 + 2=" + calculatorService.add(3, 2));
        System.out.println("3 - 2=" + calculatorService.subtract(3, 2));
        System.out.println("3 * 2=" + calculatorService.multiply(3, 2));
        System.out.println("3 / 2=" + calculatorService.divide(3, 2));

        scaDomain.close();
    }

}


 

你可能感兴趣的:(apache,spring)