Search code examples
cdijava-ee-6

CDI - get unproxied instance of bean


I would like to get an unproxied instance of a bean so that another module won't break as it throws an exception indicating the object I'm passing to it isn't of the right type.

Is this possible, if so how? I don't want to create a new instance of the object because it already has some state saved. I am hoping there is a short and quick way for me to get the actual object from the proxied instance.


Solution

  • You can't in a none implementation specific way, and you shouldn't.

    The only thing you can do is to use a scope that doesnt need proxies. The two common ones are @javax.enterprise.context.Dependent and @javax.inject.Singleton.

    Depending on use case you might be able to wrap it in an @Dependent scope been.