Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject String with qualifier in CDI

Tags:

cdi

jboss-weld

I'm trying to do simple thing. Inject qualified String (or File) in CDI.

So I have a qualifier:

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD,METHOD,PARAMETER,TYPE})
@Qualifier
public @interface FilesRepositoryPath {}

I have a producer:

public class FilesRepositoryPathProducer {

  @Produces
  @FilesRepositoryPath
  public String getRepositoryDirectory() {
    return "path.taken.from.configuration";
  }
}

And I'm trying to use it:

@ApplicationScoped
public class FilesRepository {

  @Inject
  public FilesRepository(@FilesRepositoryPath String filesDirectory) {
    //Do some stuff
  }
}

However, WELD cannot instantiate this bean. I am getting an exception:

org.jboss.arquillian.impl.event.FiredEventException: org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001410 The injection point [field] @Inject private za.co.fnb.commercial.dms.file.FilesRepositoryBeanTest.repo has non-proxyable dependencies

I know String is unproxable, but why WELD wants to create a proxy? It has @Dependent scope, so AFAIK it shouldn't create proxy anyway. How can I make it work?

like image 635
amorfis Avatar asked Apr 20 '11 16:04

amorfis


People also ask

What is the use of @inject annotation?

The @Inject annotation lets us define an injection point that is injected during bean instantiation. Injection can occur via three different mechanisms. A bean can only have one injectable constructor.

How do you inject a dependency?

The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

What is a method qualifier?

A qualifier is an annotation that you apply to a bean. A qualifier type is a Java annotation defined as @Target({METHOD, FIELD, PARAMETER, TYPE}) and @Retention(RUNTIME). For example, you could declare an @Informal qualifier type and apply it to another class that extends the Greeting class.


1 Answers

you need the default constructor

@ApplicationScoped
public class FilesRepository {

  public FilesRepository() {
  }

  @Inject
  public FilesRepository(@FilesRepositoryPath String filesDirectory) {
    //Do some stuff
  }
}
like image 155
Kurohige Avatar answered Oct 09 '22 15:10

Kurohige