springboot @Autowired 不建议使用字段注入
作者:互联网
因为这种注入方式存在缺陷,springboot不建议使用这种方式。可以采用构造方法的方式来注入
修改前:
@RestController public class UserController { @Autowired UserMapper userMapper; @RequestMapping("/select") public String select(){ return userMapper.selectAll().toString(); } }
修改后:
@RestController public class UserController { final UserMapper userMapper; public UserController(UserMapper userMapper){ this.userMapper = userMapper; } @RequestMapping("/select") public String select(){ return userMapper.selectAll().toString(); } }
标签:selectAll,springboot,Autowired,UserMapper,userMapper,UserController,public,selec 来源: https://www.cnblogs.com/qinggua/p/15184455.html