java - Correct CDI annotations with Jersey/Glassfish -


since i'm struggling on documentation cdi, hope question become useful resource correct cdi annotations use in jersey/glassfish.

say have application bookstore:

package my.bookstore;  import javax.ws.rs.applicationpath; import org.glassfish.jersey.server.resourceconfig;  @applicationpath("/bookstore") public class bookstore extends resourceconfig {     public bookstore() {         this.packages("my.bookstore.resource");     } } 

we want make book entities accessible via restful service:

package my.bookstore.entity;  public class book {     public string isbn;     public string title;     public string author;      public book(string isbn, string title, string author) {         this.isbn = isbn;         this.title = title;         this.author = author;     } } 

so need dao access datastore:

package my.bookstore.dao;  import my.bookstore.entity.book; import java.util.list;  public interface bookdao {     public list<book> getallbooks(); } 

and implementation:

package my.bookstore.dao;  import my.bookstore.entity.book; import java.util.list; import java.util.arraylist;  public class defaultbookdao implements bookdao {     public list<book> getallbooks() {         list<book> booklist = new arraylist<>();         list.add(new book("1234", "awesome book", "some author"));         return booklist;      } } 

then want inject defaultbookdao restful service:

package my.bookstore.resource;  import javax.inject.inject; import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype;  @path("/books") public class bookresource {     @inject    bookdao dao;     @get    @produces(mediatype.application_json)    public list<book> getbooks() {        return this.dao.getallbooks();    } } 

now, when deploying application get:

unsatisfied dependencies type bookdao qualifiers @default 

since need make cdi aware of it; how? tried various combinations of @named, @default, @model, @singleton, @stateless , many resources such questions , blog articles have own interpretation of them.

what correct, plain cdi annotation use make injection work in jersey/glassfish?

to me, seems did not put beans.xml file application. glassfish 4 (generally java ee 7) file not required, however, if omit it, beans annotated scope annotations considered. therefore, defaultbookdao not marked annotation, not considered cdi candidate injection.

you have 2 options fix , make cdi mechanism consider defaultbookdao:

  • put @dependent annotation on defaultbookdao class - not change scope, @dependent default scope, make cdi consider class
  • create beans.xml file in either meta-inf or web-inf (for web apps) value of bean-discovery-mode="all"

in opinion, first option cleaner - may separate code can injected , cannot. if want increase productivity omitting unnecessary annotations, go second option. more complicated, have once per module.

please see this oracle blog post beans.xml in java ee 7 , default behavior if omitted.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -