On Feb 10, 2009, at 2:59 PM, Aaron Freeman wrote: > I have a question about the one of the annotations in the Comet demo > at: > > http://www.caucho.com/resin-3.1/examples/servlet-comet/comet > > I am not too familiar with Java's annotations, so I am wondering how > I can > get access to the _timerService, which is declared like: > > @In private TimerService _timerService; > > I would like to create a JSTL custom tag, for example, that can > access the > _timerService. What does @In do?
I've updated the example at http://caucho.com/resin/examples/servlet-comet . The example has been disabled because caucho.com isn't running the latest snapshot yet. (Possible upgrade this weekend) The META-INF/beans.xml triggers a scan of all the classes to register the beans. For the TimerService, I've marked it as @javax.context.ApplicationScoped, which means it has the same lifetime as the <web-app>, i.e. it's essentially a singleton. The TimerService has a default binding type of @javax.inject.Current. You use the binding annotation wherever you want the TimerService (as long as the class is managed by Resin). Since JSP tags are managed by Resin, you can use @javax.inject.Current in a JSP tag definition's field to get a reference to the TimerService. With the latest Java DI spec draft, a bean/class like TimerService does not automatically get a name. So it can't automatically be used by a JSP/JSF EL expression. To register the bean with a name for EL, you use @javax.annotation.Named on the class, like: @javax.annotation.Named("timer") @javax.context.ApplicationScoped public class TimerService { ... } Then ${timer} will refer to the timer instance. > Do I just have to create a getter method that returns TimerService? > That > wouldn't seem to work unless TimerService is a singleton -- but it's > not > declared statically. Correct. TimerService is basically a singleton within the context of a web-app. You should avoid static singletons because of classloader issues. -- Scott > I am thinking I can solve this easily with > annotations, but I just don't understand them quite yet. > > Thanks, > > Aaron > > > > _______________________________________________ > resin-interest mailing list > [email protected] > http://maillist.caucho.com/mailman/listinfo/resin-interest _______________________________________________ resin-interest mailing list [email protected] http://maillist.caucho.com/mailman/listinfo/resin-interest
