Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI object not proxyable with injected constructor

When trying to inject arguments into the constructor of a CDI bean (ApplicationScoped), I'm encountering the following issue:

Caused by: org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001435: Normal scoped bean class xx.Config is not proxyable because it has no no-args constructor - Managed Bean [class xx.Config] with qualifiers [@Default @Named @Any].
    at org.jboss.weld.bean.proxy.DefaultProxyInstantiator.validateNoargConstructor(DefaultProxyInstantiator.java:50)
    at org.jboss.weld.util.Proxies.getUnproxyableClassException(Proxies.java:217)
    at org.jboss.weld.util.Proxies.getUnproxyableTypeException(Proxies.java:178)

However, I do have an injectable constructor on the class:

@Inject
public Config(ConfigLocator configLocator) {
    defaultConfigPath = configLocator.getPath();
    doStuff();
}

With a default constructor, variable injection and a postconstruct method, this all works fine, but I'd prefer the constructor injection in this case.

Any thoughts what is going wrong here?

like image 379
Steven De Groote Avatar asked Oct 03 '17 11:10

Steven De Groote


2 Answers

We resolved similar problem splitting class into interface and implementation. In your case something like this:

public interface Config
{
  // API here
}

@ApplicationScoped @Priority(0)
public class ConfigImpl implements Config
{
  @Inject
  public ConfigImpl(ConfigLocator configLocator) { ... }

  // API implementation here
}
like image 198
Vladimir Nesterovsky Avatar answered Nov 19 '22 00:11

Vladimir Nesterovsky


This example might help you :

@ApplicationScoped
public class Config {

    private String defaultConfigPath;  

    @Inject
    public Config(ConfigLocator configLocator) {
       this.defaultConfigPath = configLocator.getPath();
       doStuff();
    }

    // create a no-args constructor which is required for any scoped bean.
    public Config() {
    }

}

You need to have a public non-args constructor in the @ApplicationScoped bean.

Note : The bean for this class will created only once and is maintained for the entire life of an application. This bean will shared among all the managed beans. @ApplicationScoped beans are singleton in nature.

The mentioned issue:

Caused by: org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001435: Normal scoped bean class xx.Config is not proxyable because it has no no-args constructor - Managed Bean [class xx.Config] with qualifiers [@Default @Named @Any].

The possible reason for this is that a non-dependent scoped bean is required to provide a public no-args constructor for CDI, so that it can pick up the desired proxy bean at runtime.

like image 5
Anish B. Avatar answered Nov 19 '22 00:11

Anish B.