spring Java Sping Boot 问题LoggiFailureAnalysisReporter

2eafrhcq  于 5个月前  发布在  Spring
关注(0)|答案(2)|浏览(64)

很明显,我是Boot-Spring的新手,在这个案例中,我主要是从youtube上复制一些代码。然而,经过修改,最后,我得到了这样一条消息:
产品描述:
找不到com.example.demo.BlogController中的字段postService需要类型为“Server.PostService”的Bean。
行动:
考虑在配置中定义一个类型为“Server.PostService”的bean。
.....任何想法如何处理这种情况.感谢您的支持. 1stclass-BlogAppl ciation-com.example.demo(package)2nd-Blog Controller-same package as BlogApplication 3rdclass-Post-entities 4 rthclass-PostRepositories-Repositories

**package com.example.demo;  

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;
  

@SpringBootApplication

public class BlogApplication {

public static void main(String[] args) {

    SpringApplication.run(BlogApplication.class, args);
}

字符串
}**

**package com.example.demo;
  import java.util.List;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 import Server.PostService;
 import entities.Post;
 import java.util.Date;
  @RestController
  public class BlogController {
  @Autowired
  private PostService postService;
  @GetMapping(value="/")

  public String index() {
    return "index";
    }
  @GetMapping(value="/posts")
  public List<Post>posts(){
  return postService.getAllPosts();
 
    }
  @PostMapping(value="/post")
  public void publishPost(@RequestBody Post post) {
  
   if(post.getDatecreation() == null) 
  post.setDatecreation(new Date());
      postService.insert(post);  
     }
     }**
 

  **package entities;
    import java.util.Date;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    @Entity
     public class Post {
      @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
     private Long id;
     private String title;
    private String body;
    private Date Datecreation;

    public Post() {
    
     }
    public long getId() {
    return id;
     }
    public void setId(long id) {
    this.id = id;
    }   
    public String gettitle() {
    return title;
     }
    public void settitle(String title) {
     this.title= title;
     }
    public String getBody() {
    return body;
     }
    public void setBody(String body) {
    this.body = body;
     }
    public Date getDatecreation() {
    return Datecreation;
    }
    public void setDatecreation(Date datecreation) {
    this.Datecreation = datecreation;
    }
    }**

   **package Repositories;

   import org.springframework.data.jpa.repository.JpaRepository;
   import org.springframework.stereotype.Repository;
   import entities.Post;
   @Repository
   public interface PostRepository extends JpaRepository<Post,Long>{
    }**

wz8daaqr

wz8daaqr1#

您的BlogApplication Class,也就是用@SpringBootApplication注解的class,位于包com.example.demo中。这意味着,默认情况下,Spring将从该包开始启动组件扫描。
问题是,您的类PostService和接口PostRepositorycom.example.demo不在同一个包中(或在com.example.demo的子包中),因此Spring无法找到它们,也不会自动为您创建这些bean。
若要更正此问题,请移动您在根软件包(com.example.demo)中创建的软件包。
您可以找到有关使用@SpringBootApplicationhere的更多信息。

cunj1qz1

cunj1qz12#

编辑:
您缺少PostService类或导入了错误的类作为Server.PostService。
尝试创建这样的服务:

@Component 
public class PostService {
    public List<Post> getAllPosts(){
     //your code
    }
}

字符串

相关问题