Announcement Announcement Module
Collapse
No announcement yet.
JPA repositories with shared behaviour, factory not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA repositories with shared behaviour, factory not working

    Hi all,

    I'm trying to adding custom behaviour to all repositories, as described here
    http://static.springsource.org/sprin...l-repositories
    My spring config however complains about my repositories not having a default constructor, and thus cannot instantiate my @autowired repositories.
    I do have a RepositoryFactoryBean as factory-class mentioned in my <repositories> tag, but that factory bean isn't being used in any way. The question is, why not?
    I'm using Spring-3.2.2.RELEASE, please see the attached screenshot for all the libs. We're using Java 7 and Tomcat 7.

    Here's my configuration.
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:jpa="http://www.springframework.org/schema/data/jpa"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
        													   
      	<jpa:repositories base-package="nl.company.repository"
     	   	 factory-class="nl.company.repository.impl.MyRepositoryFactoryBean"/>
        
     	<import resource="spring-security-config.xml"/>
    
        <context:component-scan base-package="nl.company.web.controller"/>
     
    <!--
    	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
     -->
         
        <bean name="currentUserProvider" class="nl.company.web.security.CurrentUserProvider" />
        
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="spring-jpa" />
            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                    <property name="showSql" value="true" />
                    <property name="generateDdl" value="true" />
                </bean>
            </property>
            <property name="loadTimeWeaver">
                <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="#{projectProperties['jdbc.driverClassName']}" />
            <property name="url" value="#{projectProperties['jdbc.url']}" />
            <property name="username" value="#{projectProperties['jdbc.username']}" />
            <property name="password" value="#{projectProperties['jdbc.password']}" />
        </bean>
        
        <!-- Property settings for various profiles -->
        <util:properties id="projectProperties" location="/WEB-INF/app-local.properties"/>  
        <beans profile="test">
            <util:properties id="projectProperties" location="/WEB-INF/app-test.properties"/>  
        </beans>
        
    </beans>
    The exact errormessage is:
    Code:
    ERROR (ContextLoader.java:319) - Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyRepositoryImpl' defined in file [/Volumes/ramdisk/ramdisk_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/app/WEB-INF/classes/nl/company/repository/impl/CompanyRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [nl.company.repository.impl.CompanyRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [nl.company.repository.impl.CompanyRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
    	... 27 more
    Caused by: java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at java.lang.Class.getConstructor0(Class.java:2730)
    	at java.lang.Class.getDeclaredConstructor(Class.java:2004)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
    	... 28 more
    May 21, 2013 1:53:50 PM org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyRepositoryImpl' defined in file [/Volumes/ramdisk/ramdisk_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/app/WEB-INF/classes/nl/company/repository/impl/CompanyRepositoryImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [nl.company.repository.impl.CompanyRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1013)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:959)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [nl.company.repository.impl.CompanyRepositoryImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1006)
    	... 27 more
    Caused by: java.lang.NoSuchMethodException: nl.company.repository.impl.CompanyRepositoryImpl.<init>()
    	at java.lang.Class.getConstructor0(Class.java:2730)
    	at java.lang.Class.getDeclaredConstructor(Class.java:2004)
    	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78)
    	... 28 more
    AFAIK I have to inherit the constructor(Class<T> domainClass, EntityManager entityManager) from the MyRepositoryImpl class (See Example 1.15. Custom repository base class), making it impossible for the SimpleInstantiationStrategy class to instantiate the repository with a default constructor. But why is the factory-class nl.company.repository.impl.MyRepositoryFactoryBean not used?

    Any hints are greatly appreciated - I'm kinda stuck...

  • #2
    The stacktrace says that a no-arg default constructor cannot be found. Did you implement one in your class, "MyRepositoryImpl"? If you didn't, try adding one and see what happens.

    Comment

    Working...
    X