Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Spring Web Tutorial"

m (EclipseLink JPA Deployed on Spring)
m (Systematic Inclusion of Spring libraries in WebLogic)
Line 99: Line 99:
 
====Systematic Inclusion of Spring libraries in WebLogic====
 
====Systematic Inclusion of Spring libraries in WebLogic====
 
*The goal is to get the latest Spring 3.0.5 working with the latest WebLogic 10.3.3.0.  Hopefully we do not need to rever to using the supported Spring 3.5.3 or 3.5.6 versions.
 
*The goal is to get the latest Spring 3.0.5 working with the latest WebLogic 10.3.3.0.  Hopefully we do not need to rever to using the supported Spring 3.5.3 or 3.5.6 versions.
*The following ClassNotFoundExceptions are being eradicated one at a time usually be easily matching the class with the appropriate 1 of 20 Spring module.
+
*The following ClassNotFoundExceptions are being eradicated one at a time usually be easily matching the class with the appropriate 1 of 20 Spring module.  It would be nice to use some sort of OSGI support for dynamic module loading - we are only using PDE at this point.
 
**1) '''org.springframework.web.servlet.DispatcherServlet''' is in ''org.springframework.web.servlet-3.0.5.RELEASE.jar''
 
**1) '''org.springframework.web.servlet.DispatcherServlet''' is in ''org.springframework.web.servlet-3.0.5.RELEASE.jar''
 
**2) '''org.apache.commons.logging.Log''' is in ''commons-logging.jar''
 
**2) '''org.apache.commons.logging.Log''' is in ''commons-logging.jar''
 
**3) '''org.springframework.web.util.UrlPathHelper''' is in ''org.springframework.web-3.0.5.RELEASE.jar''
 
**3) '''org.springframework.web.util.UrlPathHelper''' is in ''org.springframework.web-3.0.5.RELEASE.jar''
**4) '''org.springframework.context.ApplicationContext''' is in ''''
+
**4) '''org.springframework.context.ApplicationContext''' is in ''org.springframework.context-3.0.5.RELEASE.jar''
 
Caused By: java.lang.ClassNotFoundException: org.springframework.web.util.UrlPathHelper
 
Caused By: java.lang.ClassNotFoundException: org.springframework.web.util.UrlPathHelper
  

Revision as of 10:30, 23 December 2010

EclipseLink JPA Deployed on Spring

DISCLAIMER: This page reflects investigation into how EclipseLink users can use EclipseLink JPA and the Spring Framework inside existing WebLogic and Spring releases. It does NOT imply any formal certification from Oracle or SpringSource on any technical details or configuration within this document this time.

This content is undergoing an expansion as of 20101220 in enhancement # 332953 from an EE enthusiast.


Purpose

  • A portion of enterprise development uses the Spring framework which is an alternative to traditional JEE/EJB distributed transactions(1.0), local transactions (2.0) and transactional SE frameworks (3.0). Spring has grown into as large a framework as JEE6 from its original goal of providing a simpler less verbose enterprise MVC framework. The spring framework can be used in conjunction with or as a replacement for EJB 3.1 technologies.
  • This document is for Spring developers that wish to use EclipseLink JPA as their persistence provider or for Spring developers that are migrating from another ORM/JPA provider.
  • The open source persistence provider - EclipseLink JPA or native EclipseLink can be the JPA or ORM provider for Spring beans in Spring based applications.
  • The API provided by the Spring framework can range from using the dependency injection (DI) features along with the Aspect Oriented Programming (AOP) support - all the way to using declarative transactions and management of remote resources.
  • In this tutorial page we will concentrate on providing support for developers and projects that wish to use EclipseLink with Spring applications and we will demonstrate a very simple web application that uses either a JTA or local RESOURCE_LOCAL persistence unit and use Spring's @Autowired, @Controller, @RequestMapping, @Repository and possibly the AOP @Aspect and @Around annotations as required.

JPA Enterprise Application running on Spring inside WebLogic Server

  • As most enterprise applications that use Spring will do so via a web application - instead of a Web/EJB EE combination - we will concentrate on Web (WAR) applications.
  • WebLogic offers the following services to Spring applications
  • The primary use case we will prototype for this tutorial is one where the application uses Spring (either managing its' own local transactions, or using the transaction manager in WebLogic for distributed transactions) as the enterprise container on WebLogic Server. WebLogic currently support Spring 2.5.3 and Dependency Injection using Spring in non-Spring applications via the open source Pitchfork project.
  • Therefore theoretically we should be able to use Spring AOP and other DI annotations in our application without any extra configuration - we will verify this.
  • A secondary use case will be migration of a SpringSource Tool Suite 2.5.0.M3 (based on Eclipse 3.6 and Apache Tomcat 6.0.20) based native Hibernate project to one that uses EclipseLink JPA.

JEE and Spring correspondence

  • Spring can be used in parallel with formal EE specification technologies like IIOP Remote Session Beans and Transactions or as an alternative to them using Spring's remote RMI/JAX-RPC/WS proxying and declarative transactions/rollback support.
  • However, Spring does not provide native distributed transaction support or native ORM support - in both these cases JEE must be used.
    • Distributed transactions via transactions that occur across remote session bean methods
    • ORM persistence is provided via JPA (using either EclipseLink or Hibernate as the provider) - or via native ORM for either of these persistence providers.
  • Using the Spring framework as the EE container allows the user to run enterprise applications on simple JSR-154 Servlet containers like Tomcat, Jetty, WebSphere CE, Spring TC or Spring DM/Virgo
  • The spring framework treats all EE artificts as Spring Beans

Architecture

  • There are 3 standard tiers to our web application.
    • 1) presentation/view layer (JSP/JSF pages)
    • 2) controller layer (Controller Servlet)
    • 3) integration/data-access layer (Spring beans)
  • The cross-cutting concerns are
    • 1) The data model (Entities)
    • 2) Logging
    • 3) Transaction Management (Spring beans)
  • We will be using standard JPA Entities for our data model domain objects.
  • The domain objects will be managed by Spring beans that act as Data Access Objects between our Entities and the persistence store

Installation

WebLogic 10.3.3.0

  • Oracle WebLogic officially supports Spring 2.5.x - we will be verifying this July 2008 statement in the System Requirements and Supported Platforms for Oracle WebLogic Server 10.3 spreadsheet - page 2.
  • In WebLogic 10.3.3.0 there exists 3 spring jars in the modules directory - these are internally wrapped API that support the 2.5.3 version of Spring as indicated. We may upgrade these to work with Spring 3 as long as we do not encounter issues between 2.5.3 and 3.0 dependency injection. The pitchfork jar is part of the combined Oracle and SpringSource open source project for metadata translation of JSR-250 annotations for both WebLogic ans Spring - started by Rod Johnson of Interface21 and Michael Chen of BEA under both the Apache and EPL licenses.
  • WebLogic has used Spring as its AOP (Aspect Oriented Programming) engine since June 2005 to provide support for DI (Dependency Injection) of resources and functionality in a Proxy implementation on the server that contains a reference to the actual business object (Bean or EJB). The container uses Spring AOP to bytecode weave - at runtime - the proxy so that cross-cutting concerns can be architected properly. By instrumenting a business object at runtime using AOP we are able to provide dynamic functionality before and after a client operation in support of pre and post operations for transactions and security for example.
  • The notable thing about using the com.bea.core namespace is that it will not confict with any com.springsource Spring versions shipped with Spring EAR applications.
  • From what I can see there is a bit of history on Spring and WebLogic cooperation
  • The wlserver_10.3/server/lib/weblogic-spring.jar contains WebLogic specific integration with the above 3 spring jars
  • The wlserver_10.3/server/lib/console-ext/spring-console.jar contains WebLogic specific integration with the WebLogic admin console

WebLogic 10.3.3.0 Default Spring Support

  • If you try to deploy a Spring framework based application - you will see the following CNFE (ClassNotFoundException) or similar depending on what Spring JSR-250 annotations or XML markup you use.
<21-Dec-2010 3:52:51 o'clock PM EST> <Error> <HTTP> <BEA-101371> <There was a failure when processing annotations for application F:\wse\view_w35e\org.eclipse.persistence.example.jpa.spring.weblogic.enterpriseWeb\WebContent. Please make sure that the annotations are valid. The error is org.springframework.web.servlet.DispatcherServlet> 
<21-Dec-2010 3:52:51 o'clock PM EST> <Error> <Deployer> <BEA-149265> <Failure occurred in the execution of deployment request with ID '1292964771051' for task '1'. Error is: 'weblogic.application.ModuleException: Failed to load webapp: 'springhypercube''
weblogic.application.ModuleException: Failed to load webapp: 'springhypercube'
	at weblogic.servlet.internal.WebAppModule.prepare(WebAppModule.java:393)
	at weblogic.application.internal.flow.ScopedModuleDriver.prepare(ScopedModuleDriver.java:176)
	at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:199)
	at weblogic.application.internal.flow.DeploymentCallbackFlow$1.next(DeploymentCallbackFlow.java:517)
	at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:52)
	Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
	at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
	at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
	at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:64)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
  • In my case, this is because of the standard Spring DispatchServlet which replaces your own FrontController, FacesServlet or StrutsController - single point of control in web.xml
    <servlet>
        <servlet-name>FrontController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-config.xml</param-value>
        </init-param>
    </servlet>
    <!-- standard web.xml (non-Spring) - leave commented --> 
    <!-- servlet>
      <description></description>
      <display-name>FrontController</display-name>
      <servlet-name>FrontController</servlet-name>
      <servlet-class>org.eclipse.persistence.example.jpa.spring.presentation.FrontController</servlet-class>
    </servlet-->
  • The reason that the org.springframework.web.servlet.DispatcherServlet class cannot be loaded by WebLogic is either that it is unavailable or that there is another version in the classpath (not likely or we would get a ClassCastException).
  • The DispatcherServlet is inside org.springframework.web.servlet-3.0.5.RELEASE.jar
  • You therefore need to distribute the Spring jars or place them on the server as explained in the section

Systematic Inclusion of Spring libraries in WebLogic

  • The goal is to get the latest Spring 3.0.5 working with the latest WebLogic 10.3.3.0. Hopefully we do not need to rever to using the supported Spring 3.5.3 or 3.5.6 versions.
  • The following ClassNotFoundExceptions are being eradicated one at a time usually be easily matching the class with the appropriate 1 of 20 Spring module. It would be nice to use some sort of OSGI support for dynamic module loading - we are only using PDE at this point.
    • 1) org.springframework.web.servlet.DispatcherServlet is in org.springframework.web.servlet-3.0.5.RELEASE.jar
    • 2) org.apache.commons.logging.Log is in commons-logging.jar
    • 3) org.springframework.web.util.UrlPathHelper is in org.springframework.web-3.0.5.RELEASE.jar
    • 4) org.springframework.context.ApplicationContext is in org.springframework.context-3.0.5.RELEASE.jar

Caused By: java.lang.ClassNotFoundException: org.springframework.web.util.UrlPathHelper

Spring 3 Download

Spring Integration with Eclipse

  • 1) If you wish to customize spring support in eclipse - enable the Spring project nature via the context menu off our Web application

Spring Tools menu in Eclipse 35 ee.jpg

  • 2) And manually enter the jars below...
  • Instead of picking individual jars - just add everything from the dist directory to your in-Web lib directory, or directly on the server on either your domain lib or the modules dir of the server.
    • \user_projects\domains\base_domain\lib
    • \modules
C:\opt\spring305\dist>dir /B
Library-SymbolicName: org.springframework.spring
Library-Version: 3.0.5.RELEASE
Library-Name: Spring Framework
Import-Bundle: 
 org.springframework.aop;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.asm;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.aspects;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.beans;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.context;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.context.support;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.core;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.expression;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.jdbc;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.jms;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.orm;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.oxm;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.transaction;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.web;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.web.servlet;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 org.springframework.web.portlet;version="[3.0.5.RELEASE, 3.0.5.RELEASE]",
 com.springsource.org.aopalliance;version="[1.0.0, 1.0.0]"
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.portlet-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web.struts-3.0.5.RELEASE.jar


  • Where the following classpath variables need to be defined
    • SPRING_AOP=org.springframework.aop-3.0.5.RELEASE.jar
    • SPRING_ASM=org.springframework.asm-3.0.5.RELEASE.jar
    • SPRING_ASPECTS=org.springframework.aspects-3.0.5.RELEASE.jar
    • SPRING_BEANS=org.springframework.beans-3.0.5.RELEASE.jar
    • SPRING_CONTEXT=org.springframework.context-3.0.5.RELEASE.jar
    • SPRING_CONTEXT_SUPPORT=org.springframework.context.support-3.0.5.RELEASE.jar
    • SPRING_CORE=org.springframework.core-3.0.5.RELEASE.jar
    • SPRING_EXPRESSION=org.springframework.expression-3.0.5.RELEASE.jar
    • SPRING_INSTRUMENT=org.springframework.instrument-3.0.5.RELEASE.jar
    • SPRING_INSTRUMENT_TOMCAT=org.springframework.instrument.tomcat-3.0.5.RELEASE.jar
    • SPRING_JDBC=org.springframework.jdbc-3.0.5.RELEASE.jar
    • SPRING_JMS=org.springframework.jms-3.0.5.RELEASE.jar
    • SPRING_ORM=org.springframework.orm-3.0.5.RELEASE.jar
    • SPRING_OXM=org.springframework.oxm-3.0.5.RELEASE.jar
    • SPRING_TEST=org.springframework.test-3.0.5.RELEASE.jar
    • SPRING_TRANSACTION=org.springframework.transaction-3.0.5.RELEASE.jar
    • SPRING_WEB=org.springframework.web-3.0.5.RELEASE.jar
    • SPRING_PORTLET=org.springframework.web.portlet-3.0.5.RELEASE.jar
    • SPRING_SERVLET=org.springframework.web.servlet-3.0.5.RELEASE.jar
    • SPRING_STRUTS=org.springframework.web.struts-3.0.5.RELEASE.jar

Spring Integration with WebLogic

  • There are several options for getting the Spring framework enabled for your Spring WAR in WebLogic Server.

Option 1: Spring library jars in WebLogic modules directory

Option 2: Spring library jars in WebLogic Domain lib directory

Eclipse 3.6 Setup

Standard Eclipse 3.6 EE

  • Spring must be configured as follows.

SpringSource Tools Suite 2.5.1

  • Spring will run out of the box on Apache Tomcat 6.0.20

JNDI Datasource Setup

EclipseLink JAR

Spring JARs

Create Enterprise Projects

  • Possible configurations of the projects are...
  • Using the default WebLogicJtaTransactionManager
    • SC1: WAR only project with entities/beans inside war jar.
    • SC2: WAR only project with entities/beans inside separate domain jar.
    • SC3: EE EAR project with entities/beans inside the war project
    • SC4: EE EAR project with entities/beans inside separate ejb.jar project
    • SC5: EE EAR project with entities/beans inside separate domain jar project

Object Model

  • Following Scenario SC3 we will place our entities inside

Dependency Injection

@Controller

@Autowired

@RequestMapping

@Repository

@Transactional

  • Declarative transaction management - like JSR-220/JSR-318 EJB 3.0/3.1 transactional stateless and stateful session bean annotations via the javax.ecj.SessionSychronization package updated for non-remote calls (2.0) and non-EE (SE) clients in (3.0)

Web.xml

  • The version of the xmlns:web namespace must be 2.5 not 2.4
  • Add a persistence-ref for this application managed persistence unit
<persistence-context-ref>
   <persistence-context-ref-name>persistence/em</persistence-context-ref-name>
   <persistence-unit-name>spring</persistence-unit-name>
  </persistence-context-ref>

org.eclipse.persistence.jpa.PersistenceProvider

  • The SPI file javax.persistence.spi.PersistenceProvider' containing the text org.eclipse.persistence.jpa.PersistenceProvider must be placed at the root of where the entities/beans are off of META-INF/services

Persistence.xml

servlet-config.xml

application-config.xml

  • If you were using a Hibernate SessionFactory for ORM you could describe your entities in the AnnotationSessionFactoryBean bean element to match the class elements from persistence.xml - if auto-discovery was not enabled
  • The

Transaction Manager Configuration

  • If we use the default WebLogicJtaTransactionManager we get the following Spring beans by default when the application context is wrapped in a parent proxy by WebLogic
    • ref="transactionManager" - a subclass of org.springframework.transaction.jta.jtaTransactionManager

Configuration MBeans

  • On WebLogic Server we get the following Spring beans by default when the application context is wrapped in a parent proxy.
  • Both of these MBeans should be running off the platform MBeanServer - I will need to verif this.
    • ref="editMBeanServerConnection"
    • ref="runtimeMBeanServerConnection"

Servlet Client

Start Server

Publish EAR

Console Output

Browser Output

Previous 2008 content - currently undergoing refactoring on 20101220

Downloading EclipseLink Libraries

Download EclipseLink using HTTP - recommended

Download EclipseLink using Maven

See the repository on http://www.eclipse.org/eclipselink/downloads/index.php

Download EclipseLink using SVN - developers only

  • Get the following eclipselink.jar and javax.persistence*.jar from http://www.eclipselink.org ready for your EclipseLink shared library - (I am running rev 2.0.0.20090218-SNAPSHOT)


http://www.springframework.org/download http://sourceforge.net/project/downloading.php?group_id=73357&use_mirror=voxel&filename=spring-framework-2.5.5-with-dependencies.zip&50273885

Eclipse Launch Targets

-javaagent:c:\opt\spring3\org.springframework.instrument-3.0.0.RELEASE.jar

Change Log

  • 20080922 - start tutorial template (no content)
  • 20101217 - revisit tutorial using Spring 3, SpringSource Tool Suite 2.5.0, AspectJ 1.6 weaver at 3019.

References

Known Issues

Back to the top