@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(); } }