![](/img/trans.png)
[英]Spring dependency injection with @Autowired annotation without setter
[英]Why is the Spring Framework Not Logging Autowired Setter Injection?
我是 Spring 的新手,目前正在學習依賴注入。 我正在使用 Spring Boot 2.2.2
如果我使用基於構造函數的指令,我會在日志中看到它正在自動裝配。
這是我的代碼:
// Sorting an array
@Autowired
private SortAlgrithm sortAlgorithm;
//^Tell Spring this is a dependency
public BinarySearchImpl(SortAlgrithm sortAlgorithm) {
super();
this.sortAlgorithm = sortAlgorithm;
}
這是我的日志,其中反映了 Spring 處理的自動裝配過程:
2020-01-04 20:34:30.435 DEBUG 10396 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'springIn10StepsApplication'
2020-01-04 20:34:30.441 DEBUG 10396 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'binarySearchImpl'
2020-01-04 20:34:30.446 DEBUG 10396 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'bubbleSortAlgorithm'
2020-01-04 20:34:30.447 DEBUG 10396 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Autowiring by type from bean name 'binarySearchImpl' via constructor to bean named 'bubbleSortAlgorithm'
現在,如果我使用 setter 注入稍微更改代碼:
private SortAlgorithm sortAlgorithm;
// Setter injection
@Autowired
public void setSortAlgorithm(SortAlgorithm sortAlgorithm) {
System.out.println("setter called");
this.sortAlgorithm = sortAlgorithm;
}
並提供日志:
2020-01-04 20:37:23.853 DEBUG 7600 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.context.internalConfigurationPropertiesBinderFactory'
2020-01-04 20:37:23.858 DEBUG 7600 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'springIn10StepsApplication'
2020-01-04 20:37:23.863 DEBUG 7600 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'binarySearchImpl'
2020-01-04 20:37:23.870 DEBUG 7600 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'bubbleSortAlgorithm'
setter called
我包含了要打印出來的“setter called”,讓我知道正在調用 setter 注入,因為它沒有被包含在日志中。 我想知道這是否是 Spring 的僥幸,其他人是否也遇到同樣的事情。 我知道我的 bean 在應用程序上下文中進行管理,並且在運行時,Spring 正在尋找依賴項。 @Autowire 告訴 Spring 我的依賴項是什么並適當地注入。 第一個日志代表該行為。 但是,'setter called' 被打印出來的事實,我想知道這是否意味着 Spring 正在通過調用 setter 方法執行自動裝配,即使它沒有反映在日志中。
在你的第一種情況下,setter 注入甚至沒有被觸發,它的構造函數注入發生在那里,因此沒有日志。
從docs
自動裝配構造函數:
.....
如果一個類只聲明一個構造函數開始,它將始終被使用,即使沒有注釋。 帶注釋的構造函數不必是公共的。
例如,如果你有這樣的事情:
@Component
public class TestAbd {
@Autowired
private OtpService otpService;
public TestAbd(OtpService otpService) {
System.out.println("Setting OTP service thorugh constructor injection.");
this.otpService = otpService;
}
public void setOtpService(OtpService otpService) {
System.out.println("Setting OTP service thorugh setter injection.");
this.otpService = otpService;
}
}
您將擁有以下日志
...
Setting OTP service thorugh constructor injection.
2020-01-05 10:44:50:743 [main]
...
因此,在您的第一種情況下,由於您已經聲明了一個構造函數,它將始終用於設置您的依賴項,因此整個 setter 注入過程都將被跳過。
試試logging.level.org.springframework=TRACE
。
調試 output 取決於 springframework 版本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.