前两天写代码的时候遇到一个问题,通过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. ..\USER\stm32f10x.h(428): error: #67: expected a "}" ADC1_2_IRQn = 18, /*!

    MDK软件编译,出现如下错误: ..\USER\stm32f10x.h(428): error: #67: expected a "}" ADC1_2_IRQn = 18, /*! ...

  2. Java内存模型JMM简单分析

    参考博文:http://blog.csdn.net/suifeng3051/article/details/52611310 http://www.cnblogs.com/nexiyi/p/java_ ...

  3. Js网站开发学习第一天

    1.登录时,记住密码单选框,鼠标移上去显示div里的内容,移开则消失: <head> <meta http-equiv="Content-Type" conten ...

  4. js 数组 去重 算法(转载)

    以下内容可能有重复部分,项目有用上,但还没来得急整理和验证. 一:https://www.cnblogs.com/jiayuexuan/p/7527055.html 1.遍历数组法 它是最简单的数组去 ...

  5. 用python执行 js代码__来自脚本之家

    "" github地址 :https://github.com/emmetio/pyv8-binaries "" 安装依赖 首先安装依赖:Boost, 这一步网 ...

  6. mac 下enable mysql的load data in file

    1)使用root用户登录mysql 2)将 local_infile 变量设置为true SET GLOBAL local_infile = true; 3)重启数据库 在系统偏好设置中找到MySql ...

  7. 如果不用 ReSharper,那么 Visual Studio 2019 能还原 ReSharper 多少功能呢?

    原文:https://blog.csdn.net/WPwalter/article/details/100158000 本文的内容分为三个部分: Visual Studio 能完全还原的 ReShar ...

  8. Systemweaver — 电子电气协同设计研发平台

            当前电子电气系统随着功能安全.AutoSAR.车联网.智能驾驶等新要求,导致其复杂性.关联性日益上升.当前,传统基于文档的设计由于其低复用性.无关联性.无协同性等缺点,已经无法适应日益 ...

  9. DT6.0下搜索页解决canonical获取乱码问题

    最近研究dt6.0,官方内核默认是把搜索页屏蔽的,但是做seo的人都知道,搜索页聚合是争取排名的好地方,所以我就二次开发搜索页,具体可以查看前几期分享的,今天说说关于搜索的canonical的url乱 ...

  10. machine learning (3)---Linear Algebra Review

    Matrix Vector Multiplication 左边的矩阵向量相乘法比右边的更简洁而且计算高效 Matrix Matrix Multiplication 可以同时计算12个结果(4个房子面积 ...