Spring自动装配(Autowired)的类字段和方法之间的区别

huangapple go评论90阅读模式
英文:

Spring autowired difference between class field and method

问题

以下是翻译好的部分:

"我是Spring框架的新手,正在尝试弄清楚在类的字段和类的方法上使用Spring的@Autowired注解之间的区别,就像下面的IUtil和IUserDao一样:

@Controller
@RequestMapping("/user")
public class UserController{

  1. @Autowired
  2. private IUtil util;
  3. private IUserDao userDao;
  4. @Autowired
  5. public UserController(IUserDao dao){
  6. this.userDao = dao;
  7. }
  8. ...
  9. ...
  10. ...

}"

"IUtil是通过字段注入的,而IUserDao是通过构造函数注入的,它们之间有什么区别?

提前感谢。"

英文:

I'm new to Spring framework, and try to figure out what is the difference between spring autowired at class's field and class's method, like below's IUtil and IUserDao:

> @Controller
> @RequestMapping("/user")
> public class UserController{
>
> @Autowired
> private IUtil util;
>
> private IUserDao userDao;
>
> @Autowired
> public UserController(IUserDao dao){
> this.userDao = dao;
> }
>
> ...
> ...
> ...
> }

IUtil is inject by field, and IUserDao is inject by constructor, what's the difference between them?

Thanks in advance.

答案1

得分: 0

"注射的时间点" 不同;

考虑以下代码:

  1. public class Index {
  2. public static void main(String[] args) {
  3. ApplicationContext AcContext = new AnnotationConfigApplicationContext(MyConfigration.class);
  4. }
  5. }
  6. @Configuration
  7. class MyConfigration {
  8. @Bean
  9. public UserController userController() { return new UserController(); }
  10. @Bean
  11. public IUtil util() { return new Util(); }
  12. @Bean
  13. public IUserDao userDao() { return new UserDao(); }
  14. }
  15. interface IUtil {
  16. public String getDate();
  17. }
  18. interface IUserDao {}
  19. class Util implements IUtil {
  20. public String date = "2023-06-26";
  21. @Override
  22. public String getDate() {
  23. return date;
  24. }
  25. }
  26. class UserDao implements IUserDao {}
  27. class UserController {
  28. @Autowired
  29. private IUtil util;
  30. private String date;
  31. public UserController(){
  32. this.date = util.getDate();
  33. }
  34. }
  35. 思考一下执行这段代码会发生什么
  36. 答案是在 `this.date = util.getDate();` 处会发生空指针异常
  37. 因为在通过类的属性进行注入时是在类实例化之后进行的
  38. 回到你的示例在你的UserController初始化中你可以对userDao做一些操作但在初始化时不能使用util
  39. <details>
  40. <summary>英文:</summary>
  41. The &quot;time point&quot; of injection is different;
  42. Consider the following code
  43. public class Index {
  44. public static void main(String[] args) {
  45. ApplicationContext AcContext = new AnnotationConfigApplicationContext(MyConfigration.class);
  46. }
  47. }
  48. @Configuration
  49. class MyConfigration {
  50. @Bean
  51. public UserController userController() { return new UserController(); }
  52. @Bean
  53. public IUtil util() { return new Util(); }
  54. @Bean
  55. public IUserDao userDao() { return new UserDao(); }
  56. }
  57. interface IUtil {
  58. public String getDate();
  59. }
  60. interface IUserDao {}
  61. class Util implements IUtil {
  62. public String date = &quot;2023-06-26&quot;;
  63. @Override
  64. public String getDate() {
  65. return date;
  66. }
  67. }
  68. class UserDao implements IUserDao {}
  69. class UserController {
  70. @Autowired
  71. private IUtil util;
  72. private String date;
  73. public UserController(){
  74. this.date = util.getDate();
  75. }
  76. }
  77. Think about it, execute this code, what will happen?
  78. The answer is that a null pointer exception will occur when `this.date = util.getDate();`
  79. Because when injecting through the properties of the class, it is after the class is instantiated.
  80. Going back to your example, in your example, in your UserController initialization, you can do something with userDao, but in initialization you cannot use util
  81. </details>

huangapple
  • 本文由 发表于 2023年6月26日 11:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定