@Autowired注入的对象在注入之前就已经实例化,是从ioc容器中获取已经初始化的对象

new实例化一个对象,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();
    }
}
View Code

相关文章:

  • 2021-11-13
  • 2022-12-23
猜你喜欢
  • 2021-11-06
  • 2021-11-03
  • 2021-06-30
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-06-01
相关资源
相似解决方案