JBoss Community

Struggling with injecting singleton in 6.0

created by Robin Hughes in Beginner's Corner - View the full discussion

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

Start a new discussion in Beginner's Corner at Community