Suppose there is an interface:
public interface BookDAO { public Book getBookByID(int id); } Class:
public class BookDAOImpl implements BookDAO { public Book getBookByID(int id) { .....dowork } } And servlet:
import javax.inject.Inject; @WebServlet("/management") public class BookStore extends HttpServlet { @Inject private BookDAO bookDAO; protected void doGet(HttpServletRequest req, HttpServletResponse resp) { List<Book> books = new List<>(); books = bookDAO.getALLBooks(); } } src\META-INF\bean.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans> I NullPointerException on books = bookDAO.getALLBooks() , the method in a usual console form through new BookDAOImpl works out normally.
In Spring, @Repository and @Autowired + were used to specify where to scan, but what about @Inject ?
Thank!
BookDAOImpl, for example,@Named,@Statelessor some@...Scoped. - user194374beans.xmlcreated? - user194374bean.xml. - CheshireKbeans.xmlshould be insrc/webapp/WEB-INF. - user194374