Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'session'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.http.HttpSession' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
说明:一般这种报错很有可能是控制器或mvc.xml出了问题
解决办法
方法一:
由于报错 Error creating bean with name ‘loginController’,即问题出现在LoginController类上,源代码为:
这里是直接将session定义为成员变量了,按理说,在SpringMVC中是可以直接注入Servlet的核心对象的,但为什么在运行测试类时报错,我有点迷惑…为了可以成功运行,将该对象直接作为方法的形参即可,即:
方法二:
直接在测试类加注解@WebAppConfiguration
参考:https://blog.csdn.net/qq_31683775/article/details/102950119