<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Struggling with injecting singleton in 6.0
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/rhinmass">Robin Hughes</a> in <i>Beginner's Corner</i> - <a href="http://community.jboss.org/message/597183#597183">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I am having trouble figuring out how to inject a singleton into another class - in this case a servlet.&#160; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My singleton looks like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>package com.robin.ejb</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javax.ejb.Singleton;</p><p>import javax.ejb.Startup;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Singleton</p><p>@Startup </p><p>public class HelloWorld</p><p>{</p><p>&#160;&#160;&#160; public HelloWorld()</p><p>&#160;&#160;&#160; {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println("constructing HelloWorld");</p><p>&#160;&#160;&#160; }&#160;&#160; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; public String sayHello() {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return "Hello World !!!";</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My servlet looks like this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import ...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import com.robin.ejb.HelloWorld;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@WebServlet(name="helloServlet", urlPatterns={"/hello"}, </p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; initParams={ @WebInitParam(name="nameParam", value="world")})</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public class HelloServlet extends HttpServlet {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; private static final long serialVersionUID = 1L;</p><p>&#160;&#160;&#160; @Inject</p><p>&#160;&#160;&#160; private HelloWorld hw;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @Override</p><p>&#160;&#160;&#160; protected void doGet(HttpServletRequest request, HttpServletResponse response)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; throws ServletException, IOException {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; PrintWriter out = response.getWriter();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; String nameParam = request.getParameter("nameParam");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (nameParam == null)</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nameParam = getServletConfig().getInitParameter("nameParam");</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; out.println("hello " + nameParam + " " + hw.sayHello());</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; out.flush();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; out.close();</p><p>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>When I run this in JBoss AS 6.0.0.Final I get the following errors:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>2011-04-01 10:23:43,299 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController] (Thread-2) Error installing to Start: name=vfs:///C:/jboss/jboss-6.0.0.Final/server/rbh/deploy/myapp-0.0.1.ear_WeldBootstrapBean state=Create: org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [HelloWorld] with qualifiers [@Default] at injection point [[field] @Inject private com.robin.webapp.HelloServlet.hw]. Possible dependencies [[Session bean [class com.robin.ejb.HelloWorld with qualifiers [@Any @Default]; local interfaces are [HelloWorld], Session bean [class com.robin.ejb.HelloWorld with qualifiers [@Any @Default]; local interfaces are [HelloWorld]]]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:309) [:6.0.0.Final]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:139) [:6.0.0.Final]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:162) [:6.0.0.Final]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:377) [:6.0.0.Final]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:363) [:6.0.0.Final]</p><p>&#160;&#160;&#160; at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:388) [:6.0.0.Final]</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/597183#597183">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in Beginner's Corner at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>