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.
In the design view of composite.xml
, drag the reference link from the Spring component to the service link 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"/>
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.
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.
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.
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.
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.