Java servlet上的jhipster注入
我创建了一个servlet,但我无法注入存储库对象。有人有这个问题吗 WebConfigure中的我的配置:Java servlet上的jhipster注入,java,servlets,jhipster,Java,Servlets,Jhipster,我创建了一个servlet,但我无法注入存储库对象。有人有这个问题吗 WebConfigure中的我的配置: @Override public void onStartup(ServletContext servletContext) throws ServletException { log.info("Web application configuration, using profiles: {}", Arrays.toString(env.getActiveProfiles())
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
log.info("Web application configuration, using profiles: {}", Arrays.toString(env.getActiveProfiles()));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(WebMvcAutoConfiguration.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
我的servlet:
@WebServlet(urlPatterns = "/servlet/preview")
public class ServletPreViewHtml extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private PagecontentRepository pagecontentRepository;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
doGet(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Pagecontent p = pagecontentRepository.getOne(2l); //line 34
out.println(p.getFullHtml());
始终向我返回以下错误:
java.lang.NullPointerException:在
br.com.dmsolutions.netfarmacontentmanager.web.rest.ServletPreViewHtml.doGet(ServletPreViewHtml.java:34)
NPE的行:
pagecontentRepository.getOne(2L)
我宁愿使用@WebServlet
,这样Spring就可以注入你的存储库。我宁愿使用@Controller
,这样Spring就可以注入你的存储库。我同意Gaël Marziou,但您也可以使用Spring Boot 1.3的一部分@ServletComponentScan
我同意Gaël Marziou的观点,但您也可以使用Spring Boot 1.3的一部分
@ServletComponentScan