jpa - Injecting Services in Entity Listener ...?
问题描述
Is it by any means possible to @Inject a Service-Bean (say a session bean) into an entity Listener?
Consider the following scenario as an example
Entity:
@Entity
@EntityListeners(BookListener.class)
public class Book {
// fields, getters & setters
}
Utility class:
@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();
}
}
Listener:
public class BookListener {
@Inject
BookUtil bookUtil // --> CAN THIS BE ACHIEVED?
@PostPersist
private void refreshCache(Book b){
bookUtil.refreshBooks();
}
}
I tried out several things I could think of but none of them successfully injected an instance of BookUtil. I could manually instantiate it, which works. But I prefer injection as then the BookRepository(inside the BookUtil) would also be injected, without me having to worry about it
解决方案
推荐阅读
- vb.net - 更新 DataGridViewTable
- javascript - JS/jQuery 本地存储数组
- python - Keras 回归 | 从具有多个 y 参数的模型中获取单个预测
- c++ - 如何在c ++中绕一个圆圈
- c# - 当用户未添加图像时,如何避免在 datagridview 中查看 img 时出错
- python - 如何以向量化方式numpy多次索引多维数组?
- autodesk-viewer - 应用加载选项后缺少一些对象
- javascript - 如何在我的屏幕上显示秒表读数的值
- javascript - JavaScript 中的随机图像
- azure - API 的 JWT 令牌和 OAUTH 策略的 azure 保管库密钥