0
votes

As far as I know CDI uses dynamic proxy for non-dependent bean injection. If there is a class that implements some interface there is no problem, example:

@SessionScoped
public class MessageBean implements Message {...}

proxy can be created based on Message interface, but what if the class implements no interface:

@SessionScoped
public class MessageBean {...}

The injection into Servlet still works:

@WebServlet("/example")
public class MessageServlet extends HttpServlet {

    @Inject
    private MessageBean messageBean;

so the question is how it is handled for example by Weld?

1
It just extends the class. - BalusC
According to the docs there are two statements: A proxy class extends java.lang.reflect.Proxy and A proxy class implements exactly the interfaces specified at its creation so it is possible to create proxy without interface? According to this topic it is only possible with CGLIB for example. - swch
hk2 also uses javassist as cglib seems as though it is not being actively maintained at this time - jwells131313

1 Answers

2
votes

Not every proxy is an instance of java.lang.reflect.Proxy, Weld has its own proxying framework at this point which can subclass any non-final class. Weld also does not use javassist to do proxying (older versions did, but the 2.x are internal).

If you're curious to see how it happens you can find that here: https://github.com/weld/core/blob/master/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java

One note - the whole process relies non-final methods and classes. You'll notice that even the CDI spec makes a reference to non-final.