Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
On 25/02/2013 18:49, Scott Ferguson wrote: > On 2/24/13 2:54 PM, c.whalley wrote: >> On 24/02/2013 17:21, Scott Ferguson wrote: >>> On 2/24/13 2:06 AM, c.whalley wrote: On 24/02/2013 05:49, Dan Ziegelbein wrote: > The reason you're getting NPE's is that your instances of APICommand are > not themselves injected by the container (you are instantiating them in > code somewhere...indicated by the fact that APICommand has a > non-injected parameterized constructor). > > Dan Hi Dan & thanks for this, I looked more at this and realised I need to do as you say, and it seems I need Webbeans and the @Component annotation, plus token META-INF/web-beans.xml to mark it as so? If that's right, I'm now struggling to even get the @Component to resolve - does the webbeans package come with Resin or is it an external download? >>> For Resin 4.0, it's CDI and all the names changed: >>> >>> the magic file in META-INF/beans.xml in a jar or WEB-INF/beans.xml for >>> the WEB-INF/classes. The smallest content of that file "". >>> >>> You don't need an annotation on the bean, but you can use something like >>> @javax.inject.Singleton if you want it to be a single instance. The >>> default behavior instantiates a new instance of your bean at each >>> @Inject point. >>> >>> -- Scott >> Umm ... I hope I read you right - I just added beans.xml to WEB-INF with >> just "" as its content and that ut is still null. >> I did change it slightly from the code I posted earlier to ensure it >> really is a bean, in case that was significant: > How are you instantiating APICommand? > > For example, are you injecting it into your servlet? > > If you used "new APICommand()", CDI wouldn't be invoked, because it's > not doing that kind of bytecode generation on the constructor. > > Two general patterns are: > @Inject APICommand _command; // injecting into another bean > @Inject Instance _factory; // creates a factory to return > new instances That was it - much clearer now, thanks & it all working beautifully;-) ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
On 2/24/13 2:54 PM, c.whalley wrote: > On 24/02/2013 17:21, Scott Ferguson wrote: >> On 2/24/13 2:06 AM, c.whalley wrote: >>> On 24/02/2013 05:49, Dan Ziegelbein wrote: The reason you're getting NPE's is that your instances of APICommand are not themselves injected by the container (you are instantiating them in code somewhere...indicated by the fact that APICommand has a non-injected parameterized constructor). Dan >>> Hi Dan & thanks for this, >>> >>> I looked more at this and realised I need to do as you say, and it seems >>> I need Webbeans and the @Component annotation, plus token >>> META-INF/web-beans.xml to mark it as so? If that's right, I'm now >>> struggling to even get the @Component to resolve - does the webbeans >>> package come with Resin or is it an external download? >> For Resin 4.0, it's CDI and all the names changed: >> >> the magic file in META-INF/beans.xml in a jar or WEB-INF/beans.xml for >> the WEB-INF/classes. The smallest content of that file "". >> >> You don't need an annotation on the bean, but you can use something like >> @javax.inject.Singleton if you want it to be a single instance. The >> default behavior instantiates a new instance of your bean at each >> @Inject point. >> >> -- Scott > Umm ... I hope I read you right - I just added beans.xml to WEB-INF with > just "" as its content and that ut is still null. > I did change it slightly from the code I posted earlier to ensure it > really is a bean, in case that was significant: How are you instantiating APICommand? For example, are you injecting it into your servlet? If you used "new APICommand()", CDI wouldn't be invoked, because it's not doing that kind of bytecode generation on the constructor. Two general patterns are: @Inject APICommand _command; // injecting into another bean @Inject Instance _factory; // creates a factory to return new instances -- Scott > > public class APICommand implements Serializable { > private static final long serialVersionUID = 1L; > private static final Logger log = > Logger.getLogger(APICommand.class.getName()); > > @PersistenceUnit(unitName="my_persistence_unit") > private EntityManagerFactory emf; > > @Inject > private UserTransaction ut; > private EntityManager em; > ... > private void update(User user) { > try { > ut.begin(); > em = emf.createEntityManager(); > ... > } > } > >>> -- Carl Whalley >>> >>> ___ >>> resin-interest mailing list >>> resin-interest@caucho.com >>> http://maillist.caucho.com/mailman/listinfo/resin-interest >>> >> ___ >> resin-interest mailing list >> resin-interest@caucho.com >> http://maillist.caucho.com/mailman/listinfo/resin-interest >> > > ___ > resin-interest mailing list > resin-interest@caucho.com > http://maillist.caucho.com/mailman/listinfo/resin-interest > ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
On 24/02/2013 17:21, Scott Ferguson wrote: > On 2/24/13 2:06 AM, c.whalley wrote: >> On 24/02/2013 05:49, Dan Ziegelbein wrote: >>> The reason you're getting NPE's is that your instances of APICommand are >>> not themselves injected by the container (you are instantiating them in >>> code somewhere...indicated by the fact that APICommand has a >>> non-injected parameterized constructor). >>> >>> Dan >> Hi Dan & thanks for this, >> >> I looked more at this and realised I need to do as you say, and it seems >> I need Webbeans and the @Component annotation, plus token >> META-INF/web-beans.xml to mark it as so? If that's right, I'm now >> struggling to even get the @Component to resolve - does the webbeans >> package come with Resin or is it an external download? > For Resin 4.0, it's CDI and all the names changed: > > the magic file in META-INF/beans.xml in a jar or WEB-INF/beans.xml for > the WEB-INF/classes. The smallest content of that file "". > > You don't need an annotation on the bean, but you can use something like > @javax.inject.Singleton if you want it to be a single instance. The > default behavior instantiates a new instance of your bean at each > @Inject point. > > -- Scott Umm ... I hope I read you right - I just added beans.xml to WEB-INF with just "" as its content and that ut is still null. I did change it slightly from the code I posted earlier to ensure it really is a bean, in case that was significant: public class APICommand implements Serializable { private static final long serialVersionUID = 1L; private static final Logger log = Logger.getLogger(APICommand.class.getName()); @PersistenceUnit(unitName="my_persistence_unit") private EntityManagerFactory emf; @Inject private UserTransaction ut; private EntityManager em; ... private void update(User user) { try { ut.begin(); em = emf.createEntityManager(); ... } } > >> -- Carl Whalley >> >> ___ >> resin-interest mailing list >> resin-interest@caucho.com >> http://maillist.caucho.com/mailman/listinfo/resin-interest >> > > ___ > resin-interest mailing list > resin-interest@caucho.com > http://maillist.caucho.com/mailman/listinfo/resin-interest > ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
On 2/24/13 2:06 AM, c.whalley wrote: > On 24/02/2013 05:49, Dan Ziegelbein wrote: >> The reason you're getting NPE's is that your instances of APICommand are >> not themselves injected by the container (you are instantiating them in >> code somewhere...indicated by the fact that APICommand has a >> non-injected parameterized constructor). >> >> Dan > Hi Dan & thanks for this, > > I looked more at this and realised I need to do as you say, and it seems > I need Webbeans and the @Component annotation, plus token > META-INF/web-beans.xml to mark it as so? If that's right, I'm now > struggling to even get the @Component to resolve - does the webbeans > package come with Resin or is it an external download? For Resin 4.0, it's CDI and all the names changed: the magic file in META-INF/beans.xml in a jar or WEB-INF/beans.xml for the WEB-INF/classes. The smallest content of that file "". You don't need an annotation on the bean, but you can use something like @javax.inject.Singleton if you want it to be a single instance. The default behavior instantiates a new instance of your bean at each @Inject point. -- Scott > > -- Carl Whalley > > ___ > resin-interest mailing list > resin-interest@caucho.com > http://maillist.caucho.com/mailman/listinfo/resin-interest > ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
On 24/02/2013 05:49, Dan Ziegelbein wrote: > The reason you're getting NPE's is that your instances of APICommand are > not themselves injected by the container (you are instantiating them in > code somewhere...indicated by the fact that APICommand has a > non-injected parameterized constructor). > > Dan Hi Dan & thanks for this, I looked more at this and realised I need to do as you say, and it seems I need Webbeans and the @Component annotation, plus token META-INF/web-beans.xml to mark it as so? If that's right, I'm now struggling to even get the @Component to resolve - does the webbeans package come with Resin or is it an external download? -- Carl Whalley ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest
Re: [Resin-interest] Resin Pro 4.0.34 with Eclipselink 2.4.0.v20120608-r11652 - @Inject UserTransaction null
The reason you're getting NPE's is that your instances of APICommand are not themselves injected by the container (you are instantiating them in code somewhere...indicated by the fact that APICommand has a non-injected parameterized constructor). Dan On Sat, 2013-02-23 at 11:59 +, c.whalley wrote: > I have a rather long servlet which I'm trying to refactor such that > separate helper objects perform the same work. The trouble is, I think > I'm missing some config setting because the same working code from the > servlet gives npe's when moved to another class. What's special about > having these annotation in the servlet class (where ultimately I don't > want them anyway)? The problem is the "ut" instance in the ut.begin(); > line of APICommand below is null, whereas in the APIServlet it's fine. > > Thanks. > > -- > Servlet definition: > > @WebServlet(value="/api", name="api-servlet") > public class APIServlet extends HttpServlet { > > @PersistenceUnit(unitName="my_persistence_unit") > private EntityManagerFactory emf; > private EntityManager em; > > @Inject > private UserTransaction ut; > > public void service(HttpServletRequest req, HttpServletResponse > res) throws IOException, ServletException { > PrintWriter out = res.getWriter(); > res.setContentType("text/html"); > > User user = new User(); > user.setName("foo"); > update(user); > ... > } > > private void update(User user) { > try { > ut.begin(); > em = emf.createEntityManager(); > ... > em.persist(userToPersist); > em.flush(); > } catch (Exception e) { > ... > } finally { > try { > ut.commit(); > } catch (Exception commitEx) { > ... > } > } > } > } > > Helper class definition: > -- > public class APICommand { > > @PersistenceUnit(unitName="my_persistence_unit") > private EntityManagerFactory emf; > private EntityManager em; > > @Inject > private UserTransaction ut; > > public APICommand(HttpServletRequest req) throws IOException { > api = API.findAPI(req); > } > > public String execute() { > if (api.isValid()) { > if (api == API.EXAMPLE) { > User user = new User(); > user.setName("foo"); > update(user); > } > } > } > > private void update(User user) { > try { > ut.begin(); > em = emf.createEntityManager(); > ... > em.persist(userToPersist); > em.flush(); > } catch (Exception e) { > ... > } finally { > try { > ut.commit(); > } catch (Exception commitEx) { > ... > } > } > } > } > > > ___ > resin-interest mailing list > resin-interest@caucho.com > http://maillist.caucho.com/mailman/listinfo/resin-interest > ___ resin-interest mailing list resin-interest@caucho.com http://maillist.caucho.com/mailman/listinfo/resin-interest