仓库不会Bean化

时间:2019-02-24 03:21:29

标签: java spring-boot spring-data-jpa

我有一个无法跟踪的错误。我是新来的,如果在搜索中错过了此内容,对不起,但是我尝试了几件事,没有运气。

UserApi.java:

    public ActionResult addEditProduct()
    {
        return View();
    }

    [HttpPost]
    public ActionResult addEditProduct(EditProductModel model)
    {
        if (!model.HasCategories)
        {
            ModelState.AddModelError(string.Empty, "A category is required.");
            return View(new { id = model.P.ID });
        }

        if (!ModelState.IsValid())
        {
            return View(new { id = model.P.ID });
        }
    }

UserRepository.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;
    }
}

Server.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();
}

UserService.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);
    }
}

pom.xml

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);
    }
}

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

  

2019-02-23 19:07:52.744 WARN 25412 --- [restartedMain]   ConfigServletWebServerApplicationContext:遇到异常   在上下文初始化期间-取消刷新尝试:   org.springframework.beans.factory.UnsatisfiedDependencyException:   在文件中定义名称为“ userService”的bean创建时出错   [C:\ Users \ Alex \ Documents \ dojo \ javatown \ everything \ target \ classes \ com \ jsp \ services \ UserService.class]:   通过构造函数参数0表示的不满足的依赖关系;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型为“ com.jsp.repositories.UserRepository”的合格Bean   可用:至少有1个符合自动装配条件的bean   候选人。依赖项注释:{}

我的理解是,我创建的存储库应该自动转换为Bean,然后将其自身连接到我创建的服务。但是,这似乎没有发生。我的示例视频使用MySQL-会有所作为吗?我在为Postgres 11使用正确的驱动程序吗?我迷路了。

1 个答案:

答案 0 :(得分:1)

一些评论和建议:

1。在构造函数上添加自动装配的注释,可以更清楚地了解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);
    }
 }