字段..在里面需要类型为..的bean..找不到错误



我来找你是最后的希望,我是编程新手,在使用Spring和Gradle启动我的项目时遇到了麻烦。每次我试图启动我的项目时,我都会遇到一个错误,我不明白为什么。。。

这是错误

o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-10 00:08:24.364  INFO 16456 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-08-10 00:08:24.383  INFO 16456 --- [  restartedMain] o.a.c.c.C.[Tomcat-8].[localhost].[/]     : Initializing Spring embedded WebApplicationContext
2022-08-10 00:08:24.383  INFO 16456 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 221 ms
2022-08-10 00:08:24.388  WARN 16456 --- [  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 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.TechTicketing.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-08-10 00:08:24.388  INFO 16456 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-08-10 00:08:24.393  INFO 16456 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-10 00:08:24.395 ERROR 16456 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userRepository in com.example.TechTicketing.service.UserService required a bean of type 'com.example.TechTicketing.repository.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.example.TechTicketing.repository.UserRepository' in your configuration.

我搜索了很多关于这个错误的信息,但我找到的每一个解决方案似乎都不起作用,我检查了我的包处理情况,它看起来很好:

包装处理

我确实在类中使用了注释@Service、@RestController、@Entity、@Repository,我确实尝试删除了@Autowired,应用程序启动了,但一旦我发出get请求,它就会给出一个空指针,所以基本上是一样的问题。。。我试着修改我的主类配置中的选项,但要么它没有改变任何东西,要么它崩溃了。。。我尝试了在网上找到的所有选项,但无法解决问题。这是我的一些代码。

用户实体


import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
@Entity
@Data
@Table(name="User")
public class UserEntity {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long IdUser;
private String Name;
private String FirstName;
private String Mail;
private String Password;
private String Phone;
private String Address;
private String TechCode;
private int Role;
@JsonIgnore
@OneToMany(mappedBy="IdInternalInter")
private List<InterventionEntity> intervention;
@JsonIgnore
@ManyToOne
@JoinColumn(name="IdRole")
private RoleEntity role;
}

用户存储库


import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.TechTicketing.entity.UserEntity;
@Repository
public interface UserRepository extends CrudRepository<UserEntity, Long>{
}

用户控制器


import java.util.Optional;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.TechTicketing.entity.UserEntity;
import com.example.TechTicketing.service.UserService;
@RestController
public class UserController {

@Autowired
private UserService userService;

@GetMapping("/user/{id}")
public UserEntity getUserById(@PathVariable("id")final Long id) {
Optional<UserEntity> user = userService.getUserById(id);
if(user.isPresent()) {
return user.get();
} else {
return null;
}
}

@GetMapping("/users")
public Iterable<UserEntity> getUsers(){
return userService.getUsers();
}

@PostMapping("/addUser")
public UserEntity addUser(@RequestBody UserEntity user) {
return userService.addUser(user);
}

@PutMapping("/updateUser")
public UserEntity updateUser(@RequestBody UserEntity user) {
return userService.updateUser(user);
}
}

用户服务


import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.TechTicketing.entity.UserEntity;
import com.example.TechTicketing.repository.UserRepository;
import lombok.Data;
@Service
@Data
public class UserService {

@Autowired
private UserRepository userRepository;

public Optional<UserEntity> getUserById(final Long userID){
return userRepository.findById(userID);
}

public Iterable<UserEntity> getUsers(){
return userRepository.findAll();
}

public UserEntity addUser(UserEntity user) {
UserEntity addedUser = userRepository.save(user);
return addedUser;
}

public UserEntity updateUser(UserEntity user){
UserEntity existingUser = userRepository.findById(user.getIdUser()).orElse(null);
existingUser.setAddress(user.getAddress());
existingUser.setFirstName(user.getFirstName());
existingUser.setIntervention(user.getIntervention());
existingUser.setMail(user.getMail());
existingUser.setName(user.getName());
existingUser.setPassword(user.getPassword());
existingUser.setPhone(user.getPhone());
existingUser.setRole(user.getRole());
existingUser.setTechCode(user.getTechCode());
return userRepository.save(existingUser);

}
}

TechTicketingApp


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.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.Configuration;
//@ComponentScan("com.example.TechTicketing.repository")
@Configuration
@EnableAutoConfiguration(
exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@SpringBootApplication
public class TechTicketingApplication {
public static void main(String[] args) {
SpringApplication.run(TechTicketingApplication.class, args);
}

}

build.gradle

id 'org.springframework.boot' version '2.7.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'war'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}

如果你需要更多,请告诉我,如果需要,我最终也可以提供github文件夹的链接!提前感谢!

如果不是spring数据,则看起来UserRepository没有用@Repository进行注释如果春季数据试图添加到以下

@EnableJpaRepositories("com.example.TechTicketing.repository") @EntityScan(basePackages="com.example.TechTicketing.entity")

相关内容

最新更新