Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java jspbean中的Guice注入_Java_Jsp_Jetty_Guice - Fatal编程技术网

Java jspbean中的Guice注入

Java jspbean中的Guice注入,java,jsp,jetty,guice,Java,Jsp,Jetty,Guice,我正在编写一个java应用程序,使用Guice作为我的DI框架,使用Hibernate作为我的Orm。我想运行一个简单的嵌入式Jetty服务器来服务几个jsp页面。我使用以下代码运行了服务器: Server server = new Server(8081); final WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/rpga"); webAppContext.setResour

我正在编写一个java应用程序,使用Guice作为我的DI框架,使用Hibernate作为我的Orm。我想运行一个简单的嵌入式Jetty服务器来服务几个jsp页面。我使用以下代码运行了服务器:

Server server = new Server(8081);
final WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/rpga");
webAppContext.setResourceBase("web/WEB-INF/");
webAppContext.setDescriptor("web/WEB-INF/web.xml");
webAppContext.setParentLoaderPriority(true);

final Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration","org.eclipse.jetty.annotations.AnnotationConfiguration");

webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$|.*/classes/.*");

webAppContext.setServer(server);

server.setHandler(webAppContext);
server.start();
server.join();
现在我想使用几个简单的bean在jsp中添加一些数据。我尝试创建一个bean并将我的dao注入其中,但是由于bean不是由Guice管理的,所以dao没有被注入

我的JSP看起来像

<html>
   <head>
      <title>Playlist</title>
   </head>
   <body>
        <jsp:useBean id="playlist" class="com.duom.beans.PlaylistBean" /> 
        ...do stuff with playlistBean
   </body>
</html>

为了实现我的目标,我错过了什么?

我设法找到了解决问题的办法。我设法澄清了我的意图,并使用正确的技术重新启动了我的开发人员

我从JSP切换到JSF2,为Guice注入器创建了一个工厂类:

public class GuiceFactory {

    private static final Injector injector = Guice.createInjector(new RpgaAppModule());

    public static Injector getInjector() {
        return injector;
    }
}
然后在我的bean的每个构造函数上调用:

GuiceFactory.getInjector().injectMembers(this);

如果我的解决方案在设计或体系结构方面有误,请不要犹豫发表评论。

我认为您不能。如果您想在JSP中使用DI,则需要使用JavaEE的CDI框架。除非您希望创建自己的JSP标记,该标记调用直接传递到Guice注入器以获取实例的工厂。(请注意,这绝对是一个黑客,而不是一个好的)。感谢您的回复,我终于找到了解决方案,请参阅下面我的答案!
GuiceFactory.getInjector().injectMembers(this);