职业菜鸟☆ 2022-05-16 21:40 采纳率: 75%
浏览 1527
已结题

ecplise中spring练习项目报错:Cannot invoke "annotation.dao.TestDao.save()" because "this.testDao" is null

问题遇到的现象和发生背景

刚学习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,应该不是版本问题

问题相关代码,请勿粘贴截图

以下我将展示我所有的代码及相关截图

img


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出来的值,求各位指点迷津

  • 写回答

1条回答 默认 最新

  • Silwings银翼 2022-05-17 20:50
    关注

    TestSeviceImpl
    import jakarta.annotation.Resource; => import javax.annotation.Resource;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月25日
  • 已采纳回答 5月17日
  • 创建了问题 5月16日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值