servlets - How to fill @EJB annotated fields programmatically via glassfish container? -
i have following problem. have defined interface:
@local public interface producttypelister { collection<listelement> getlist(); } also, have defined implementation thing:
@stateless(name = "producttypelister") @local(producttypelister.class) public class producttypelisterimpl implements producttypelister { @override public collection<listelement> getlist() { // implementation code } } and want use via annotation:
public class listproducttypeaction extends actionhandler { @ejb protected producttypelister lister; @override public string execute(httpservletrequest request) throws servletexception { request.setattribute("list", lister.getlist()); return "listpage.jsp"; } } but nullpointerexception, since lister not filled. now, problem is, actionhandler not derivative of httpservlet , not executed container itself. instead, using frontcontroller patter - i.e. there 1 servlet handles incoming messages , creates required handlers via simple construction. , therefore ejb annotated fields not filled automatically. however, can fill specific case in following manner:
public listproducttypeaction() throws namingexception { initialcontext context = new initialcontext(); lister = (producttypelister) context.lookup("<path>/producttypelister"); } in case works fine, means have every handler specified lookup jndi help, want annotation help. therefore, main servlet needs following method:
private void fillhandler(actionhandler handler) { // fill @ejb annotated fields } but how can fill it? of course, can manually run through every field , check if it's annotated ejb , fill using jndi based on interface unqualified name. there way using libraries have? after all, glassfish supposed fill fields during deployment phase. how can make him non-servlet class?
dependency injection simplest way of obtaining enterprise bean reference. clients run within java ee server-managed environment, javaserver faces web applications, jax-rs web services, other enterprise beans, or java ee application clients, support dependency injection using javax.ejb.ejb annotation.
applications run outside java ee server-managed environment, such java se applications, must perform explicit lookup. jndi supports global syntax identifying java ee components simplify explicit lookup.
so if class inside j2ee container, lifecycle not managed container, out of luck, have manually.
Comments
Post a Comment