org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘bookDaoImpl’
大家!
Java Eclipse Maven SpringMVC Hibernate Tomcat
我手动创建项目 https://www.boraji.com/index.php/spring-mvc-4-hibernate-5-restful-crud-operations-example 完全相同,它不能在我的 Eclipse 中工作(((因为:
1
|
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘bookDaoImpl’: Unsatisfied dependency expressed through field ‘sessionFactory’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘getSessionFactory’ defined in com.example.spring.config.AppConfig: Invocation of init method failed;
|
任何帮助,我必须为我的项目工作解决什么问题?现在 CRUD 方法不起作用,但服务器 Tomcat 正在运行…
我的项目在 https://github.com/OlegSandro/library-service 或者你可以在下面看到我的代码:
pom.xml:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example.spring</groupId> library–service</artifactId> <version>0.0.1–SNAPSHOT</version> <packaging>war</packaging> <properties> <dependencies> </dependencies> <build> <!– Embedded Apache Tomcat required for testing war –> |
model.Book.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.spring.model;
import javax.persistence.Entity; @Entity(name ="Book") @Id public Book() { public Long getId() { public void setId(Long id) { public String getTitle() { public void setTitle(String title) { public String getAuthor() { public void setAuthor(String author) { |
controller.BookController.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.spring.service;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.example.spring.dao.BookDao; @Service @Autowired @Transactional public Book get(long id) { @Transactional } @Transactional public List<Book> list() { } |
service.BookServiceImpl.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.example.spring.service;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.example.spring.dao.BookDao; @Service @Autowired @Transactional public Book get(long id) { @Transactional } @Transactional public List<Book> list() { } |
dao.BookDaoImpl.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.example.spring.dao;
import java.util.List; import javax.persistence.criteria.CriteriaBuilder; import org.hibernate.query.Query; import com.example.spring.model.Book; @Repository @Autowired public long save(Book book) { public Book get(long id) { public void update(long id, Book book1) { public void delete(long id) { public List<Book> list() { } |
config.WebConfig.java
1
2 3 4 5 6 7 8 9 10 11 12 13 |
package com.example.spring.config;
import org.springframework.context.annotation.ComponentScan; @Configuration } |
config.AppConfig.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.example.spring.config;
import java.util.Properties; import org.springframework.beans.factory.annotation.Autowired; @Configuration @Autowired @Bean Properties props = new Properties(); // Setting Hibernate properties // Setting C3P0 properties factoryBean.setHibernateProperties(props); return factoryBean; @Bean |
config.MyWebAppInitializer.java
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.example.spring.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override @Override @Override } |
resources.db.properties
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# MySQL properties mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/library mysql.user=root mysql.password=**** # Hibernate properties #C3P0 properties |
能够在本地重现此问题并修复它。
尝试将 mysql-connector-java 的 artifact 版本更新到最新版本
1
2 3 4 5 |
<dependency>
<groupId>mysql</groupId> mysql–connector–java</artifactId> <version>8.0.16</version> </dependency> |
如果您使用的是最新/更新的 MySQL 版本(除了帖子中提到的版本),这是必需的
相关问题:如何解决无法加载身份验证插件caching_sha2_password 问题
如果没有帮助,请提供完整的堆栈跟踪,否则接受解决方案。
顺便说一句,建议使用spring boot,它更简单,更高级。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267586.html