title: How do I configure dynamic weaving using EclipseLink & Spring?
body: <p>How do I configure dynamic weaving using EclipseLink &amp; Spring?  Right now I'm trying to get this working with a Junit test, but I'll later have to have it work with Tomcat (my department has been standardized on it for something like 10 years).</p>
  
  <p>I'm running into two main problems:</p>
  
  <p><strong>1. Spring wants a load time weaver:</strong></p>
  
  <p>Exception:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:181)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
  &lt;snip&gt;
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
  &lt;snip&gt;
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
  &lt;snip&gt;
  Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
      at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:91)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1437)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1408)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      ... 44 more
  </code></pre>
  
  <p>I can fix this by configuring the Spring Instrumentation:</p>
  
  <pre><code>JVM parameter:
  -javaagent:/path/to/org.springframework.instrument-3.1.0.M2.jar
  </code></pre>
  
  <p><strong>2. Eclipselink is not satisfied with the Spring Instrumentation:</strong></p>
  
  <p>Error:</p>
  
  <pre><code>(similar error messages repeated many times)
  
  [EL Severe]: 2011-06-20 16:12:57.951--ServerSession(979781372)--Local Exception Stack: 
  Exception [EclipseLink-0] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.IntegrityException
  Descriptor Exceptions: 
  ---------------------------------------------------------
  
  Exception [EclipseLink-60] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
  Exception Description: The method [_persistence_set_shellType_vh] or [_persistence_get_shellType_vh] is not defined in the object [ps.entity.JudicialArtifactFamily].
  Internal Exception: java.lang.NoSuchMethodException: ps.entity.JudicialArtifactFamily._persistence_get_shellType_vh()
  Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[shellType]
  Descriptor: RelationalDescriptor(ps.entity.JudicialArtifactFamily --&gt; [DatabaseTable(JUDICIAL_ARTIFACT_FAMILY)])
  
  Exception [EclipseLink-218] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
  Exception Description: A NullPointerException would have occurred accessing a non-existent weaved _vh_ method [_persistence_get_shellType_vh].  The class was not weaved properly - for EE deployments, check the module order in the application.xml deployment descriptor and verify that the module containing the persistence unit is ahead of any other module that uses it.
  </code></pre>
  
  <p>I <em>tried</em> to fix this by using the EclipseLink weaver:</p>
  
  <pre><code>-javaagent:/dev/tools/javaagents/eclipselink.jar
  </code></pre>
  
  <p>But then I get the same exception from problem #1.</p>
  
  <p><strong>Library versions</strong></p>
  
  <p>I am using:</p>
  
  <ul>
  <li>Java 1.6.0_24</li>
  <li>EclipseLink 2.2.0.v20110202-r8913</li>
  <li>Spring 3.1.0 M2</li>
  </ul>
  
  <p><strong>Configuration files</strong></p>
  
  <p>Here is my persistence.xml:</p>
  
  <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               version="2.0"
               xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="eclipseLinkHashPU" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;class&gt;ps.entity.Artifact&lt;/class&gt;
          &lt;class&gt;ps.entity.JudicialArtifactFamily&lt;/class&gt;
          &lt;class&gt;ps.entity.ShellType&lt;/class&gt;
          &lt;class&gt;ps.entity.SourceArtifact&lt;/class&gt;
          &lt;class&gt;ps.entity.Health&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;    
              &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables"/&gt;
              &lt;property name="eclipselink.ddl-generation.output-mode" value="database"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>Here is my application.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxrs="http://cxf.apache.org/schema/jaxrs" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-2.5.xsd
             http://cxf.apache.org/schema/jaxrs
             http://cxf.apache.org/schema/jaxrs.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd          
             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"&gt;
  
      &lt;context:component-scan base-package="ps"&gt;
      &lt;/context:component-scan&gt;
  
      &lt;context:load-time-weaver/&gt;
  
      &lt;alias alias="entityManagerFactory" name="eclipseLinkEntityManagerFactory"/&gt;
  
      &lt;bean id="eclipseLinkEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceXmlLocation" value="classpath:eclipselink-persistence.xml" /&gt;
          &lt;property name="persistenceUnitName" value="eclipseLinkHashPU" /&gt;
          &lt;property name="jpaPropertyMap"&gt;
              &lt;map&gt;
                  &lt;entry key="eclipselink.logging.level" value="Severe"/&gt;
                  &lt;entry key="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.OraclePlatform"/&gt;
                  &lt;entry key="javax.persistence.nonJtaDataSource" value-ref="hash1DataSource"/&gt;
                  &lt;!-- it's not good that I'm turning weaving off --&gt;
                  &lt;!-- &lt;entry key="eclipselink.weaving" value="false"/&gt; --&gt;
              &lt;/map&gt;
          &lt;/property&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"&gt;
                  &lt;property name="databasePlatform" value="oracle" /&gt;
                  &lt;property name="database" value="ORACLE" /&gt;
                  &lt;property name="generateDdl" value="true" /&gt;
                  &lt;property name="showSql" value="true" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- datasources, etc --&gt;
  &lt;/beans&gt;
  </code></pre>