Dependency injection problem
I'm doing an example from the Pro Srping3 book. I am having a problem with what should be a simple dependency injection
I have the following implementation class for a ContactService data retrival xface:
Code:
package com.apress.prospring3.ch10.service.springjpa;
// Implementation of ContactServiceR xface
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.apress.prospring3.ch10.domain.Contact;
import com.apress.prospring3.ch10.service.ContactService;
import com.apress.prospring3.ch10.repository.ContactRepository;
import com.google.common.collect.Lists;
@Service("springJpaContactService")
@Repository
@Transactional
public class ContactServiceImpl implements ContactService {
// Instead of the EntityManager, we just need to inject the ContactRepository xface
// into the service class and Spring Data JPA will do all the dirty work for us.
@Autowired
private ContactRepository contactRepository;
public List<Contact> findAll() {
return Lists.newArrayList(contactRepository.findAll());
}
@Transactional(readOnly=true)
public List<Contact> findByFirstName(String firstName) {
return contactRepository.findByFirstName(firstName);
}
@Transactional(readOnly=true)
public List<Contact> findByFirstNameAndLastName(String firstName,
String lastName) {
return contactRepository.findByFirstNameAndLastName(firstName, lastName);
}
}
The class I am using to test is:
Code:
package com.apress.propring3.ch10;
// Testing Program for ContactServiceR xface and ContactServiceRImpl
import java.util.List;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.apress.prospring3.ch10.domain.Contact;
import com.apress.prospring3.ch10.service.ContactService;
public class SpringJpaSample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:spring-data-app-context.xml");
ctx.refresh();
ContactService contactService = ctx.getBean("springJpaContactService", ContactService.class);
// Find all contacts
List<Contact> contacts = contactService.findAll();
listContacts(contacts);
// Find all by first name
contacts = contactService.findByFirstName("Clarence");
listContacts(contacts);
// Find all contacts by first and last name
contacts = contactService.findByFirstNameAndLastName("Clarence", "Ho");
}
private static void listContacts(List<Contact> contacts) {
System.out.println("");
System.out.println("Listing contacts without details:");
for (Contact contact: contacts) {
System.out.println(contact);
System.out.println("");
}
}
}
My config file is:
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="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/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans">
<!-- Note we added the jpa namespace to this configuration.-->
<!-- Use the H2 database for testing -->
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.apress.prospring3.ch10.domain"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.apress.prospring3.ch10.service.springjpa" />
<!-- The <jap:repositorie tag is used to configure Spring Data JPA's Repository abstraction. We instruct
Spring to scan the package com.apress.prospring3.ch10.repository for repository interfaces and to
pass in the EntityManagerFactory adn transaction manager respectively-->
<jpa:repositories base-package="com.apress.prospring3.ch10.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
</beans>
When I run the main I get the following exceptions:
Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'springJpaContactService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.apress.prospring3.ch10.repository.ContactRepos itory com.apress.prospring3.ch10.service.springjpa.Conta ctServiceImpl.contactRepository; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'contactRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NoSuchMethodError: org.springframework.data.repository.query.parser.P art.getProperty()Lorg/springframework/data/repository/query/parser/Property;
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 287)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 94)
at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:225)
at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:291 )
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:913)
at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:464)
at com.apress.propring3.ch10.SpringJpaSample.main(Spr ingJpaSample.java:18)
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private com.apress.prospring3.ch10.repository.ContactRepos itory com.apress.prospring3.ch10.service.springjpa.Conta ctServiceImpl.contactRepository; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'contactRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NoSuchMethodError: org.springframework.data.repository.query.parser.P art.getProperty()Lorg/springframework/data/repository/query/parser/Property;
........
The @Autowired contactRepository attribute is not being loaded, I'm not sure why. I added setter and getter methods, and even tried adding a constructor with this attribute as an argument, but it didn't help.
Can someone help me?
Thank
Mike
More info on Autowired injection problem
I should add that the contactRepository bean I am trying to inject which is failing to be created is defined below. I believe this bean should be created by the <jpa:repositories .... /> tag in the XML file
Code:
package com.apress.prospring3.ch10.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import com.apress.prospring3.ch10.domain.Contact;
public interface ContactRepository extends CrudRepository<Contact, Long> {
public List<Contact> findByFirstName(String firstName);
public List<Contact> findByFirstNameAndLastName(String firstName, String lastName);
}