Using an EJB reference in a Spring component

In this recipe, we will configure a Spring bean to use an EJB reference from within a composite as a constructor argument. This is useful if we have a Spring bean that references an EJB, because it shows the dependency on the composite diagram.

Getting ready

Make sure that you have a composite open to configure the Spring component.

How to do it...

  1. Wire Spring component to the EJB reference.
    How to do it...

    In the design view of composite.xml, drag the reference link How to do it... from the Spring component to the service link How to do it... of the EJB reference.

    This will generate a new reference in Spring Context, as shown in the following code snippet:

    <bean id="employeeBean"
          class="soa.cookbook.EmployeeEJBClient"/>
    <sca:service name="employeeSpringService"
                 target="employeeBean"
                 type="soa.cookbook.EmployeeEJBBean"/>
    <sca:reference type="soa.cookbook.model.EmployeeEJB"
                   name="EmployeeEJBService"/>
    
  2. Use a reference in Spring Context.

    Open the composite in JDeveloper and drag the Bean Constructor Argument from the Component Palette onto the bean in the Spring Context Structure view. This will add a constructor to the bean.

    How to do it...
  3. Configure a constructor argument.

    Use the Bean Constructor Argument – Property Inspector window to set a ref. The ref should be the name of the generated sca:reference element in Spring Context.

    How to do it...

    This will generate the code, shown as follows, that wires the constructor argument of the bean to the EJB:

    <bean id="employeeBean"
          class="soa.cookbook.EmployeeEJBClient">
      <constructor-arg ref="EmployeeEJBService"/>
    </bean>
    <sca:service name="employeeSpringService"
                 target="employeeBean"
                   type="soa.cookbook.EmployeeEJBBean"/>
    <sca:reference type="soa.cookbook.model.EmployeeEJB"
                   name="EmployeeEJBService"/>

Your bean is now ready to use.

How it works...

When we wire another component service or reference to a Spring bean, then we create a new interface within Spring Context that can be injected into other Spring beans. In this case, we injected the EJB interface as a parameter to the constructor of our Spring bean, avoiding the need to use a JNDI lookup in our Spring code.

There's more...

If we were to wire up a reference from our Spring Context to a non-Java component, then JDeveloper would generate a JAXB mapping to make available the WSDL reference as a Java interface in our Spring Context. This makes it easy for our Spring Context to interact with any other portion of SOA Suite without us having to perform complex, object-XML mapping.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.172.93