Java 在Vaadin和Spring中使用@autowired的NullPointerException
我正在瓦丁8号和春天进行简单的图书库项目。我正在努力将服务类注入到UI组件中。它一直给我nullPointerExceptionMessage。我不会在任何地方使用“新建”来创建服务 以下是图书馆服务类:Java 在Vaadin和Spring中使用@autowired的NullPointerException,java,spring,nullpointerexception,vaadin,autowired,Java,Spring,Nullpointerexception,Vaadin,Autowired,我正在瓦丁8号和春天进行简单的图书库项目。我正在努力将服务类注入到UI组件中。它一直给我nullPointerExceptionMessage。我不会在任何地方使用“新建”来创建服务 以下是图书馆服务类: @Service public class LibraryService { @Autowired private BookDao bookDao; @Autowired private LibraryDao libraryDao; priv
@Service
public class LibraryService {
@Autowired
private BookDao bookDao;
@Autowired
private LibraryDao libraryDao;
private static LibraryService libraryServiceInstance;
private LibraryService() {
}
public static LibraryService getInstance() {
if (libraryServiceInstance == null) {
synchronized (LibraryService.class) {
if (libraryServiceInstance == null) {
libraryServiceInstance = new LibraryService();
}
}
}
return libraryServiceInstance;
}
public void saveBook(Book book) {
libraryDao.findAll().forEach(l -> l.getBooks().add(book));
book.setLibrary(libraryDao.findOne(1L));
bookDao.save(book);
}
public List<Book> getAllBooks() {
List<Book> bookList = new ArrayList<>();
libraryDao.findAll().forEach(l -> l.getBooks().forEach(b -> bookList.add(b)));
return bookList;
}
}
我将非常感谢您提供的任何帮助或了解问题所在。您可以像这样注入服务类
import org.springframework.context.ApplicationContext;
导入org.springframework.context.ApplicationContextAware;
导入org.springframework.beans.BeansException;
@望远镜
@组成部分
公共类GridLayout扩展CustomComponent实现ApplicationContextAware{
私有静态应用上下文上下文;
私人图书馆服务图书馆服务;
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext)抛出BeansException{
上下文=应用上下文;
libraryService=context.getBean(libraryService.class);
}
Grid bookGrid=新网格(Book.class);
VerticalLayout VerticalLayout=新的VerticalLayout();
公共网格布局(){
createMainLayout();
createGrid();
}
私有void createMainLayout(){
setCompositionRoot(垂直布局);
}
私有void createGrid(){
bookGrid=新网格(Book.class);
垂直布局。添加组件(bookGrid);
List books=libraryService.getAllBooks();
bookGrid.setItems(书籍);
bookGrid.removeAllColumns();
bookGrid.addColumn(Book::getTitle).setCaption(“标题”);
bookGrid.addColumn(Book::getAuthor).setCaption(“作者”);
bookGrid.addColumn(Book::getYear).setCaption(“年”);
垂直布局。添加组件(bookGrid);
}
}
问题是您从GridLayout构造函数调用createGrid()@Autowire在调用构造函数后注入bean,所以您需要做的事情如下所示
public GridLayout(){
}
@PostConstruct
public init(){
createMainLayout();
createGrid();
}
你能加一个堆栈吗trace@urag看起来是这样的:bean的实例化失败了;嵌套异常为org.springframework.beans.beanInstationException:未能实例化[com.vaadin.simpleexercise.ui.GridLayout]:构造函数引发异常;嵌套异常是java.lang.NullPointerException],其根本原因我同意,问题与Spring相关。@SteffenHarbich与Spring无关。与错误使用Spring@urag非常感谢您的帮助、解释和快速回答,您的解决方案很有效!:)很高兴为您提供帮助:)@Kakadu您可以通过单击此帖子旁边的复选标记将此帖子标记为正确答案。我尝试了您的解决方案,但得到了与之前相同的错误:(谢谢您的想法:)
@SpringUI
public class LibraryUI extends UI {
@Autowired
private LibraryService libraryService;
@Autowired
private BookForm bookForm;
@Autowired
private GridLayout gridLayout;
private HorizontalLayout mainLayout;
private VerticalLayout verticalLayout = new VerticalLayout();
@Override
protected void init(VaadinRequest vaadinRequest) {
setMainLayout();
setHeader();
loadBooks();
addBookForm();
}
public void setMainLayout() {
mainLayout = new HorizontalLayout();
setContent(mainLayout);
}
public void setHeader() {
Label header = new Label("Library");
header.setStyleName(ValoTheme.LABEL_H1);
verticalLayout.addComponent(header);
mainLayout.addComponent(verticalLayout);
verticalLayout.setComponentAlignment(header, Alignment.TOP_CENTER);
header.addStyleName(ValoTheme.LABEL_H1);
}
private void loadBooks(){
mainLayout.addComponent(gridLayout);
}
public void addBookForm() {
mainLayout.addComponent(bookForm);
mainLayout.setComponentAlignment(bookForm, Alignment.MIDDLE_CENTER);
}
public GridLayout(){
}
@PostConstruct
public init(){
createMainLayout();
createGrid();
}