英文:
Field dao in com.car.services.CarServices required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found
问题
我正在尝试学习Spring Boot基本应用程序,并无法解决这个错误
***************************
应用程序启动失败
***************************
描述:
com.car.services.CarServices中的字段dao需要找到类型为'javax.persistence.EntityManagerFactory'的bean,但找不到。
注入点具有以下注释:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
操作:
考虑在您的配置中定义类型为'javax.persistence.EntityManagerFactory'的bean。
英文:
Im trying to learn spring-boot basic application and unable to solve this error
APPLICATION FAILED TO START
Description:
Field dao in com.car.services.CarServices required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.
repository file
@SuppressWarnings("unused")
@Transactional
@Repository
public class CarDAO implements ICarDAO {
@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
@Override
public List<Car> getCars() {
//String hql = "FROM Car ";
String hql = "FROM Car as a ORDER BY a.id DESC";
return (List<Car>) entityManager.createQuery(hql).getResultList();
}
@Override
public Car getCar(int carId) {
return entityManager.find(Car.class, carId);
}
}
my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.car</groupId>
<artifactId>car</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>car-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service file
@Service
public class CarServices implements ICarServices {
@Autowired
private ICarDAO dao;
@Override
public List<Car> getCars() {
return dao.getCars();
}
@Override
public Car getCars(int carId) {
return dao.getCar(carId);
}
}
Controller file
@RestController
@RequestMapping("carservice")
public class CarController {
@Autowired
private ICarServices service;
@GetMapping("car")
public ResponseEntity<List<Car>> getCars(){
List<Car> cars = service.getCars();
return new ResponseEntity<List<Car>>(cars, HttpStatus.OK);
}
}
main function
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class CarDemoApplication {
public static void main(String[] args) {
SpringApplication.run(CarDemoApplication.class, args);
System.out.println("hello from car");
}
}
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/cardb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings = false
spring.jpa.properties.hibernate.format_sql = true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
I tried
import org.springframework.beans.factory.annotation.Autowired;
in my repository.java file but it didnt work for me.
答案1
得分: 0
你需要将 DataSourceAutoConfiguration.class
从排除项中移除。这是用来配置 EntityManagerFactory
的部分。
所以原先的代码:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
// 主类定义
应该改成:
@SpringBootApplication()
// 主类定义
英文:
You need to remove DataSourceAutoConfiguration.class from exclusions. That is what sets up the EntityManagerFactory.
So this:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
// Main class definition
Should be:
@SpringBootApplication()
// Main class definition
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论