Howto Inject a DynamicProxy
antoen Feb 12, 2013 7:20 PMHowto Inject a DynamicProxy?
Dear weld community,
I am using JBOSS 7.1.1.Final and have the following Problem. The users of our API provide an interface with some annotations. Our implementation now builds a dynamicProxy with an invoationHandler for this interface and provides the user with an implementation. Is it possible to provide the users of our API with some CDI / WELD miracles, so that they can use injection to obtain the proxy?
actual Example using the method "buildObject" deep within a factory: deep in our API:
public <T> T buildObject(final Class<T> aInterface)
{
final T impl =
(T) java.lang.reflect.Proxy.newProxyInstance(
aInterface.getClassLoader(),
new Class[] { aInterface },
new PropertiesProxyInvocationHandler(someConfiguration));
return impl;
}
Code of the user of our API:
public interface SomeInterface
{
@Fetch("http://www.example.com/api")
void doSomething();
@Store("http://www.example.com/api")
void doSomethingElse();
}
@Stateless public class MyService
{
public void execute()
{
final ApiFactory factory = ApiFactoryFactory.createDefaultFactory();
final ApiManagement management = factory.createDefaultManagement();
final SomeInterface myImpl = management.getImplementation(SomeInterface.class);
myImpl.doSomething();
}
}
I would like to reduce the boilerplate for the user of our api with something like that:
@Stateless public class MyService
{
@Inject final SomeInterface myImpl;
public void execute()
{
myImpl.doSomething()
}
}
Is there a CDI Extension / Weld Extension that can be used to inject a DynamicProxy for an interface?
Any help is highly welcome.
Best regards,
Anton