18. Blending Actions with non-generated Spring Beans

PROBLEM

The Blending Actions with Custom Java Code recipe described how to use custom java from within Actions, and it was accomplished by creating an instance to the JAVA classes that contain the code. As further described in the recipe, there are various options for creating those instances. However the Invoke Java step doesn't support having instances of a java class be acquired through Spring.

SOLUTION

The Invoke Spring step lets you invoke Spring beans from an Action. By adding one or more beans to a Spring context file and registering the Spring context file in the Skyway modelling project, the application developer lets Spring handle instantiating the bean. Using the Invoke Spring step, the developer can leverage the logic contained in the bean.

HOW IT WORKS

In order for a Spring bean to be used by the Invoke Spring step, the class must have a PUBLIC constructor that doesn't require any arguments. The following figure shows a very basic Spring Bean (MyBean) that will be leveraged from an Action.

Example 2.13. Blending Spring Beans with Actions - MyBean.java

package example.web.mycontroller.mymodel;

public class MyBean {

 private String country;

 public String getCountry() {
   return country;
 }

 public void setCountry(String country) {
   this.country = country;
 }

 public MyBean(){
   System.out.println("I have been instantiated");
 }
 
 public String doThis(String x){
   System.out.println(country);
   return x+"DONE";
 }
}

The following Spring context defines a bean call MyBean.

Example 2.14. Spring Context File - mycontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-2.5.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

  <bean id="MyBean" class="example.web.mycontroller.mymodel.MyBean">
     <property name="country" value="Ecuador"/>
  </bean>

</beans>

Steps for calling Spring Beans using Invoke Spring Step:

  1. Copy mycontext.xml to the source folder of the same project (i.e. example-Web/src) that MyBean.java is defined in.

  2. From the Spring Configuration tab on the Model project (i.e. example) add mycontext.xml to project Spring contexts. Note that the context file can be added to the web layer and/or service layer. Make sure you add it to the correct layer based on the project you added MyBean.java and mycontext.xml to.

  3. From an Action, open the Java drawer in the Skyway Step Palette.

  4. Click on the Invoke Spring step in the palette and click on the Action Canvas. The step will be placed at the location you click.

  5. Switch to the Bean panel on the Steps properties.

  6. Select an invocation type

    • Static - the bean will be looked-up by bean id

    • Dynamic - the bean will be looked-up by bean class

  7. After the invocation type is selected, the Bean panel will show the fields that need to be configured. Configure the remaining fields to invoke MyBean.

  8. Based on the method signature, the Parameters panel will list any input parameters. The input parameters must be mapped to variables

The following examples will show the generated code for the various invocation types.

Static Invocation

Example 2.15. Invoke Spring Step - Static Invocation (Generated Code)

  // InvokeSpringStep: Invoke Spring
  MyBean bean = (MyBean) doBeanLookup("MyBean");1

  String inputSting = ((String) getVariableContext().get("inputString"));2

  String result = bean.doThis(inputString);3
  getVariableContext().set("outputString", result);4

1

Spring is providing an instance of MyBean.

2

Input parameters are being obtained from model variables.

3

The doThis() method is being called, and input parameters are being passed.

4

The return parameter is being assigned to model variable.

Dynamic Invocation

Example 2.16. Invoke Spring Step - Dynamic Invocation (Generated Code)

  // InvokeSpringStep: Invoke Spring
  MyBean bean = (MyBean) doBeanLookup(example.web.mycontroller.mymodel.MyBean.class);1

  String inbean = ((String) getVariableContext().get("inbean"));2

  String result = bean.doThis(inbean);3
  getVariableContext().set("outbean", result);4

1

Spring is providing an instance of example.web.mycontroller.mymodel.MyBean.

2

Input parameters are being obtained from model variables.

3

The doThis() method is being called, and input parameters are being passed.

4

The return parameter is being assigned to model variable.

RELATED RECIPES

  1. Implementing Controller Methods using Actions and Steps

  2. Blending Actions with non-generated Java Code