每当我将 @Component 添加到我的 Spring 程序时,启动都不会启动

Posted

技术标签:

【中文标题】每当我将 @Component 添加到我的 Spring 程序时,启动都不会启动【英文标题】:Whenever I add @Component to my Spring program the boot don't start 【发布时间】:2019-12-26 17:00:45 【问题描述】:

我是 Spring 新手,我一直在谈论 Spring Framework 5: Beginner to Guru 课程,每当我将 Componet 添加到我的代码中时,整个启动都不会启动 这个用于在数据库中存储书籍和作者姓名的简单代码

这是我的主线

package guru.springframework.spring5webapp;

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication 
public class Spring5webappApplication 

    public static void main(String[] args) 
        SpringApplication.run(Spring5webappApplication.class, args);    
        


这是我的数据库代码,我正在使用 h2

package guru.springframework.spring5webapp.bootstrap;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import guru.springframework.spring5webapp.domain.Author;
import guru.springframework.spring5webapp.domain.Book;
import guru.springframework.spring5webapp.repositories.AuthorRepository;
import guru.springframework.spring5webapp.repositories.BookRepository;

 @Component
 class DevBootstrap implements ApplicationListener<ContextRefreshedEvent> 

    private AuthorRepository authorRepository;
    private BookRepository bookRepositroy;

    public DevBootstrap(AuthorRepository authorRepository, BookRepository bookRepositroy) 
        super();
        this.authorRepository = authorRepository;
        this.bookRepositroy = bookRepositroy;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) 
        initdata();

    
    private void initdata() 

        //Mostafa
        Author Mostafa = new Author("Mostafa", "Desoky");
        Book Farm = new Book("How to farm101","135","Feeders");
        Mostafa.getBooks().add(Farm);
        Farm.getAuthors().add(Mostafa);
        authorRepository.save(Mostafa);
        bookRepositroy.save(Farm);

        //Mahmoud
        Author Mahmoud = new Author("Mahmoud", "Ragab");
        Book  EM = new Book("El3ol2ya mas2olya", "22485", "ElGomhorya");
        Mahmoud.getBooks().add(EM);
        EM.getAuthors().add(Mahmoud);
        authorRepository.save(Mahmoud);
        bookRepositroy.save(EM);

    


我的作者代码

package guru.springframework.spring5webapp.domain;

import javax.persistence.*;
import java.util.Set;


@Entity
public class Author 

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;

    @ManyToMany(mappedBy = "authors")
    private Set<Book> books;



    public Author(String firstName, String lastName) 
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    

    public Author(String firstName, String lastName, Set<Book> books) 
        this.firstName = firstName;
        this.lastName = lastName;
        this.books = books;
    

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getFirstName() 
        return firstName;
    

    public void setFirstName(String firstName) 
        this.firstName = firstName;
    

    public String getLastName() 
        return lastName;
    

    public void setLastName(String lastName) 
        this.lastName = lastName;
    

    public Set<Book> getBooks() 
        return books;
    

    public void setBooks(Set<Book> books) 
        this.books = books;
    

    @Override
    public String toString() 
        return "Author [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", books=" + books + "]";
    

    @Override
    public int hashCode() 
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    

    @Override
    public boolean equals(Object obj) 
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Author other = (Author) obj;
        if (id == null) 
            if (other.id != null)
                return false;
         else if (!id.equals(other.id))
            return false;
        return true;
    


我的书码

package guru.springframework.spring5webapp.domain;

import javax.persistence.*;
import java.util.Set;

@Entity
public class Book 

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;
    private String isbn;
    private String publisher;

    @ManyToMany
    @JoinTable(name = "author_book", joinColumns = @JoinColumn(name = "book_id"),
            inverseJoinColumns = @JoinColumn(name = "author_id"))
    private Set<Author> authors;


    public Book(String title , String isbn, String publisher) 
        super();
        this.publisher = publisher;
        this.title = title;
        this.isbn = isbn;
    

    public Book(String title, String isbn,String publisher, Set<Author> authors) 
        this.publisher = publisher;
        this.title = title;
        this.isbn = isbn;
        this.authors = authors;
    

    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 getIsbn() 
        return isbn;
    

    public void setIsbn(String isbn) 
        this.isbn = isbn;
    


    public String getPublisher() 
        return publisher;
    

    public void setPublisher(String publisher) 
        this.publisher = publisher;
    

    public Set<Author> getAuthors() 
        return authors;
    

    public void setAuthors(Set<Author> authors) 
        this.authors = authors;
    

    @Override
    public String toString() 
        return "Book [id=" + id + ", title=" + title + ", isbn=" + isbn + ", publisher=" + publisher + ", authors="
                + authors + "]";
    

    @Override
    public int hashCode() 
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    

    @Override
    public boolean equals(Object obj) 
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Book other = (Book) obj;
        if (id == null) 
            if (other.id != null)
                return false;
         else if (!id.equals(other.id))
            return false;
        return true;
    


最后我的作者和书籍存储库扩展了 CrudRepository

package guru.springframework.spring5webapp.repositories;

import org.springframework.data.repository.CrudRepository;
import guru.springframework.spring5webapp.domain.Author;

public interface AuthorRepository extends CrudRepository<Author, Long> 


package guru.springframework.spring5webapp.repositories;

import org.springframework.data.repository.CrudRepository;
import guru.springframework.spring5webapp.domain.Book;

public interface BookRepository extends CrudRepository<Book, Long 


堆栈跟踪

spring5webapp - Spring5webappApplication [Spring Boot App]  
    guru.springframework.spring5webapp.Spring5webappApplication at localhost:59942  
        Thread [main] (Suspended (uncaught exception NullPointerException)) 
            ReflectionUtils.rethrowRuntimeException(Throwable) line: 142    
            SpringApplication.handleRunFailure(ConfigurableApplicationContext, Throwable, Collection<SpringBootExceptionReporter>, SpringApplicationRunListeners) line: 810 
            SpringApplication.run(String...) line: 325  
            SpringApplication.run(Class<?>[], String[]) line: 1226  
            SpringApplication.run(Class<?>, String...) line: 1215   
            Spring5webappApplication.main(String[]) line: 12    
    C:\Program Files\Java\jdk-13.0.1\bin\javaw.exe (Dec 26, 2019, 7:42:44 PM)   

【问题讨论】:

请添加错误堆栈跟踪 我在这里看到了几个问题。您的存储库类中没有@Repository,添加它们后,请尝试在您的springboot 主应用程序类中添加@EnableJpaRepositories("some.root.package") @SantosshKumhar 我这样做了,但没有发生任何事情,仍然是同样的错误 请发布您的堆栈跟踪,顺便说一句 sme.root.package 应替换为 guru.springframework.spring5webapp.repositories 我添加了堆栈跟踪,如果你有时间帮助我,这是我的项目的 git github.com/RasLGhoul/Spring5 【参考方案1】:

问题是我必须在书籍和作者代码中初始化书籍和作者

private Set<Book> books = new HashSet<>();

【讨论】:

以上是关于每当我将 @Component 添加到我的 Spring 程序时,启动都不会启动的主要内容,如果未能解决你的问题,请参考以下文章

为啥每当我尝试将图像添加到我的 django 项目时都会出错?

当我将值从服务传递到我的角度组件时,结果错误

当我将 AVCaptureVideoPreviewLayer 添加到我的 UIView 时,UIButton 消失

如果我将它作为子视图添加到我的 UIPickerView,是不是需要向我的 UIToolBar 添加约束?

当我将新闻添加到我的 Firebase 时,有啥方法可以自动发送推送通知

当我将它添加到我的 html 文档时,向上/向下投票系统不起作用