我是Spring的新手,并尝试用Oracle DB实现Spring启动的一个例子,用于我的学习目的。请指导我。我不知道我错过了什么。以下是错误,我无法解决。
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
我在POM.xml中添加了以下依赖项
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
</dependencies>
下面是application.properties
#Tomcat Server Properties
server.port = 8099
#Oracle Database Properties
oracle.username=xxxxx
oracle.password=xxxxx
oracle.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
#Hibernate Properties
spring.jpa.database-platform=org.hibernate.dialect.OracleDialect
这是我的SpringBootLearningApplication.java
package com.learning.springboot.main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.learning.springboot")
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
public class SpringBootLearningApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootLearningApplication.class, args);
}
}
user repository.Java
package com.learning.springboot.dao;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.learning.springboot.entity.User;
@Repository
public interface UserRepository extends CrudRepository<User, String> {
}
和我的控制器类TestController.java
package com.learning.springboot.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.learning.springboot.dao.UserRepository;
import com.learning.springboot.entity.User;
@RestController
public class TestController {
@Autowired
UserRepository userRepository;
@GetMapping("/welcome")
public List<User> displayWelcomeMessage() {
List<User> list = new ArrayList<>();
userRepository.findAll().forEach(list::add);
return list;
}
}
我想我错过了一些配置。请指导。
你的application.properties
必须看起来像
#Tomcat Server Properties
server.port = 8099
#Oracle Database Properties
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE