spring 的数据源配置
Data source config with spring
我正在寻找使用配置 java 为我的应用程序创建 jpa 数据源,我编写了以下代码:
@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;
}
}
在 datasource.xml 我有:
<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>
我在寻找启动应用程序时遇到此异常:
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$33708d] 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$33708d] to required type [javax.sql.DataSource] for property 'defaultDataSource': no matching editors or conversion strategy found
去掉这一行应该就好了
<bean id="datasource" class="com.package.datasource.DataSourceConfig"/>
您上面的行是错误的,因为用于实例化数据源的数据源 class 不是 DataSourceConfig。
DataSourceConfig 是一个配置 class,它将从 javax.sql.dataSource
创建一个数据源实例 数据源将自动从注解
供您的应用程序使用
@Bean(name = "datasource")
public DataSource getDataSource() {
...
所以我在这里指出的第一行对于您的应用程序来说不是必需的,实际上是错误的,因为它可能需要
<bean id="datasource" class="javax.sql.DataSource"/>
但如前所述,您不需要它,因为您通过 DataSourceConfig 中的注释定义了它。
我正在寻找使用配置 java 为我的应用程序创建 jpa 数据源,我编写了以下代码:
@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;
}
}
在 datasource.xml 我有:
<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>
我在寻找启动应用程序时遇到此异常:
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$33708d] 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$33708d] to required type [javax.sql.DataSource] for property 'defaultDataSource': no matching editors or conversion strategy found
去掉这一行应该就好了
<bean id="datasource" class="com.package.datasource.DataSourceConfig"/>
您上面的行是错误的,因为用于实例化数据源的数据源 class 不是 DataSourceConfig。
DataSourceConfig 是一个配置 class,它将从 javax.sql.dataSource
创建一个数据源实例 数据源将自动从注解
@Bean(name = "datasource")
public DataSource getDataSource() {
...
所以我在这里指出的第一行对于您的应用程序来说不是必需的,实际上是错误的,因为它可能需要
<bean id="datasource" class="javax.sql.DataSource"/>
但如前所述,您不需要它,因为您通过 DataSourceConfig 中的注释定义了它。