严重:Servlet.service() for servlet [spring] 在路径 [/com.elearning.webapp] 的上下文中抛出异常 [请求处理失败;

SEVERE: Servlet.service() for servlet [spring] in context with path [/com.elearning.webapp] threw exception [Request processing failed;

我正在构建一个 Spring MVC WebApp.I 我正在使用 JPA 并且 Hibernate.An 测试时发生了错误 EntityManager.I 我正在按以下方式设计和配置:

BaseController.java

public class BaseController {


    public Facade facade;//it is an interface


    /**
     * Inject Services from Spring
     */

    public void setFacade(Facade facade){
        this.facade = facade;
    }

}

BlogController.java

@Controller
public class BlogController extends BaseController{



    @RequestMapping(value="blog/index")
    public String saveValueFromEM() {

        facade.saveCustomBlog();

        return "blog/index";
    }


      @RequestMapping(value="home") 
      public String saveValueFromJPARep() {
      //facade.saveRepositoryBlog();

      return "home"; 

      }

}

HibernateAuthenticator.java

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class HibernateAuthenticator {


    /**
     * Inject persistence layer to classes that extends this class
     * */
    public static EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager emf) {
        em = emf;
    }

}

FacadeImpl.java

public class FacadeImpl extends HibernateAuthenticator implements Facade {


    private BlogService blogService;//it is an interface


    @Override
    public void saveCustomBlog() {
        blogService.saveCustomBlog();

    }


    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }



}

BlogServiceImpl.java

@Transactional(readOnly = true)
public class BlogServiceImpl extends HibernateAuthenticator implements BlogService {



    @Override
    @Transactional(readOnly = false)
    public void saveCustomBlog() {
        // TODO Auto-generated method stub

        Blog blog=new Blog();
        blog.setId(13);
        blog.setName("Fation");
        em.merge(blog);

    }


}

web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" 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/web-app_2_5.xsd">

    <display-name>eLearning</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="elearningPU">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>

            <property name="hibernate.search.default.optimizer.operation_limit.max" value="1000" />
            <property name="hibernate.search.default.optimizer.transaction_limit.max" value="100" />
            <!-- <property name="hibernate.search.default.directory_provider" value="fi.foyt.hibernate.gae.search.GaeDirectoryProvider"/> -->
            <!-- <property name="hibernate.search.default.worker.backend" value="fi.foyt.hibernate.gae.search.GaeBackendQueueProcessor"/> -->


            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
            <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver" />
            <property name="hibernate.connection.username" value="root" />

            <property name="hibernate.connection.password" value="pasw1" />

            <!-- shtim -->
            <property name="hibernate.enable_lazy_load_no_trans" value="true"/>

            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/elearningdb" />
            <property name="hibernate.max_fetch_depth" value="3" />
            <!-- You may want to change this to false in production -->
            <property name="hibernate.show_sql" value="true" />
            <!-- You can change this to create at the first time -->
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <!-- Set this to 0 otherwise you sometimes get errors -->
            <property name="hibernate.connection.pool_size" value="0" />

        </properties>


    </persistence-unit>
</persistence>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="elearningPU" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" />

    <context:component-scan base-package="com.elearning.domain" />

    <context:annotation-config/>

    <bean id="blogService" class="com.elearning.service.BlogServiceImpl" scope="singleton"/>

    <bean id="facade" class="com.elearning.service.FacadeImpl" scope="singleton">
         <property name="blogService" ref="blogService"></property>
    </bean>

</beans>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


    <context:component-scan base-package="com.elearning.controller" />

    <mvc:annotation-driven />

    <mvc:default-servlet-handler />
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean> 

</beans>

Eclipse 控制台错误

SEVERE: Servlet.service() for servlet [spring] in context with path [/com.elearning.webapp] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.elearning.controller.BlogController.saveValueFromEM(BlogController.java:17)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

根据您的代码,facade 对象为空。您需要在 BaseController:

中注入 facade
public class BaseController {

    @Autowired
    public Facade facade;


    /**
     * Inject Services from Spring
     */

    public void setFacade(Facade facade){
        this.facade = facade;
    }

}

你可以参考这个类似的post - Spring can you autowire inside an abstract class?

..正如您在屏幕截图行中看到的那样。 18 && 22 我们使用了 Multiple 注释,所以在代码可以正常工作后删除 22 行号注释。不要在 xml 文件中使用多个注释驱动.........

enter image description here