Java SpringBoot-创建名为“org.springframework.Boot.autoconfigure.jdbc.DataSourceAutoConfiguration”的bean时出错
我有spring启动应用程序设置。现在我需要向它添加SpringJDBC模板。在这样做的时候,我面临着以下例外Java SpringBoot-创建名为“org.springframework.Boot.autoconfigure.jdbc.DataSourceAutoConfiguration”的bean时出错,java,spring,spring-boot,spring-jdbc,spring-java-config,Java,Spring,Spring Boot,Spring Jdbc,Spring Java Config,我有spring启动应用程序设置。现在我需要向它添加SpringJDBC模板。在这样做的时候,我面临着以下例外 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXX': Injection of autowired dependencies failed; nested exception is org.springframework.beans
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'XXX': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.jdbc.core.JdbcTemplate com..XXX.jdbcTemplate; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "LOCAL" are currently active).
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "LOCAL" are currently active).
下面是代码
@Service
public class XXX {
@Autowired
JdbcTemplate jdbcTemplate;
public void testDataSource() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
System.out.println("list : " + list);
}
}
当SpringBoot查找application.properties时,我也在resources目录中添加了它。
应用。属性
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD
spring.datasource.username=Solid_batch
spring.datasource.password=solid_batch123
spring.datasource.initialize=true
它无法生成应用程序。如果我做错了,请纠正我。您的项目类路径中缺少ojdbc jar,请按照以下步骤下载、安装并将其用作依赖项:
从下载ojdbc6.jar
安装它,运行命令-
mvn安装:安装文件-Dfile={Path/to/your/ojdbc.jar}-DgroupId=com.oracle-DartifactId=ojdbc6-Dversion=11.2.0-dpackage=jar
对于jar版本,提取jar文件并在MANIFEST.MF中检查实现版本,例如:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_51-b10 (Sun Microsystems Inc.)
Implementation-Vendor: Oracle Corporation
Implementation-Title: JDBC
Implementation-Version: 11.2.0.4.0
Repository-Id: JAVAVM_11.2.0.4.0_LINUX.X64_RELEASE
Specification-Vendor: Sun Microsystems Inc.
Specification-Title: JDBC
Specification-Version: 4.0
Main-Class: oracle.jdbc.OracleDriver
sealed: true
Name: oracle/sql/converter/
Sealed: false
Name: oracle/sql/
Sealed: false
Name: oracle/sql/converter_xcharset/
Sealed: false
Name: oracle/replay/driver/
Sealed: false
添加为项目中的依赖项,如下所示:
com.oracle
ojdbc6
11.2.0
我已经阅读了spring boot参考文档。我知道,如果我们使用H2、HSQL或Derby数据库,那么我们就不需要application.properties
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@g9u1769.houston.hpecorp.net:1525:ODSDBD
spring.datasource.username=Solid_batch
spring.datasource.password=solid_batch123
spring.datasource.initialize=true
如果项目具有Oracle数据库,则应更新application.properties。就我而言,我已经更新了属性文件
所以我只更新了以下内容,然后它就正常工作了
pom.xml
我希望这能帮助别人。
谢谢@ChrisThompson和@Arpit你能告诉我们你的主要方法吗?通常情况下,当你得到一个无法确定的驱动程序类型。。。错误,您没有实际使用您认为是的配置。另外,不要将属性文件中的重复配置与Java配置相结合。这只会让事情变得混乱。你的类路径上有ojdbc jar吗?@ChrisThompson我的应用程序是一个rest服务,当请求到来时,我需要调用这个方法。所以我没有任何主菜。我认为,当我们首先构建应用程序时,它应该读取配置文件,并且必须为该服务进行配置。因此,它在服务类XXX中失败。一开始我没有application.properties。我只是关注了一些博客,但没有用。@AdityaSarma:我有ojdbc.jar。如果我没有它应该给不同的错误,我希望如此。like class not found.@abc您是如何运行Spring启动应用程序的?我的构建路径中有ojdbc.jar。您是否按照我在步骤2中的建议安装了ojdbc.jar?我认为这不是必需的。我已经检查了buildpath,并在那里找到了ojdbc.jar。确切地说,我能够使用测试类运行上述类。它正在发挥作用。我知道这是可行的,但我必须使用SpringBootJDBC来编写。感谢您的帮助。Spring boot自动为您配置数据源。在大多数情况下,您不需要声明Datasource@Bean。Spring boot根据application.properties中指定的数据源属性初始化数据源。但是,您仍然可以在需要时声明它们。@CodeItLikeAmeen:谢谢,阿米恩。是的,这是我为我的项目创建的。
Use jdbctemplate by extends JdbcDaoSupport .
By it programmer not concern about the open and close the connection.
Use commons-dbcp2-2.1.1.jar and commons-pool2-2.4.2.jar for use dbcp2 because dbcp2 support Connection pooling.
It's a technique to allow multiple clinets to make use of a cached set of shared and reusable connection objects providing access to a database
public class XXX extends JdbcDaoSupport {
public void testDataSource() {
List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from C_MASTER");
System.out.println("list : " + list);
}
}
In spring.xml write
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/database_name" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<bean id="xXX" class="your_package_name.XXX">
<property name="dataSource" ref="dataSource" />
</bean>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
@Configuration
public class DaoConfig {
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
dataSource.setUrl("xxx");
dataSource.setUsername("xxx");
dataSource.setPassword("xxx");
return dataSource;
}
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(getDataSource());
}