如何解决Vaadin Spring Boot @autowired字段在视图中返回null
首先,由于我是Spring Boot生态系统的新手,请原谅我的问题。在我的应用程序中,我有一个vaadin页面,我想在其中使用存储库向DB提交用户详细信息。在我的视图类中,我将它们添加为@autowired字段,但是,在运行时,我看到它们的值已运行,因此操作失败。我知道要受益于@autowired,不应在构造过程中重新创建实例,但我不知道该如何独自完成。这是我的课程:
@SuppressWarnings("serial")
public class LoginAwareComposite extends Composite<Div> {
@Autowired
private ApplicationEventPublisher publisher;
public LoginAwareComposite() {
}
@Override
protected void onAttach(AttachEvent event) {
super.onAttach(event);
UserCredentials userPrincipal = UI.getCurrent().getSession().getAttribute(UserCredentials.class);
if (userPrincipal != null) {
// --- NOT LOGGED IN
UI.getCurrent().navigate(AddressBookManagementView.class);
}
}
}
@SuppressWarnings("serial")
@Route(value = "account")
@Theme(value = Lumo.class,variant = Lumo.LIGHT)
public class AddressBookManagementView extends LoginAwareComposite {
private VerticalLayout pageLayout = new VerticalLayout();
public AddressBookManagementView() {
getContent().setSizeFull();
getContent().add(initPage());
}
private Component initPage() {
pageLayout.getStyle().set("padding-left","0px");
pageLayout.getStyle().set("padding-bottom","0px");
pageLayout.getStyle().set("padding-right","0px");
pageLayout.getStyle().set("overflow","auto");
pageLayout.setSizeFull();
pageLayout.add(new HeaderLayout(),new BodyLayout(),new FooterLayout());
return pageLayout;
}
}
@SuppressWarnings("serial")
@SpringComponent
public class BodyLayout extends VerticalLayout {
// some fields
@Autowired
EmailRepository emailRepository;
@Autowired
FaxRepository faxRepository;
public BodyLayout() {
init(); //this function inits the view,and eventually inits the on click event for submit button,which then calls my function
}
private void myFunction() {
//here i use the repository entities but they do return null although they are autowired
}
所以发生的是,在BodyLayout的构造函数中,我们调用了init()函数,该函数用于初始化布局并提供功能按钮等,init方法中的子功能之一提供了使用myFunction提交按钮的功能。 MyFuction使用存储库实体,但它返回null。
解决方法
由于您将vaadin与springboot一起使用,请确保以下几点:
- 确保在
@Repository
之类的存储库界面上使用EmailRepository
批注。 - 尝试对您的存储库类使用构造函数注入:
尝试如下:
@SuppressWarnings("serial")
@SpringComponent
@UIScope
public class BodyLayout extends VerticalLayout {
// some fields
private final EmailRepository emailRepository;
private final FaxRepository faxRepository;
@Autowired
public BodyLayout(EmailRepository emailRepository,FaxRepository faxRepository) {
this.emailRepository = emailRepository;
this.faxRepository = faxRepository;
init(); //this function inits the view,and eventually inits the on click event for submit button,which then calls my function
}
private void myFunction() {
//here i use the repository entities but they do return null although they are autowired
}
,
我能够使其按以下方式工作:
@SuppressWarnings("serial")
@Route(value = "account")
@Theme(value = Lumo.class,variant = Lumo.LIGHT)
@UIScope
@SpringComponent
public class AddressBookManagementView extends LoginAwareComposite {
private VerticalLayout pageLayout = new VerticalLayout();
@Autowired
BodyLayout bodyLayout;
public AddressBookManagementView(BodyLayout bodyLayout) {
this.bodyLayout = bodyLayout;
getContent().setSizeFull();
getContent().add(initPage());
}
private Component initPage() {
pageLayout.getStyle().set("padding-left","0px");
pageLayout.getStyle().set("padding-bottom","0px");
pageLayout.getStyle().set("padding-right","0px");
pageLayout.getStyle().set("overflow","auto");
pageLayout.setSizeFull();
pageLayout.add(new HeaderLayout(),bodyLayout,new FooterLayout());
return pageLayout;
}
那么BodyLayout是
@SuppressWarnings("serial")
@UIScope
@SpringComponent
public class BodyLayout extends VerticalLayout {
private final EmailRepository emailRepository;
private final FaxRepository faxRepository;
@Autowired
public BodyLayout(EmailRepository emailRepository,FaxRepository faxRepository) {
this.emailRepository = emailRepository;
this.faxRepository = faxRepository;
init();
}
,
大约只有@Route
,布局和vaadin初始化侦听器参与自动依赖项注入(即:vaadin spring集成要求spring上下文来构建它们)。如果您执行new MyClass()
,则从不参与DI。将基于字段的注入与@Autowired
一起使用会隐藏此问题-因此,使用基于构造函数的注入是“行业标准”。另一种方法是,如果您想参加DI,但要求spring上下文为您构建实例,则不要构建自己的实例。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。