java - .... Spring Boot 中构造函数的参数 0
问题描述
我是 Spring Boot 和 Data Jpa 中的一条鱼,我尝试创建一个基本的 Spring Boot 应用程序,但每次遇到错误。你能帮助我吗?
那是我的代码: Spring Boot Application 类:
@SpringBootApplication
@ComponentScan(basePackages = "com.project.*")
@EnableJpaRepositories(basePackages = "com.project.repository.*")
@EntityScan(basePackages = "com.project.entities.*")
@EnableAutoConfiguration
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
控制器类:
@RestController
@RequestMapping(value = "/api")
public class controller {
private IUserServices userServices;
@Autowired
public controller(IUserServices userServices) {
this.userServices = userServices;
}
@GetMapping(value = "/merhaba")
public String sayHello(){
return "Hello World";
}
@GetMapping(value = "/getall")
public List<User> getAll(){
return this.userServices.getAllUsers();
}
}
存储库类:
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
服务类:
@Service
public interface IUserServices {
void saveUser(User user);
List<User> getAllUsers();
}
ServicesImpl 类:
@Service
public class UserServicesImpl implements IUserServices{
private UserRepository userRepository;
@Autowired
public UserServicesImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void saveUser(User user) {
this.userRepository.save(user);
}
@Override
public List<User> getAllUsers() {
return this.userRepository.findAll();
}
}
实体类:
@Entity
@Table(catalog = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
这是我的错误信息:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.project.services.UserServicesImpl required a bean of
type 'com.project.repository.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.project.repository.UserRepository' in your
configuration.
Process finished with exit code 0
所以这是应用程序属性文件:
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/u
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.javax.persistence.validation.mode = none
解决方案
有一些问题你应该解决它们。
第一的
当您拥有带有@SpringBootApplication的spring boot 应用程序时,您不需要诸如此类的其他东西@EnableAutoConfiguration
,因此将它们全部删除。你可以在这里
阅读更多关于它的信息。
第二
你不需要用@Service 来注释你的服务接口,因为你是在UserServicesImpl
课堂上做的。
第三
您在用户实体中定义id
为整数,但在存储库中,您将 id 写为Long
. 这是错的。它应该是这样的。
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
尝试上述解决方案并让我知道结果。
推荐阅读
- javascript - 有没有办法使用 SurveyJS 将调查结果保存为 Json 或文本文件?
- python - 学习率对收敛速度没有影响?
- python - 如何打印列表中每个元素的第一个字母
- python - 如何将固定的 subplot2grid 排列的倍数绘制成一个图形?
- python - 当 Pandas 在多个带有颜色标签的子图中创建散点图时,无法设置 xlabel
- javascript - Need mmenu to react differently on desktops and mobile devices
- css - MaterializeCSS collapsible with IMAGE
- npm - 找不到模块“指挥官”
- algorithm - 堆栈是否是数据结构?
- git - Libgit2sharp, Pushing a specific branch to new remote