I am working on simple bookLibrary project in Vaadin 8 and Spring. I am struggling with injecting service class into UI components. It gives me nullPointerExceptionMessage all the time. I do not use "new" to create service anywhere.
Here is LibraryService class:
@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;
}
}
GridLayout looks like this:
@UIScope
@Component
public class GridLayout extends CustomComponent {
@Autowired
LibraryService libraryService; //this is null
Grid<Book> bookGrid = new Grid<>(Book.class);
VerticalLayout verticalLayout = new VerticalLayout();
public GridLayout(){
createMainLayout();
createGrid();
}
private void createMainLayout() {
setCompositionRoot(verticalLayout);
}
private void createGrid() {
bookGrid = new Grid<>(Book.class);
verticalLayout.addComponent(bookGrid);
List<Book> books = libraryService.getAllBooks();
bookGrid.setItems(books);
bookGrid.removeAllColumns();
bookGrid.addColumn(Book::getTitle).setCaption("TITLE");
bookGrid.addColumn(Book::getAuthor).setCaption("AUTHOR");
bookGrid.addColumn(Book::getYear).setCaption("YEAR");
verticalLayout.addComponent(bookGrid);
}
}
and main UI code is here :
@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);
}
I will be very grateful for any help or idea what is wrong.
The problem is you calling createGrid() from GridLayout constructor the @Autowire injects the beans after constructor was called so what you need to do is something like this
public GridLayout(){
}
@PostConstruct
public init(){
createMainLayout();
createGrid();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With