Я использую SpringBoot вместе с JPA и Vaadin-flow. Я получаю эту ошибку при попытке сохранить данные формы.
Я пытаюсь добавить аннотацию @Repository в класс CustomerRepository, но она все еще не работает. Может кто-нибудь, пожалуйста, помогите мне в этом.
Customer.java
@Entity
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id", nullable = false, unique = true)
private Long id;
private String firstName;
private String lastName;
protected Customer() {
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//Getters ans Setters
}
CustomerEditor.java
@Route(value = "NewPickUp", layout = MainView.class)
@SpringComponent
@UIScope
public class CustomerEditor extends VerticalLayout{
@Autowired
private final CustomerRepository repository;
@Autowired
private Customer customer;
TextField firstName = new TextField("First name");
TextField lastName = new TextField("Last name");
Button save = new Button("Save", VaadinIcon.CHECK.create());
Binder<Customer> binder = new Binder<>(Customer.class);
@Autowired
public CustomerEditor(CustomerRepository repository) {
this.repository = repository;
add(firstName, lastName, save );
binder.forField(firstName).bind(Customer::getFirstName, Customer::setFirstName);
binder.forField(lastName).bind(Customer::getLastName, Customer::setLastName);
save.addClickListener(e -> save());
}
void save() {
repository.save(customer);
}
}
CustomerRepository.java
public interface CustomerRepository extends JpaRepository<Customer, Long> {
List<Customer> findByLastNameStartsWithIgnoreCase(String lastName);
}
MainView.java
@Route
public class MainView extends AppLayout{
public MainView() {
Button addButton = new Button(new RouterLink("", CustomerEditor.class));
RouterLink routerLink = new RouterLink("NewPickUp", CustomerEditor.class);
routerLink.getElement().appendChild(addButton.getElement());
this.addToNavbar(routerLink);
}
Сообщение об ошибке
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.apt.lts.view.CustomerEditor': Unsatisfied dependency expressed through field 'customer'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.apt.lts.model.Customer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:305) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at com.vaadin.flow.spring.SpringInstantiator.getOrCreate(SpringInstantiator.java:117) ~[vaadin-spring-12.0.5.jar:na]
at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:158) ~[flow-server-2.0.11.jar:2.0.11]
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:127) ~[flow-server-2.0.11.jar:2.0.11]
at java.base/java.util.Optional.orElseGet(Optional.java:369) ~[na:na]