gpt4 book ai didi

java - 存储库不会 Beanify

转载 作者:行者123 更新时间:2023-12-02 10:13:16 25 4
gpt4 key购买 nike

我遇到一个无法追踪的错误。我是新人,如果我在搜索中错过了这个,我很抱歉,但我尝试了几件事,但没有运气。

UserApi.java:

package com.jsp.jsp;

import com.jsp.models.User;
import com.jsp.services.UserService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
class UserApi {
private final UserService userService;

public UserApi(UserService userService) {
this.userService = userService;
}

@RequestMapping(value="/api/user", method=RequestMethod.POST)
public User createUser(
@RequestParam(value="name", required=true) String name,
@RequestParam(value="email", required=true) String email,
@RequestParam(value="password", required=true) String password,
@RequestParam(value="confirm", required=true) String confirm)
{
User u = this.userService.createUser(new User(name, email, password));
return u;
}
}

UserRepository.java:

package com.jsp.repositories;

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import com.jsp.models.User;

import org.springframework.data.repository.CrudRepository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByEmail(String email);
Optional<User> findById(Long id);
List<User> findAll();
}

服务器.java

package com.jsp.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(scanBasePackages={"com.jsp.models","com.jsp.repositories","com.jsp.services"})
@RestController
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}

UserService.java

package com.jsp.services;

import java.util.List;

import com.jsp.models.User;
import com.jsp.repositories.UserRepository;

import org.springframework.stereotype.Service;

@Service
public class UserService {
private final UserRepository userRepository;

public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public List<User> allUsers() {
return this.userRepository.findAll();
}

public User createUser(User u) {
return this.userRepository.save(u);
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.jsp</groupId>
<artifactId>jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

简而言之,当我在 VSCode 中运行上述项目时,出现以下错误:

2019-02-23 19:07:52.744 WARN 25412 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in file [C:\Users\Alex\Documents\dojo\javatown\everything\target\classes\com\jsp\services\UserService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jsp.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我的理解是,我制作的存储库应该自动变成一个 bean,然后将其自身连接到我制作的服务。然而,这似乎并没有发生。我的示例视频使用的是 MySQL——这会有所不同吗?我是否使用了适合 Postgres 11 的正确驱动程序?我 super 迷失。

最佳答案

一些意见和建议:

1.在你的构造函数上添加 Autowiring 注释,更清楚你需要spring做什么。

@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

2.我不明白你的主类上的 @RestController 注释。

@SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
@RestController --> THIS CAN BE REMOVED
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}

3.您应该在 Spring Boot 应用程序上添加 @EnableJpaRepositories("com.jsp.repositories") => 它将扫描该包中的存储库。

 @SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
@EnableJpaRepositories("com.jsp.repositories")
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}

关于java - 存储库不会 Beanify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54848409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com