存储库中缺少bean

时间:2019-06-10 09:31:11

标签: java spring javabeans applicationcontext

我的应用程序无法启动,因为spring无法看到bean。

我正在尝试运行mu应用程序并将创建的用户添加到db。我不知道如何创建丢失的bean。我发现接口上方必须有注释@Repository,因此我将其放置了,但仍然无法正常工作。

用户:

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;

@Entity
public class User implements Serializable, UserInterface {

private static final long serialVersionUID = 8062231146287834334L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String login;
@Column
private String password;
@Column
private String email;
@Column
private Long phoneNumber;
@Column
private String firstName;
@Column
private String lastName;
@Column
private LocalDate dateOfBirth;
@Column
private String address;
@Column
private String city;
@Column
private String zipCode;

public User() {
    //constructor for hibernate
}

private User(String login, String password, String email, Long     phoneNumber, String firstName,
             String lastName, LocalDate dateOfBirth, String address,     String city, String zipCode) {

    this.login = login;
    this.password = password;
    this.email = email;
    this.phoneNumber = phoneNumber;
    this.firstName = firstName;
    this.lastName = lastName;
    this.dateOfBirth = dateOfBirth;
    this.address = address;
    this.city = city;
    this.zipCode = zipCode;
}

@Override
public String getLogin() {
    return login;
}

@Override
public String getPassword() {
    return password;
}

@Override
public String getEmail() {
    return email;
}

@Override
public Long getPhoneNumber() {
    return phoneNumber;
}

@Override
public String getFirstName() {
    return firstName;
}

@Override
public String getLastName() {
    return lastName;
}

@Override
public LocalDate getDateOfBirth() {
    return dateOfBirth;
}

@Override
public String getAddress() {
    return address;
}

@Override
public String getCity() {
    return city;
}

@Override
public String getZipCode() {
    return zipCode;
}

@Override
public String toString() {
    return "User{" +
            "login='" + login + '\'' +
            ", password='" + password + '\'' +
            ", email='" + email + '\'' +
            ", phoneNumber=" + phoneNumber +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", dateOfBirth=" + dateOfBirth +
            ", address='" + address + '\'' +
            ", city='" + city + '\'' +
            ", zipCode='" + zipCode + '\'' +
            '}';
}
public static class UserBuilder {

    private String login;
    private String password;
    private String email;
    private Long phoneNumber;
    private String firstName;
    private String lastName;
    private LocalDate dateOfBirth;
    private String address;
    private String city;
    private String zipCode;

    public UserBuilder setUserLogin(String login) {
        this.login = login;
        return this;
    }

    public UserBuilder setUserPassword(String password) {
        this.password = password;
        return this;
    }

    public UserBuilder setUserEmail(String email) {
        this.email = email;
        return this;
    }

    public UserBuilder setUserPhoneNumber(Long phoneNumber) {
        this.phoneNumber = phoneNumber;
        return this;
    }

    public UserBuilder setUserFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public UserBuilder setUserLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

    public UserBuilder setUserDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = LocalDate.parse(dateOfBirth);
        return this;
    }

    public UserBuilder setUserAddress(String address) {
        this.address = address;
        return this;
    }

    public UserBuilder setUserCity(String city) {
        this.city = city;
        return this;
    }

    public UserBuilder setUserZipCode(String zipCode) {
        this.zipCode = zipCode;
        return this;
    }

    public User build() {
        boolean isAllFielsdAreFull = login != null && password != null && email != null
                && phoneNumber != null && firstName != null
                && lastName != null && dateOfBirth != null && address != null
                && city != null && zipCode != null;

        if (isAllFielsdAreFull) {
            return new User(login, password, email, phoneNumber, firstName, lastName, dateOfBirth, address, city, zipCode);
        } else {
            throw new RuntimeException("Some fields are null!");
        }
    }

}

}

UserRepo:

package wawer.kamil.library;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import wawer.kamil.library.domain.User;

@Repository
public interface UserRepo extends JpaRepository<User, Long> {
}

控制器:

package wawer.kamil.library;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Controller;
import wawer.kamil.library.domain.User;

@Controller 
public class Start {

private UserRepo userRepo;

@Autowired
public Start(UserRepo userRepo) {
    this.userRepo = userRepo;
}

@EventListener(ApplicationReadyEvent.class)
public void run() {
    User user = new User.UserBuilder()
            .setUserLogin("Mareczek")
            .setUserPassword("Mareczek3@")
            .setUserEmail("mareczek@gmail.com")
            .setUserPhoneNumber(515791468L)
            .setUserFirstName("Marek")
            .setUserLastName("Kowalski")
            .setUserDateOfBirth("1990-12-12")
            .setUserAddress("Kowalska 12")
            .setUserCity("Lublin")
            .setUserZipCode("20-123")
            .build();

    userRepo.save(user);

}
}


spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://0.0.0.0:32768/library
?serverTimezone=Europe/Warsaw
spring.datasource.username=root
spring.datasource.password=root

主类:

package wawer.kamil.library;

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

@SpringBootApplication
public class LibraryApplication {

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

}

错误日志:

Error starting ApplicationContext. To display the conditions report re-run     your application with 'debug' enabled.
2019-06-10 11:19:30.771 ERROR 6282 --- [           main]     o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in wawer.kamil.library.Start required a bean of type 'wawer.kamil.library.UserRepo' that could not be found.


Action:

Consider defining a bean of type 'wawer.kamil.library.UserRepo' in your configuration.


Process finished with exit code 1

我想运行我的应用程序,并将用户添加到数据库。

1 个答案:

答案 0 :(得分:0)

您需要启用存储库

@SpringBootApplication
@EnableJpaRepositories // this will fix the issue
public class LibraryApplication {

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