0

Есть простой репозиторий для объекта 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>

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

введите сюда описание изображения

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

1 ответ 1

2

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

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 >
3
  • в <property name="dataSource" value="dataSource" /> я заменил value на ref совсем в прямом смысле не получилось как вы показали или я что-то не так понял, но теперь у меня другая ошибка: oSuchBeanDefinitionException: No bean named 'transactionManager' available: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!
    – Pavel
    13 авг 2017 в 17:43
  • приведите пожалуйста полный код. А то я путаюсь(((
    – Pavel
    13 авг 2017 в 17:45
  • 1
    Имя вашего бина не transactionManager, а transactionalManager 13 авг 2017 в 18:01

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.