繁体   English   中英

在Guice中注入ManagedBean JSF

[英]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使用的任何注入形式帅哥

此处的模块装订器示例

Guice EL解析器示例在这里

Faces应用程序包装器

可选视图范围Impl


下面是捆绑的实现,您可以使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM