Show me
Show me

Reputation: 13

Data source config with spring

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

Upvotes: 1

Views: 1347

Answers (1)

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18899

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.

Upvotes: 1

Related Questions