एनाटेट @ संपत्ति के लिए क्या किया गया है या इसे सेटटर में क्या करना है?वसंत @Autowire संपत्ति बनाम सेटर
जहाँ तक मुझे पता है कि दोनों के पास एक ही परिणाम है, लेकिन क्या किसी दूसरे का उपयोग करने का कोई कारण है?
अद्यतन (अधिक संक्षिप्त होने के लिए)
इस
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
और इस
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public TextEditor() {
System.out.println("Inside TextEditor constructor.");
}
public void spellCheck(){
spellChecker.checkSpelling();
}
}
मुझे नहीं पता कि इस प्रश्न का उत्तर क्यों माना जाएगा। मैं जानना चाहता हूं कि एक सेटर में या सीधे संपत्ति में @Autowired का उपयोग करने के बीच व्यावहारिक अंतर है या नहीं। यह नहीं पूछ रहा कि कौन सा बेहतर है, अगर कोई फर्क पड़ता है तो – luso