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

【示例】

package com.example.SpringBootStudy.controller;

import com.example.SpringBootStudy.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @Author jyy
* @Description {}
* @Date 2018/12/19 18:28
*/
@RestController
@RequestMapping(value = "/test")
public class TestController { @Autowired
private TestService testService; @RequestMapping(value = "/print",method = RequestMethod.GET)
public void test() {
testService.test();
}
}
package com.example.SpringBootStudy.service;

import com.example.SpringBootStudy.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; /**
* @Author jyy
* @Description {}
* @Date 2018/12/19 18:26
*/
@Service
public class TestService { @Autowired
private TestDao testDao; public void test() {
testDao.test();
}
}
package com.example.SpringBootStudy.dao;

import org.springframework.stereotype.Repository;

/**
* @Author jyy
* @Description {}
* @Date 2018/12/19 18:26
*/
@Repository
public class TestDao { public void test() {
System.out.println("调用成功!");
}
}

输出结果:

调用成功!

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

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

package com.example.SpringBootStudy.controller;

import com.example.SpringBootStudy.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @Author jyy
* @Description {}
* @Date 2018/12/19 18:28
*/
@RestController
@RequestMapping(value = "/test")
public class TestController { //@Autowired
//private TestService testService;
private TestService testService = new TestService(); @RequestMapping(value = "/print", method = RequestMethod.GET)
public void test() {
testService.test();
}
}

输出结果:

报出空指针异常,跟踪发现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. Fortify漏洞之Insecure Randomness(不安全随机数)

    继续对Fortify的漏洞进行总结,本篇主要针对  Insecure Randomness  漏洞进行总结,如下: 1.Insecure Randomness(不安全随机数) 1.1.产生原因: 成弱 ...

  2. win10开启redis失败解决方案

    输入命令:redis-server redis.windows.conf 提示:解决redis无法启动,报错:无法将“redis-server”项识别为 cmdlet.函数.脚本文件或可运行程 序的名 ...

  3. [极客-Linux] 05 系统调用

    分配内存的系统调用 1)brk  小内存 2)mmap  大内存 sigaction 注册信号处理函数 进程间通信: 1)消息队列,在内核中,msgget 创建 2)共享内存,shmget 内核源代码 ...

  4. linux学习记录--比较基本的文件档案知识

    [档案类型权限,连接数,档案拥有者,档案所属群组,档案容量,修改日期,档名],对应了上面的每一列的参数属性. 档案类型权限那一部分总共有十个字母,第一个字母代表档案类型: 当为[ d ]则是目录,例如 ...

  5. 不错的DSP和FPGA作者

    https://blog.csdn.net/wordwarwordwar/article/details/90233903

  6. spring常用的事务传播属性说明

    事务Transaction,就是一组操作数据库的动作集合.事务是现代数据库理论中的核心概念之一.如果一组处理步骤或者全部发生或者一步也不执行,我们称该组处理步骤为一个事务.当所有的步骤像一个操作一样被 ...

  7. 使用BERT预训练模型+微调进行文本分类

    本文记录使用BERT预训练模型,修改最顶层softmax层,微调几个epoch,进行文本分类任务. BERT源码 首先BERT源码来自谷歌官方tensorflow版:https://github.co ...

  8. pyserial 挺强大的

    Ref: https://pythonhosted.org/pyserial/ pyserial写的很规范,无论安装和使用都非常的容易,目前使用下来非常好. 没有使用它做过压力测试,不知道表现如何. ...

  9. MongoDB导出与导入远程Linux服务器上的数据

    场景 将远程服务器A上的MongoDB数据库test下的集合people导入到服务器B数据库test的集合people_test下. 简单方案 先将A服务器数据导出,然后再执行导入到B服务器. 下面的 ...

  10. python网络-HTTP协议(28)

    一.服务器和客户端介绍 1.什么是服务器? 简而言之:提供服务的机器就是服务器,至于提供什么服务不重要,重要的是要给其他人提供服务.例如:FTP服务器可以提供文件下载,SMTP服务器等等,不同的服务器 ...