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

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

  1. package annotation.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import annotation.service.TestService;
  5. @Controller
  6. public class TestController {
  7. @Autowired
  8. private TestService testService;
  9. public void save() {
  10. testService.save();
  11. System.out.println("testController save");
  12. }
  13. }

2.annotation.dao包中的
接口TestDao

  1. package annotation.dao;
  2. public interface TestDao {
  3. public void save();
  4. }

接口实例化

  1. package annotation.dao;
  2. import org.springframework.stereotype.Repository;
  3. @Repository("testDaoImpl")
  4. /**相当于@Respository,但如果在Service层使用@Resource(name="testDaoImpl")testDaoImpl不能省略。testDaoImpl为Ioc容器中的对象名**/
  5. public class TestDaoImpl implements TestDao{
  6. @Override
  7. public void save() {
  8. System.out.println("testDao save");
  9. }
  10. }

3.annotation.service
TestService接口

  1. package annotation.service;
  2. public interface TestService {
  3. public void save();
  4. }

TestServiceImpl,接口实例化

  1. package annotation.service;
  2. import org.springframework.stereotype.Service;
  3. import annotation.dao.TestDao;
  4. import jakarta.annotation.Resource;
  5. @Service("testSeviceImpl")//相当于Service
  6. public class TestSeviceImpl implements TestService{
  7. @Resource(name="testDaoImpl")
  8. /**相当于@Autowired@Autowird默认按照Bean类型注入**/
  9. private TestDao testDao;
  10. @Override
  11. public void save() {
  12. testDao.save();
  13. System.out.println("testService save");
  14. }
  15. }

4.annotation.test包,测试包
TestAnnotation.java

  1. package annotation.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import annotation.controller.TestController;
  5. public class TestAnnotation {
  6. public static void main(String[] args) {
  7. @SuppressWarnings("resource")
  8. ApplicationContext appCon = new ClassPathXmlApplicationContext("config/applicationContext.xml");
  9. TestController tt = (TestController)appCon.getBean("testController");
  10. tt.save();
  11. }
  12. }

5.config包中的applicationConext.xml文件内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!-- 扫描annotation包及其子包中的注解 -->
  10. <context:component-scan base-package="annotation"/>
  11. </beans>

6.web.xml文件内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <display-name>ch1-2</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.jsp</welcome-file>
  7. <welcome-file>index.htm</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.jsp</welcome-file>
  10. <welcome-file>default.htm</welcome-file>
  11. </welcome-file-list>
  12. </web-app>
运行结果及报错内容
  1. Exception in thread "main" java.lang.NullPointerException: Cannot invoke "annotation.dao.TestDao.save()" because "this.testDao" is null
  2. at annotation.service.TestSeviceImpl.save(TestSeviceImpl.java:13)
  3. at annotation.controller.TestController.save(TestController.java:10)
  4. at annotation.test.TestAnnotation.main(TestAnnotation.java:10)
我的解答思路和尝试过的方法

在web.xml中添加监听器,在最后加上

  1. <!-- 监听器 spring有自带的 -->
  2. <listener>
  3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  4. </listener>

连web.xml都报错

我想要达到的结果

能输出TestDaoImpl,TestServiceImpl,TestController它们仨个中的system出来的值,求各位指点迷津

展开全部

  • 写回答

1条回答 默认 最新

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

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

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

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月17日
  • 创建了问题 5月16日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部