Rule Definition
For a CDI Bean with a normal scope, the CDI contained will provide a proxy of the underlying object instead of a reference to the object itself. If an injected class is not proxyable, the container will raise an exception at runtime.
This error will not be detected at compilation.
As per the CDI specs, A class can be put in a proxy if it fulfills the following constraints:
* It must have a non private Constructor with no argument,
* It must neither be final nor have a final method.
The normal scopes are Session, RequestScope, Conversation and Application, and any custom scope annotated as normal.
Remediation
Make sure that the injected class fulfills the constraints of a proxy class, change the bean scope.
Violation Code Sample
public class Producer{
@Produces
@SessionScoped
@Named("stringProvider")
public static String getStringId()
{
return "SomeStaticID";
}
}
@Named
public class MyBean
{
@Named("stringProvider")
private String id;
}
Fixed Code Sample
public class Producer{
@Produces
@Dependent
@Named("stringProvider")
public static String getStringId()
{
return "SomeStaticID";
}
}
@Named
public class MyBean
{
@Named("stringProvider")
private String id;
}
Reference
https://docs.jboss.org/weld/reference/latest/en-US/html/injection.html#_client_proxies
https://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#client_proxies
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.