是否可以通过任何方式@Inject a Service-Bean(比如会话bean)到实体监听器?
将以下场景视为示例
实体:
@Entity
@EntityListeners(BookListener.class)
public class Book {
// fields, getters & setters
}
实用程序类:
@Singleton
public class BookUtil {
private BookRepository bookRepo;
private List<Book> bookList;
@Inject
public BookUtil(BookRepository bookRepo){
this.bookRepo = bookRepo;
this.bookList = this.bookRepo.findAll();
}
public void refreshBooks(){
this.bookList = this.bookRepo.findAll();
}
}
监听器:
public class BookListener {
@Inject
BookUtil bookUtil // --> CAN THIS BE ACHIEVED?
@PostPersist
private void refreshCache(Book b){
bookUtil.refreshBooks();
}
}
我尝试了几个我能想到的东西,但没有一个成功注入了BookUtil的实例。我可以手动实例化它,这是有效的。但我更喜欢注射,因为BookRepository(在BookUtil中)也会被注入,而我不必担心它