问题遇到的现象和发生背景
刚学习springboot,跟着陈恒的《SSM+Spring Boot+Vue.js3全栈开发从入门到实战》学习,刚练习到书中第二个小例子,就报错了,搜集挺多办法,有修改web.xml添加监听器(还是报错),我用的spring-framework-5.8.3,tomcat为10.0.0,jdk版本为17,书中讲的版本为framwork-5.3.2,tomcat版本为9.0.30,应该不是版本问题
问题相关代码,请勿粘贴截图
以下我将展示我所有的代码及相关截图
1.annotation.controller包中的代码
TestControler.java
- package annotation.controller;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import annotation.service.TestService;
- @Controller
- public class TestController {
- @Autowired
- private TestService testService;
- public void save() {
- testService.save();
- System.out.println("testController save");
- }
- }
-
2.annotation.dao包中的
接口TestDao
- package annotation.dao;
- public interface TestDao {
- public void save();
- }
-
接口实例化
- package annotation.dao;
- import org.springframework.stereotype.Repository;
- @Repository("testDaoImpl")
- /**相当于@Respository,但如果在Service层使用@Resource(name="testDaoImpl")testDaoImpl不能省略。testDaoImpl为Ioc容器中的对象名**/
- public class TestDaoImpl implements TestDao{
- @Override
- public void save() {
- System.out.println("testDao save");
- }
- }
-
3.annotation.service
TestService接口
- package annotation.service;
- public interface TestService {
- public void save();
- }
-
TestServiceImpl,接口实例化
- package annotation.service;
- import org.springframework.stereotype.Service;
- import annotation.dao.TestDao;
- import jakarta.annotation.Resource;
- @Service("testSeviceImpl")//相当于Service
- public class TestSeviceImpl implements TestService{
- @Resource(name="testDaoImpl")
- /**相当于@Autowired,@Autowird默认按照Bean类型注入**/
- private TestDao testDao;
- @Override
- public void save() {
- testDao.save();
- System.out.println("testService save");
- }
- }
4.annotation.test包,测试包
TestAnnotation.java
- package annotation.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import annotation.controller.TestController;
- public class TestAnnotation {
- public static void main(String[] args) {
- @SuppressWarnings("resource")
- ApplicationContext appCon = new ClassPathXmlApplicationContext("config/applicationContext.xml");
- TestController tt = (TestController)appCon.getBean("testController");
- tt.save();
- }
- }
5.config包中的applicationConext.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 扫描annotation包及其子包中的注解 -->
- <context:component-scan base-package="annotation"/>
- </beans>
6.web.xml文件内容
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
- <display-name>ch1-2</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- <welcome-file>default.htm</welcome-file>
- </welcome-file-list>
- </web-app>
-
运行结果及报错内容
- Exception in thread "main" java.lang.NullPointerException: Cannot invoke "annotation.dao.TestDao.save()" because "this.testDao" is null
- at annotation.service.TestSeviceImpl.save(TestSeviceImpl.java:13)
- at annotation.controller.TestController.save(TestController.java:10)
- at annotation.test.TestAnnotation.main(TestAnnotation.java:10)
-
-
我的解答思路和尝试过的方法
在web.xml中添加监听器,在最后加上
- <!-- 监听器 spring有自带的 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
连web.xml都报错
我想要达到的结果
能输出TestDaoImpl,TestServiceImpl,TestController它们仨个中的system出来的值,求各位指点迷津