1

Im looking for create jpa datasource for my application with configuration java, and i wrote the code below:

@ComponentScan("com.package.datasource.*")
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
    @Bean(name = "datasource")
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource
                = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        dataSource.setUrl("jdbc:postgresql://localhost:3309/example?autoReconnect=true");
        dataSource.setUsername("user");
        dataSource.setPassword("pwddb");
        return  dataSource;
    }
}

and in datasource.xml i have:

<bean id="datasource" class="com.package.datasource.DataSourceConfig"/>
    <bean id="persistenceUnitManager"
            class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
            <property name="persistenceXmlLocations">
                <list>
                    <value>classpath*:META-INF/persist.xml</value>
                </list>
            </property>
            <property name="defaultDataSource" ref="datasource" />
            <property name="dataSources">
                <map>
                    <entry key="datasource" value-ref="datasource" />
                </map>
            </property>
        </bean>

i have this exception when Im looking for start application:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in ServletContext resource [/WEB-INF/spring-config/datasource.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type [com.package.datasource.DataSourceConfig$$EnhancerBySpringCGLIB$$4033708d] to required type [javax.sql.DataSource] for property 'defaultDataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.oxylane.cadplm.shapes.datasource.DataSourceConfig$$EnhancerBySpringCGLIB$$4033708d] to required type [javax.sql.DataSource] for property 'defaultDataSource': no matching editors or conversion strategy found
2
  • What DataSource import are you using? Commented Aug 23, 2021 at 9:18
  • I use this :import javax.sql.DataSource; Commented Aug 23, 2021 at 9:55

1 Answer 1

1

Remove this line and it should be good.

<bean id="datasource" class="com.package.datasource.DataSourceConfig"/>

Your above line is wrong as the class of the datasource in order to instantiate datasource is not DataSourceConfig.

DataSourceConfig is a configuration class which will create an instance of datasource from javax.sql.dataSource The datasource will be automatically available for your application from the annotation

@Bean(name = "datasource")
    public DataSource getDataSource() {
    ...

So the first line that I indicated here is not needed for your application and is actualy wrong as it would probably needed to be

<bean id="datasource" class="javax.sql.DataSource"/>

But as already said you don't need it as you define it by annotations in your DataSourceConfig.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your response. I have explore this and i have this exception: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceUnitManager' defined in ServletContext resource [/WEB-INF/spring-config/datasource.xml]: Cannot resolve reference to bean 'datasource' while setting bean property 'defaultDataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'datasource' is defined 'Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'datasource' is defined'
your class DataSourceConfig is not picked up by spring. Move it to the same package where the class with @SpringBootApplication exists
I don't have spring boot application, but when i changed the position of package datasource where spring scan class it work, thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.