[英]@Stateless not injected via @Inject in a JSF @ManagedBean
[英]Inject within Guice to ManagedBean JSF
我在將Guice( @Inject
)中的存儲庫類注入到@ManagedBean
類(JSF)時遇到問題,后者使用EntityManager
從數據庫獲取信息。 我讀過很多老文章,它們的用法很奇怪,但是沒有用。 這是代碼:
public class InitConfigListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
install(new JpaPersistModule("db-manager"));
filter("/*").through(PersistFilter.class);
}
}, new RepositoryModule());
}
}
public class RepositoryModule extends AbstractModule {
public void configure() {
bind(IBookRepository.class).to(BookRepository.class).asEagerSingleton();
bind(IUserRepository.class).to(UserRepository.class).asEagerSingleton();
}
}
@ManagedBean
@ViewScoped
public class BooksView {
private List<Book> bookList;
private IBookRepository booksRepository;
public BooksView() { }
@Inject
public BooksView(IBookRepository booksRepository) {
this.booksRepository = booksRepository;
}
@PostConstruct
public void initBookList() {
bookList = booksRepository.getAll();
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
}
EntityManager已注入存儲庫中。 綁定存儲庫后,沒有執行類BooksView
@Inject
,執行了@PostConstruct
,並且注入的存儲庫為null。
感謝幫助。
如BalusC所述,Guice的工作方式與CDI略有不同。
發生的情況是CDI正在正確執行並在方法上調用post構造。 JSF類也不是Guice管理的,而是CDI管理的,因此除非您使用的是基於servlet的Web前端,否則您需要從前端調用guice(這是正確的),在這里使用guice-servlet可以解決這個問題。 方法注釋也需要aopalliance 。
您可以通過過濾器(例如: https : //github.com/GedMarc/JWebSwing-Undertow/blob/master/src/main/java/za/co/mmagon/jwebswing/undertow/UndertowJWebSwingHandlerExtension.java )來初始化Guice。基於容器的容器,或者如果要在JSF視圖中的@PostConstruct之前啟動, 請作為此處提到的ServletContextListener( https://github.com/google/guice/wiki/ServletModule )。
同樣有趣的是,在EE環境中,@ PostConstruct的時序與@Singleton的時序不同。 在獨立環境中,您可以將注入生成器放置在該位置以為ejb和servlet加載注入(來自戰爭中的模塊)
只要適當配置JSF,就可以使用Guice。
簡而言之,您需要啟用以下兩項功能-設置自定義應用程序工廠-配置EL以從Guice獲取綁定
通過faces-config.xml文件設置應用程序工廠
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
<factory>
<application-factory>com.jwebmp.guicedservlets.jsf.FacesApplicationFactoryWrapper</application-factory>
</factory>
</faces-config>
Servlet上下文參數(web.xml)或表示/嵌入部署信息
com.sun.faces.facesInitializerMappingsAdded=true
deploymentInfo.addServletContextAttribute(RIConstants.FACES_INITIALIZER_MAPPINGS_ADDED, Boolean.TRUE)
.addListener(new ListenerInfo(com.sun.faces.config.ConfigureListener.class));
然后在模塊中-使用選擇的掃描機制,並將所有@ javax.inject.Named和@ManagedBean綁定到您將在Guice EL Resolver中使用的鍵,以替換CDI / CustomInject / JSF使用的任何注入形式帥哥
下面是捆綁的實現,您可以使用JDK 11的Guiced EE框架以及模塊化JPMS中的版本(向下兼容1.8)簡單地附加。
注意*由於javax.faces庫中的ServiceProviders無效,因此框架引用了本地javax.faces,在更新時該版本為2.3.9,其中的提供者已刪除,從而使您能夠使用JLink構建JRE或僅執行應用程序在JPMS中
https://github.com/GedMarc/Guiced-Servlets-JSF
https://search.maven.org/artifact/com.jwebmp.guicedee.servlets/guiced-servlets-jsf/0.68.0.1/jar
<dependency>
<groupId>com.jwebmp.guicedee.servlets</groupId>
<artifactId>guiced-servlets-jsf</artifactId>
<version>0.68.0.1</version>
</dependency>
然后,您幾乎可以在幾乎任何環境中對所有注入都適當地使用@ManagedBean和/或@Named(JSF2.3),但guice-servlet尚不支持的Servlet 4除外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.