前两天写代码的时候遇到一个问题,通过new出来的对象,自动注入的属性总是报空指针的错误。到网上查了资料,才发现问题所在,同时也加深了自己对于容器IOC的理解。现在把这个问题记录一下,仅供大家参考。

【示例】

  1. package com.example.SpringBootStudy.controller;
  2.  
  3. import com.example.SpringBootStudy.service.TestService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. /**
  10. * @Author jyy
  11. * @Description {}
  12. * @Date 2018/12/19 18:28
  13. */
  14. @RestController
  15. @RequestMapping(value = "/test")
  16. public class TestController {
  17.  
  18. @Autowired
  19. private TestService testService;
  20.  
  21. @RequestMapping(value = "/print",method = RequestMethod.GET)
  22. public void test() {
  23. testService.test();
  24. }
  25. }
  1. package com.example.SpringBootStudy.service;
  2.  
  3. import com.example.SpringBootStudy.dao.TestDao;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6.  
  7. /**
  8. * @Author jyy
  9. * @Description {}
  10. * @Date 2018/12/19 18:26
  11. */
  12. @Service
  13. public class TestService {
  14.  
  15. @Autowired
  16. private TestDao testDao;
  17.  
  18. public void test() {
  19. testDao.test();
  20. }
  21. }
  1. package com.example.SpringBootStudy.dao;
  2.  
  3. import org.springframework.stereotype.Repository;
  4.  
  5. /**
  6. * @Author jyy
  7. * @Description {}
  8. * @Date 2018/12/19 18:26
  9. */
  10. @Repository
  11. public class TestDao {
  12.  
  13. public void test() {
  14. System.out.println("调用成功!");
  15. }
  16. }

输出结果:

  1. 调用成功!

一个很简单的示例,Controller调用Service,Service调用Dao,输出结果。

我们将Controller中testService初始化的方式改为new,看下效果:

  1. package com.example.SpringBootStudy.controller;
  2.  
  3. import com.example.SpringBootStudy.service.TestService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. /**
  10. * @Author jyy
  11. * @Description {}
  12. * @Date 2018/12/19 18:28
  13. */
  14. @RestController
  15. @RequestMapping(value = "/test")
  16. public class TestController {
  17.  
  18. //@Autowired
  19. //private TestService testService;
  20. private TestService testService = new TestService();
  21.  
  22. @RequestMapping(value = "/print", method = RequestMethod.GET)
  23. public void test() {
  24. testService.test();
  25. }
  26. }

输出结果:

报出空指针异常,跟踪发现Service中的testDao未正确赋值

总结:@Autowired是从IOC容器中获取已经初始化的对象,此对象中@Autowired的属性也已经通过容器完成了注入,整个生命周期都交由容器管控。然而通过new出来的对象,生命周期不受容器管控,自然也无法完成属性的自动注入。

Spring|@Autowired与new的区别的更多相关文章

  1. spring @Autowired或@Resource 的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属于spring的),默认情况下必 ...

  2. spring @Autowired与@Resource的区别

    1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...

  3. Wiring in Spring: @Autowired, @Resource and @Inject 区别

    refer:https://www.baeldung.com/spring-annotations-resource-inject-autowire 主要是查找顺序不一致: @Resource Mat ...

  4. Spring 注释 @Autowired 和@Resource 的区别

    Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...

  5. Spring 注释标签@Resource @Autowired 和@Inject的区别

    一些spring的开发人员在使用这三个标签进行注入的时候感到困惑.我来尝试解释一下这三个注解的主要区别.事实上,这三者非常相似,只存在一些微小的差别.在稍后的文章中会进行解释. @Resource-在 ...

  6. Spring下的@Inject、@Autowired、@Resource注解区别(转)

    1.@Inject javax.inject JSR330 (Dependency Injection for Java) 这是JSR330中的规范,通过AutowiredAnnotationBean ...

  7. java @Autowired与@Resource的区别

    @Autowired与@Resource的区别     1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认 ...

  8. @Autowired 与@Resource的区别(详细)

    参考:@Autowired 与@Resource的区别(详细) spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@Pos ...

  9. Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法

    Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...

随机推荐

  1. Selenium文件上传

    转自:https://www.cnblogs.com/miaojjblog/p/9679915.html Web上本地上传图片,弹出的框Selenium是无法识别的,也就是说,selenium本身没有 ...

  2. SAP Marketing Cloud功能简述(四) : 线索和客户管理

    这个系列的前三篇文章Grace Dong已经给大家带来了下面的内容: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Clou ...

  3. 利用.bat脚本使得可运行jar开机自动运行

    1.利用Elipse到处可运行的jar包 2.写.bat脚本[点此下载],相应目录自己根据需要修改即可 3.将此脚本放在"启动"文件夹中

  4. Qt命名规范

    1) 类名:单词首字母大写,单词和单词之间直接连接,无需连接字符 如: MyClass,QPushButton class MainWindow { }; 2) 函数名字,变量名:第二个单词开始(不是 ...

  5. 如何设置CentOS 7开机自动获取IP地址详解

    本例中以CentOS 7举例说明如何设置Linux开机自动获取IP地址和设置固定IP地址. 自动获取动态IP地址 1.输入“ip addr”并按回车键确定,发现无法获取IP(CentOS 7默认没有i ...

  6. 使用safe-rm替换rm命令,防止误删除

    1.下载safe源码包: wget https://launchpad.net/safe-rm/trunk/0.12/+download/safe-rm-0.12.tar.gz 2.解压safe-rm ...

  7. 在idea中编写自动拉取、编译、启动springboot项目的shell脚本

    idea 开发环境搭建 idea中安装shell开发插件 服务器具备的条件 已经安装 lsof(用于检查端口占用) 已安装 git 安装 maven 有 java 环境 背景 代码提交到仓库后,需要在 ...

  8. html页面标记 点击目录跳转到页面相应位置 简易回到顶部

    html页面标记 点击目录跳转到页面相应位置 简易回到顶部  参考博客:

  9. python 对象引用计数增加和减少的情况

    对象引用计数增加的情况: 1.对象被创建:x=4 2.另外的别人被创建:y=x 3.被作为参数传递给函数:foo(x)  ->会增加2 4.作为容器对象的一个元素:a=[1,x,'33'] 对象 ...

  10. RabbitMQ交换机、RabbitMQ整合springCloud

    目标 1.交换机 2.RabbitMQ整合springCloud 交换机 蓝色区域===生产者 红色区域===Server:又称Broker,接受客户端的连接,实现AMQP实体服务 绿色区域===消费 ...