简体   繁体   English

如何使用自动装配注释

[英]How to use Autowired annotations

I have a doubt to use autowire annotation in different scenarios... 我怀疑在不同情况下使用自动装配注释...

public Class A {
  @Autowired
  public B b;

  public void print() {
    System.out.println("the value is " + b.getValue());
  }

}

Example 2: 范例2:

public Class A {
  public B b;

  @Autowired
  Public A(B b){}

  public void print() {
    System.out.println("the value is " + b.getValue());
  }
}

The first method to autowire objects is called field-injection and the second one is called constructor-injection . 自动连接对象的第一种方法称为field-injection ,第二种称为构造函数-injection There are many posts on SO about these two topics! 关于这两个主题,有很多关于SO的帖子! Eg What exactly is Field Injection and how to avoid it? 例如, 什么是场注入以及如何避免场注入? This should answer all of your questions. 这应该回答您所有的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM