spring-boot - 使用 SprinBoot + Jpa + vaadin 保存表单数据时的问题
问题描述
我正在使用 SpringBoot 以及 JPA 和 Vaadin-flow。尝试保存表单数据时出现此错误。
我尝试在 CustomerRepository 类中添加 @Repository 注释,但它仍然无法正常工作。有人可以帮我解决这个问题。
客户.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
}
客户编辑器.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);
}
主视图.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]
解决方案
正如 Debendra Dhinda 在他们的回答中提到的那样,您的Customer
类不是 Spring 组件,因此不能被注入。您可以不注入客户,而是在视图的构造函数中创建新客户,或者您可以@Bean
在类中添加定义方法@Configuration
。这两种方法的共同点是你new Customer()
在某个时候调用。
// code example for approach 2
@Configuration
public class MySpringConfiguration {
// this method will be called if you inject/autowire a Customer somewhere.
@Bean
public Customer getCustomer(){
return new Customer();
}
}
推荐阅读
- javascript - ReactJS - 使我的自制幻灯片响应和图像间隙校对
- kendo-grid - 当在 kendo.grid 上创建列时,是否可以实例化对象?
- php - 有没有办法在电报机器人中发送带有消息的回调按钮?
- java - Maven 构建无法从本地存储库中的 JAR 中找到符号和包
- c# - 以编程方式训练和接受 QnA 中添加的问题
- python - 基于空字符串切片python列表
- visual-studio - 如何在 Visual Studio 2019 中配置代理?
- python - 为什么原始类型不使用“is”比较代替“==”?
- javascript - 如何在javascript中打印元素异步推送到它的数组?
- d3.js - getter mettod .attr() 是否会修改与元素关联的数据?