在实体监听器中注入服务......?

时间:2018-05-14 20:36:41

标签: jpa callback cdi entitylisteners

是否可以通过任何方式@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中)也会被注入,而我不必担心它

0 个答案:

没有答案