Почему spring-data не создает bean репозитория?

270
15 августа 2017, 13:44

Есть простой репозиторий для объекта Items но при компиляции теста падает ошибка:

[main] WARN org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring-data-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource': no matching editors or conversion strategy found

Если я правильно все понял то проблема с приведением типов для объекта entityManagerFactory но не понятно что к ней приводит. Помогите пожалуйста разобраться.

Вот репозиторий:

public interface ItemRepository extends CrudRepository<Item, Integer> {
}

Это сам объект:

@Entity(name = "items")
public class Item {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String description;
    public Item() {
    }
    ...геттеры и сеттеры...
}

И конфигурационный файл spring-data-context.xml :

<?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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    <!-- Database properties -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://127.0.0.1:5432/spring_jdbc" />
        <property name="username" value="postgres" />
        <property name="password" value="1" />
    </bean>
    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
        <property name="database" value="POSTGRESQL" />
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" value="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <!-- Spring base scaling entity classes -->
        <property name="packagesToScan" value="ru.pravvich" />
    </bean>
    <bean id="transactionalManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <jpa:repositories base-package="ru.pravvich" />
</beans>

И на всякий случай структура проекта:

Буду очень признателен за любую помощь. Спасибо.

Answer 1

ругается он на это

Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource';

<property name="dataSource" value="dataSource" />

тут вы хотите чтобы в dataSource лежала строка "dataSource" или это отсылка к бину?

Если последние то надо так :

<property name="dataSource">
    <bean ref = "dataSource"/>
</property >
READ ALSO
Gradle и jdk asm

Gradle и jdk asm

В проекте, собираемом gradle, используется jdkinternal

202
Различие ответа в браузере и в приложении

Различие ответа в браузере и в приложении

Добрый деньДелаю приложение, заменяющее работу с браузером для HP Service Manager (без REST API)

199
Загрузка картинки на сервер. Java REST API +Ajax

Загрузка картинки на сервер. Java REST API +Ajax

Никак не получаеться отправить картинку на серверСначала в js обарачиваю картинку в FormData, а на сервере принимаю как MultiPartFile, но получаю с ajax ошибку...

302
Как оформить XSSFWorkbook

Как оформить XSSFWorkbook

Всем привет! Сейчас делаю отчет, который выводится в excel

253