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

1

7

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

Hrvatska answered 14/7, 2014 at 13:2 Comment(0)
U
16

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

Uvarovite answered 14/7, 2014 at 13:28 Comment(1)
Thanks! I just spent waaaaay too much time on this :DAirborne

© 2022 - 2024 — McMap. All rights reserved.