In CDI 1.2, section 6.5.3, The container must ensure that every injection point of type InjectionPoint and qualifier @Default of any dependent object instantiated during this process receives: • a null value if it is not being injected into any injection point.
The above is not working on weld 2.2.5. I created a test for this and confirmed that I got a not null injection point. ====================== public class CDIBean2 { private String data; public String getData() { return data; } public void setData(String data) { this.data = data; } } ====================== public class CDIBean { CDIBean2 beanManagerInstance;
public CDIBean(BeanManager manager){ Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class); if (beanList!=null && !beanList.isEmpty()){ Bean<?> bean = beanList.iterator().next(); CreationalContext<?> context = manager.createCreationalContext(bean); beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context); } } public String getData() { return beanManagerInstance.getData(); }
public void setData(String data) { beanManagerInstance.setData(data); } ========================================== public class CDIBean2Producer { @Produces public CDIBean2 produce(InjectionPoint ip){ if(ip != null){ throw new RuntimeException("InjectionPoint exists when producing CDIBean2"); } return new CDIBean2(); } } =================================== @WebServlet("/ProducerNullIPServlet") public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject private CDIBean bean; @Inject private BeanManager manager; @Inject private Instance<AnotherBean> anotherBeanInstance;
//@Override @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }
//@Override @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
Set<Bean<?>> beanList = manager.getBeans(CDIBean2.class); if (beanList != null && !beanList.isEmpty()) { Bean<?> bean = beanList.iterator().next(); CreationalContext<?> context = manager.createCreationalContext(bean); CDIBean2 beanManagerInstance = (CDIBean2) manager.getReference(bean, CDIBean2.class, context); beanManagerInstance.setData("Test2"); } out.println("Test Sucessful!"); } } The injection point I got in @Produces public CDIBean2 produce(InjectionPoint ip){..} refers to the @Inject private CDIBean bean, which is incorrect.
|