[我正在尝试运行我的新的Spring Boot项目,但是我知道自己无误地跟随了老师,因此我面临以下问题。
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-11-13 17:49:02.303 INFO 5340 --- [ restartedMain] c.m.G.GlobalProjectApplication : Starting GlobalProjectApplication on DESKTOP-K2GJJIF with PID 5340 (started by TAHA in C:\Users\TAHA\taha workspace\GlobalProject)
2019-11-13 17:49:02.303 INFO 5340 --- [ restartedMain] c.m.G.GlobalProjectApplication : No active profile set, falling back to default profiles: default
2019-11-13 17:49:02.429 WARN 5340 --- [ restartedMain] org.apache.tomcat.util.modeler.Registry : The MBean registry cannot be disabled because it has already been initialised
2019-11-13 17:49:02.461 INFO 5340 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-13 17:49:02.461 INFO 5340 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-13 17:49:02.461 INFO 5340 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-13 17:49:02.468 INFO 5340 --- [ restartedMain] o.a.c.c.C.[Tomcat-27].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-13 17:49:02.468 INFO 5340 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 164 ms
2019-11-13 17:49:02.473 WARN 5340 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mtaha.GlobalProject.Repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2019-11-13 17:49:02.473 INFO 5340 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2019-11-13 17:49:02.476 INFO 5340 --- [ restartedMain] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-11-13 17:49:02.527 ERROR 5340 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.mtaha.GlobalProject.Controllers.UserController required a bean of type 'com.mtaha.GlobalProject.Repositories.UserRepository' 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 'com.mtaha.GlobalProject.Repositories.UserRepository' in your configuration.
当我通过添加@EnableJpaRepositories
解决此问题时,出现此错误
Description:
Field userRepository in com.mtaha.GlobalProject.Controllers.UserController required a bean named '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 named 'entityManagerFactory' in your configuration.
这是我的代码:
User.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
private String email;
private Date birthDate;
private String userName;
private String password;
public User() {
super();
}
public User(String firstName, String lastName, String email, Date birthDate, String userName, String password) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.birthDate = birthDate;
this.userName = userName;
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserController.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mtaha.GlobalProject.Repositories.UserRepository;
import com.mtaha.GlobalProject.models.User;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public User getUser(@PathVariable long id) {
return null;
}
@GetMapping(path = "/")
public List<User> getAllUser() {
return null;
}
}
UserRepository.java
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.mtaha.GlobalProject.models.User;
public interface UserRepository extends JpaRepository<User, Long>{
}
和RepositoryConfiguration.java
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EntityScan(basePackages = {"com.mtaha.GlobalProject.models"})
@EnableJpaRepositories
@EnableTransactionManagement
public class RepositoryConfiguration {
}
application.properties
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=Global
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = update
结构:
src/main/java
|_com.mtaha.GlobalProject
|__GlobalProjectApplication.java
|__ServletInitialiser.java
|_com.mtaha.GlobalProject.Controllers
|__UserController.java
|_com.mtaha.GlobalProject.models
|__User.java
|_com.mtaha.GlobalProject.Repositories
|__UserRepository.java
|__RepositoryConfiguration.java
预先感谢
而且您在此链接中此问题中的POM.XML可能缺少某些依赖性或正在更新某些版本。
Spring Boot - repository field required a bean named 'entityManagerFactory' that could not be found