我正试图使用JpaRepository将数据推送到我的数据库中,但有一些错误阻止了我这样做


*************************** APPLICATION FAILED TO START ***************************
Description:
Field userRepository in com.Lex.Exercise.Service.RegistrationService required a bean of type 'com.Lex.Exercise.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.Lex.Exercise.Repository.UserRepository' in your configuration.

//用户存储库

package com.Lex.Exercise.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.Lex.Exercise.Model.UserEntity;
public interface UserRepository extends JpaRepository<UserEntity, String>{
}

//注册服务

@ComponentScan(basePackages = "com.Lex.Exercise.Repository")
@Component
public class RegistrationService {  
@Autowired
private UserRepository userRepository;
//business methods and other validations comes here
}

这是主要的类

package com.Lex.Exercise.SpringBootDemo;
@SpringBootApplication
@PropertySource(value = { "classpath:configuration.properties" })
public class SpringBootDemoApplication implements CommandLineRunner {
@Autowired
private RegistrationService service;
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}

以下是项目结构:/SpringBootDemo/src/main/java.com/Lex/Exercise/Model/User.java/SpringBootDemo/src/main/java.com/Lex/Exercise/Model/UserEntity.java/SpringBootDemo/src/main/java.com/Lex/Exercise/Restore/UserRepository.java/SpringBootDemo/src/main/java.com/Lex/Exercise/Service/RegistrationService.java/SpringBootDemo/src/main/java.com/Lex/Exercise/SpringBootDemo/SpringBootDemoApplication.java/SpringBootDemo/src/main/resources/application.properties/SpringBootDemo/src/main/resources/configuration.properties

请帮助我如何解决此

删除RegistrationService.class上的@ComponentScan(scanBasePackages = "com.Lex.Exercise.Repository")

你的SpringBootDemoApplication.class应该是这样的。然后com.Lex.Exercise子包中的组件将被注入到应用程序上下文中。

@SpringBootApplication
@ComponentScan(basePackages = {"com.Lex.Exercise"})
public class SpringBootDemoApplication{
public static void main(String[] args) {
new SpringApplication(SpringBootDemoApplication.class).run(args);
}
}

一般来说,将SpringApplication放在顶级包中是个好主意(就像您的案例com.Lex.Exercise(,因为SpringBoot会自动扫描该类的所有子包。所以你不需要额外的@ComponentScan

@ComponentScan(basePackages = "com.Lex.Exercise.Repository")属于您的配置,可能是您的应用程序类

相关内容

最新更新