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结尾,表示它是一个工厂类(接口),用于 ...
随机推荐
- jQuery常用知识点大总结
目录 jQuery jQuery介绍 jQuery的优势 jQuery的引入方式有两种: jQuery对象和dom对象 jQuery选择器 基本选择器(同css) 基本筛选器(选择之后进行过滤): 属 ...
- Oracle 创建数据表
数据库中的每一个表都被一个模式(或用户)所拥有,因此表是一种典型的模式对象.在创建数据表时,Oracle 将在一个指定的表空间中为其分配存储空间.最初创建的表时一个空的逻辑存储结构,其中不包含任何数据 ...
- RabbitMQ实战场景(一):异步记录用户操作日志
传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...
- altium designer(AD13)隐藏敷铜的方法
覆铜,就是将PCB上闲置的空间作为基准面,然后用固体铜填充,这些铜区又称为灌铜.敷铜的意义在于,减小地线阻抗,提高抗干扰能力;降低压降,提高电源效率;还有,与地线相连,减小环路面积. 如果拿到别人的P ...
- 2013.4.29 - KDD第十一天
今天上午在图书馆写FIrst集,真心没写出来,算法是昨天找好的,不过实现的话还是需要很大的代码量,然后就打算用郑茂或者韩冰的代码了. 晚上图书馆快关门的时候开始思考KDD的问题, 我一开始打算给中秋发 ...
- web workers 实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SpringCloud学习心得—1.3—Eureka与REST API
SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...
- java连接mysql数据库时的时区设置问题(time_zone)
java在连接mysql数据库时,会由于时区设置不正确导致报以下的错误: The server time zone value '???ú±ê×??±??' is unrecognized or ...
- 题解 洛谷P1457 【城堡 The Castle】
这道题,看似很烦,无从下手,但其实只要用位运算和联通快就能水过了呀. 首先,输入:似乎大意是把一个数拆成二进数的相加,分别表示\((i,j)\)东南西北是否有墙.\(1\)表示西,\(2\)表示北,\ ...
- c语言中,如果将无符号数转换为有符号数
在使用ti的adc芯片ads1259时,芯片是24为数据格式保存的,其中最高位是符号位,因此可以理解为是有符号数据,但是在嵌入式系统中,没有直接24位的变量,因此使用32的无符号先保存24位的数据. ...