Manually inject beans in a not managed class

//Usage CdiHelper.programmaticInjection(MyClass.class, this) public class CdiHelper { @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> void programmaticInjection(Class clazz, T injectionObject) throws NamingException { InitialContext initialContext = new InitialContext(); Object lookup = initialContext.lookup("java:comp/BeanManager"); BeanManager beanManager = (BeanManager) lookup; AnnotatedType annotatedType = beanManager.createAnnotatedType(clazz); InjectionTarget injectionTarget = beanManager.createInjectionTarget(annotatedType); CreationalContext creationalContext = beanManager.createCreationalContext(null); injectionTarget.inject(injectionObject, creationalContext); creationalContext.release(); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.