Results 1 to 2 of 2

Thread: @PersistenceContext. no longer working after removing <context:component-scan />

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    Default @PersistenceContext. no longer working after removing <context:component-scan />

    This question has to do with several Spring technologies, but I think this is the most appropriate place to put it. I recently created a Spring Roo application. By default, the application is configured to use component scanning. Here is the original applicationContext.xml file.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    
        <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    
        <context:spring-configured/>
    
        <context:component-scan base-package="com.myproject">
            <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
        
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
        
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
        
        <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="persistenceUnitName" value="transactions-optional"/>
        </bean>
        
    </beans>
    I am planning on deploying the application to the Google App Engine. However, according to Google's documentation, you should turn of component scanning and use XML configuration so your application loads faster.

    https://developers.google.com/appeng...g_optimization

    Therefore, I commented out <context:component-scan /> from applicationContext-xml and added my beans. Here is the new applicationContext.xml file.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    
        <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    
        <context:spring-configured/>
    
        <!-- <context:component-scan base-package="com.myproject">
            <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan> -->
        
        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
        </bean>
        
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
        
        <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
            <property name="persistenceUnitName" value="transactions-optional"/>
        </bean>
    
        <bean id="myService" class="com.myproject.server.service.impl.MyServiceImpl" />
        //additional service beans
        
    </beans>
    All of my services work fine. The problem is related to my entities. @PersistenceContext is no longer injecting an entityManager. Here is the basic pattern all of my entities use (created by Spring Roo)

    Code:
    public class MyEntity
    {
         //private fields...
    }
    
    privileged aspect MyEntity_Roo_Jpa_ActiveRecord {
        
        @PersistenceContext
        transient EntityManager MyEntity.entityManager;
        
        public static final EntityManager MyEntity.entityManager() {
            EntityManager em = new Bid().entityManager;
            if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
            return em;
        }
        //Data access methods....
    }
    
    
    privileged aspect  MyEntity_Roo_Jpa_Entity {
        
        declare @type: MyEntity: @Entity;
    
       //Spring Roo managed fields...
    }
    
    privileged aspect MyEntity_Roo_Configurable {
        
        declare @type: MyEntity: @Configurable;
        
    }
    If I set the entityManager by hand everything works fine. However, I would like to continue to use @PersistenceContext. It would seem as if this annotation was dependent on <context:component-scan />, but I can't fine anything in the documentation saying this is the case. In fact, the documentation implies <context:component-scan /> has nothing to do with @PersistenceContext. Am I missing something in my configuration file? Is it a limitation of GAE?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I suggest a re-read of the documentation again and also the javadoc.

    component-scan implies a couple of things one is annotation driven configuration another is (by detection) when the JPA api is available it also injects the @PersistenceContext. For this it will add the PersistenceAnnotationBeanPostProcessor for exception translation it will register a PersistenceExceptionTranslationPostProcessor. So either add these beans or add context:annotation-config (which among others also registers those PostProcessors).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •