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结尾,表示它是一个工厂类(接口),用于 ...
随机推荐
- 页面 ajax
function ajax({ url, success, data = { }, type= "GET", async = true}){ let xhr; if(XMLHttp ...
- cookie遇到java.lang.IllegalArgumentException: Control character in cookie value or attribute
java.lang.IllegalArgumentException: Control character in cookie value or attribute. 该异常说明cookie中的val ...
- linux之expect用法
1. [#!/usr/bin/expect] 这一行告诉操作系统脚本里的代码使用那一个shell来执行.这里的expect其实和linux下的bash.windows下的cmd是一类东西. 注意:这一 ...
- sqlserver 将一个表中的某些字段更新到另一个表中(转载)
来源:https://blog.csdn.net/qq_23888451/article/details/86615555 https://blog.csdn.net/cyxinda/article/ ...
- navicat for mysql 链接时报错:1251-Client does not support authentication protocol requested by server
客户端使用navicat for mysql.本地安装了mysql 8.0.但是在链接的时候提示: 主要原因是mysql服务器要求的认证插件版本与客户端不一致造成的. 打开mysql命令行输入如下命令 ...
- docker部署beego环境解决golang三方依赖库问题
直接上图,有图有真相 Dockerfile文件中 运行dockercompose命令即可 以上请注意路项目路徑不要搞错
- springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5
近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是 使用 redisTemplate 或 ...
- c# Match类
- HDFS重启集群导致数据损坏,使用fsck命令修复过程
HDFS重启集群导致数据损坏,使用fsck命令修复过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们先看一组输出 [root@flume112 ~]# hdfs fsck / ...
- Android手机测试环境搭建
Android SDK概念: SDK(software development kit)软件开发工具包.被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合. ...