Using OEPE, you can generate Spring configuration and beans from persistence mappings.
This document contains the following sections:
You can use OEPE to automatically generate Spring configuration and beans from persistence mappings.
OEPE supports Spring IDE 2.0 and Spring Framework 2.5.
To generate Spring artifacts, such as the DAO interface and its implementation, the service interface and its implementation, and configuration files, you have to perform the following steps:
Configure your project for Spring.
Generate the Spring files and update the configuration.
To configure your project for Spring:
Either add the Spring facet when you create a project, or add the facet to an existing project by right-clicking your project in the Project Explorer and selecting Properties from the drop-down menu. This opens the Properties dialog.
Note that the Spring facet is available to Web projects, EJB projects and utility projects. Adding the Spring facet to an EJB project requires special attention.
In the Properties dialog, select Project Facets on the left panel, and then select Spring from the Project Facet list.
To configure your project to use Spring Framework 2.5 Runtime library, select the Spring Framework 2.5 facet, and then click Further configuration required. This opens the Modify Faceted Project > Spring dialog.
Accept the default selections and click OK.
If you select either the Spring Framework 2.0 or 2.5 facet on the Properties dialog, and you wish to either use your own Spring runtime installation or download a copy of Spring libraries hosted by Oracle or SpringSource, select User Library in the Library Type field of the Modify Faceted Project > Spring dialog.
You can also click the Manage User Library icon to select a Java User Library pointing a proper local Spring runtime installation.
Click Download Library icon to indicate your intention to obtain Spring libraries. This will open the Download Library dialog.
To use this dialog to download the Spring libraries, make appropriate selections, and then click Finish to trigger the download.
Optionally, you may click Manage libraries icon to open the Preferences > User Libraries dialog to fine-tune your Spring library configuration. Note that by default, the entire library represented by the
spring.jar file is included.
Click OK on the Modify Faceted Project > Spring Facet dialog.
By clicking Apply > OK on the Properties dialog, you will complete the configuration of the Spring Framework library for your project.
If you added the Spring facet to a Web project, the project's
web.xml file will be updated to include the context listener for loading the Spring context at the Web application's start time, as the following example shows:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEBINF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
If you are creating Spring service beans and bean configurations for an EJB project, you should keep in mind that, unlike a Web project, there is no standard way to automatically load Spring bean configuration. In this case, a common practice is to define a Web project to help initialize the application context. Consider the following project set-up:
[EJB with Spring facet]
[Web project with Spring]
It is assumed that the EJB project includes the Spring service beans and bean configuration under the
META-INF/bean.xml file. In the Web project, find the default
WEB-INF/applicationContext.xml file created during the addition of the Spring facet, and add a factory bean looking for other bean configurations in the classpath, as follows:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans/xsd"> <bean id="bean.factory" class="org.springframework.context.support/classPathXmlApplicationContext"> <constructor-arg> <list> <value>META-INF/bean.xml</value> </list> </constructor-arg> </bean> </beans>
With this setting, the client code (such as a servlet, for example) in the JSP can access the Spring beans residing in the EJB project. Consider the following code sample from a JSF Web application:
ApplicationContext context = FacesContextUtils.getRequiredWebApplicationContext(FacesContext.getCurrentInstance( )); ApplicationContext jpaCtx = (ApplicationContext)context.getBean("bean.factory"); //now there is an access to service beans ICustServService srv = (ICustServService)jpaCtx.getBean("CustServService"); List<Customer> list = srv.findAllCustomers( );
To generate the Spring files and update the configuration:
To initiate the Spring artifact generation, right-click on the project name in the Project Explorer, and select New > Generate Spring ORM Classes. This opens the New Spring ORM Classes > Spring Services Definition wizard page.
Notice that the Spring bean configuration file field has been filled in when you added the Spring facet to your project. If you wish to use another file, click Manage Spring bean configuration, and then make your selection.
Click Add on the New Spring ORM Classes > Spring Services Definition dialog. This opens the New ORM Service dialog.
On the New ORM Service dialog, provide the following information:
Enter the service name.
Specify your project in Persistence unit. Note that you can select the persistence unit only when the service is created for the first time. Once created, you can add or remove entities from the existing persistence unit, but cannot switch to another persistence unit. To access another persistence unit, you create a new Spring Bean configuration and add a new service to it.
Select one or more entities from the ORM entities list.
Specify the exception that will be thrown by the service methods.
Provide the package information for the service and DAO classes and interfaces.
Specify the name for the service and DAO classes and interfaces (you may want to accept default values).
Click OK to continue.
Verify that the New Spring ORM Classes > Spring Services Definition dialog correctly displays the service name and the entities that you selected in the previous step, and then click Next. This opens the New Spring ORM Classes > Implementation Strategy wizard.
Select the implementation strategy for your Spring ORM service and DAO classes, and then click Finish.
The Spring classes are generated in the package that you specified. The bean classes contain methods for standard create, read, update, and delete operations.
Note that only the DAO implementation class is dependent on a specific framework. This lets you change ORM frameworks quickly, without rewriting client code.
You can generate a WebLogic web service from an existing Spring service bean for your JAX-WS Web service project.
To generate the web service:
Add the WebLogic Web Services facet to an Eclipse project configured with the Spring 2.5 facet and containing Spring service beans.
For more information, see Section 11.1, "Generating Spring Artifacts.".
In the Project Explorer, right-click your project, select New > Other, and then select Oracle > WebLogic > WebLogic Web Service for Spring Beans on the New dialog. This opens the Select Spring Service Bean dialog.
On the Select Spring Service Bean dialog, specify the Spring bean configuration. The list is populated with Spring service beans defined in the configuration file. Select a service bean and service methods from the service interface that you want to expose as WebLogic Web Service, and then click Next.
While one service bean can implement multiple interfaces, you can only select methods from one interface at a time. To expose methods in other interfaces, you need to generate a separate Web service. Also, Spring ORM service beans implemented in Spring 1.n-style based on the
On the Web Service Class Name page, you may choose to specify the Web service class name, package name and output directory, and then click Finish.
Upon completion, Eclipse creates the WebLogic Web Service class which is automatically wired up with Spring bean and delegate service calls to the Spring bean implementation.