Spring|@Autowired与new的区别
前两天写代码的时候遇到一个问题,通过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的区别的更多相关文章
- spring @Autowired或@Resource 的区别
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属于spring的),默认情况下必 ...
- spring @Autowired与@Resource的区别
1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必 ...
- Wiring in Spring: @Autowired, @Resource and @Inject 区别
refer:https://www.baeldung.com/spring-annotations-resource-inject-autowire 主要是查找顺序不一致: @Resource Mat ...
- Spring 注释 @Autowired 和@Resource 的区别
Spring 注释 @Autowired 和@Resource 的区别 一. @Autowired和@Resource都可以用来装配bean,都可以写在字段上,或者方法上. 二. @Autowired ...
- Spring 注释标签@Resource @Autowired 和@Inject的区别
一些spring的开发人员在使用这三个标签进行注入的时候感到困惑.我来尝试解释一下这三个注解的主要区别.事实上,这三者非常相似,只存在一些微小的差别.在稍后的文章中会进行解释. @Resource-在 ...
- Spring下的@Inject、@Autowired、@Resource注解区别(转)
1.@Inject javax.inject JSR330 (Dependency Injection for Java) 这是JSR330中的规范,通过AutowiredAnnotationBean ...
- java @Autowired与@Resource的区别
@Autowired与@Resource的区别 1.@Autowired与@Resource都可以用来装配bean. 都可以写在字段上,或写在setter方法上. 2.@Autowired默认 ...
- @Autowired 与@Resource的区别(详细)
参考:@Autowired 与@Resource的区别(详细) spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@Pos ...
- Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...
随机推荐
- Selenium文件上传
转自:https://www.cnblogs.com/miaojjblog/p/9679915.html Web上本地上传图片,弹出的框Selenium是无法识别的,也就是说,selenium本身没有 ...
- SAP Marketing Cloud功能简述(四) : 线索和客户管理
这个系列的前三篇文章Grace Dong已经给大家带来了下面的内容: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Clou ...
- 利用.bat脚本使得可运行jar开机自动运行
1.利用Elipse到处可运行的jar包 2.写.bat脚本[点此下载],相应目录自己根据需要修改即可 3.将此脚本放在"启动"文件夹中
- Qt命名规范
1) 类名:单词首字母大写,单词和单词之间直接连接,无需连接字符 如: MyClass,QPushButton class MainWindow { }; 2) 函数名字,变量名:第二个单词开始(不是 ...
- 如何设置CentOS 7开机自动获取IP地址详解
本例中以CentOS 7举例说明如何设置Linux开机自动获取IP地址和设置固定IP地址. 自动获取动态IP地址 1.输入“ip addr”并按回车键确定,发现无法获取IP(CentOS 7默认没有i ...
- 使用safe-rm替换rm命令,防止误删除
1.下载safe源码包: wget https://launchpad.net/safe-rm/trunk/0.12/+download/safe-rm-0.12.tar.gz 2.解压safe-rm ...
- 在idea中编写自动拉取、编译、启动springboot项目的shell脚本
idea 开发环境搭建 idea中安装shell开发插件 服务器具备的条件 已经安装 lsof(用于检查端口占用) 已安装 git 安装 maven 有 java 环境 背景 代码提交到仓库后,需要在 ...
- html页面标记 点击目录跳转到页面相应位置 简易回到顶部
html页面标记 点击目录跳转到页面相应位置 简易回到顶部 参考博客:
- python 对象引用计数增加和减少的情况
对象引用计数增加的情况: 1.对象被创建:x=4 2.另外的别人被创建:y=x 3.被作为参数传递给函数:foo(x) ->会增加2 4.作为容器对象的一个元素:a=[1,x,'33'] 对象 ...
- RabbitMQ交换机、RabbitMQ整合springCloud
目标 1.交换机 2.RabbitMQ整合springCloud 交换机 蓝色区域===生产者 红色区域===Server:又称Broker,接受客户端的连接,实现AMQP实体服务 绿色区域===消费 ...