[jboss-user] [Beginner's Corner] - Struggling with injecting singleton in 6.0

Robin Hughes do-not-reply at jboss.com
Fri Apr 1 10:28:14 EDT 2011


Robin Hughes [http://community.jboss.org/people/rhinmass] created the discussion

"Struggling with injecting singleton in 6.0"

To view the discussion, visit: http://community.jboss.org/message/597183#597183

--------------------------------------------------------------
Hello,

I am having trouble figuring out how to inject a singleton into another class - in this case a servlet.  

My singleton looks like this:

package com.robin.ejb

import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup 
public class HelloWorld
{
    public HelloWorld()
    {
        System.out.println("constructing HelloWorld");
    }   

    public String sayHello() {
        return "Hello World !!!";
    }

}



My servlet looks like this:

import ...

import com.robin.ejb.HelloWorld;

@WebServlet(name="helloServlet", urlPatterns={"/hello"}, 
        initParams={ @WebInitParam(name="nameParam", value="world")})

public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    @Inject
    private HelloWorld hw;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String nameParam = request.getParameter("nameParam");
        if (nameParam == null)
            nameParam = getServletConfig().getInitParameter("nameParam");
        out.println("hello " + nameParam + " " + hw.sayHello());
        out.flush();
        out.close();
    }

}


When I run this in JBoss AS 6.0.0.Final I get the following errors:


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]]]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:309) [:6.0.0.Final]
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:139) [:6.0.0.Final]
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:162) [:6.0.0.Final]
    at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:377) [:6.0.0.Final]
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:363) [:6.0.0.Final]
    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:388) [:6.0.0.Final]
--------------------------------------------------------------

Reply to this message by going to Community
[http://community.jboss.org/message/597183#597183]

Start a new discussion in Beginner's Corner at Community
[http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2075]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20110401/f822ab40/attachment-0001.html 


More information about the jboss-user mailing list