Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE using a producer method yields 'WELD-001408 Unsatisfied dependencies ..' exception

Hy there,

I am absolutely fresh to Java EE and CDI. I try to set up a test-project in order to do my first steps with JAX-RS and CDI. I have CDI enabled by providing a beans.xml. As long as I am testing, I use the bean-discovery-mode="all" element. CDI seems to work fine, as I can successfully inject a BeanManager into my test-resource and I can output all the beans it discovered.

I try to inject a class provided by a producer method. Netbeans shows me a warning for the provideMe field: No enabled eligible for injection beans are found

When I deploy the application to Glassfish, I get the weld exception:

WELD-001408 Unsatisfied dependencies for type [ProvideMeInterface] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private com.test.rest.DataImport.provideMe]

Here are my classes for this little test:

Interface:

package com.test.orm;
public interface ProvideMeInterface
{
    int getCount();
}

Implementation:

package com.test.orm;
public class ProvideMeImpl implements ProvideMeInterface {

    int cnt;
    ProvideMeImpl(int cnt)
    {
        this.cnt = cnt;
    }

    @Override
    public int getCount() {
        return cnt;
    }
}

Producer:

package com.test.orm;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.Produces;

public class ProvideMeProvider {

  @Produces
  @ApplicationScoped  
  public ProvideMeInterface produceFactory() {
      return new ProvideMeImpl(111);
  }
}

Resource:

package com.test.rest;

import ...
...

@Path("rest/import")
public class DataImport {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Inject
    private ProvideMeInterface provideMe;

    @Inject
    BeanManager beanManager;

    public DataImport() {}

    @GET
    @Produces("text/html")
    public String getHtml() {
        return "<html><body><h1>IMPORT Resource: " + provideMe.getCount() + " </body></h1></html>";
    }
}

When I start the application without the "ProvideMe" test and I print print out all the discovered beans, the ProvideMeProvider class is discovered, as also 'ProvideMe':

...
INFO:   2014-07-14 14:09:31,040 [DEBUG] com.test.rest.DataImport - Bean: com.test.orm.ProvideMeProvider
INFO:   2014-07-14 14:09:31,041 [DEBUG] com.test.rest.DataImport - Bean: com.test.orm.ProvideMe
...

Questions:

1) Why do I get the "Unsatisfied dependencies.." error when trying to inject the only implementation for the interface?

2) Are all necessary classes discovered here?

3) If not, what do I need to do so they get discovered?

4) What is the "ProvideMe" class that it discovers? (I've got ProvideMeImpl, ProvideMeInterface and ProvideMeProvider, but no class ProvideMe)

I am using: Glassfish 4.0 (build 89) (Java EE 7 Web)/NetBeans 8.0/maven 3.2.1/JDK 1.7

like image 546
user3836334 Avatar asked Jul 14 '14 13:07

user3836334


1 Answers

You are importing @Produces from javax.ws.rs (used by the JAX-RS runtime), instead from javax.enterprise.inject.

like image 58
A. Rodas Avatar answered Nov 03 '22 15:11

A. Rodas