At sign Autowired in Setter methods

When Spring encounters an @AutoWired annotation that is used in setter methods, it attempts to perform byType automatic wiring. In other words, Setter methods with @autoWired are equivalent to byType autowire mode.

Here’s an example:

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
	   private SpellChecker spellChecker;
	   @Autowired
	   public void setSpellChecker( SpellChecker spellChecker ){
	      this.spellChecker = spellChecker;
	   }
	   public SpellChecker getSpellChecker( ) {
	      return spellChecker;
	   }
	   public void spellCheck(a) { spellChecker.checkSpelling(); }}Copy the code

Main.app:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); }}Copy the code

Beans. XML:


      

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <! -- Definition for textEditor bean without constructor-arg -->
   <bean id="textEditor" class="com.sap.TextEditor">
   </bean>

   <! -- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.sap.SpellChecker">
   </bean>

</beans>
Copy the code

Because is used by the Type assembly, so Beans. Com in the XML. SAP. SpellChecker id can be any specified:

After removing the @AutoWired annotation for Setter methods, TextEditor’s spellChecker injection does not occur, so nullPointer exceptions will occur.

Add the @autoWired method to the property

Doing so eliminates the need to explicitly write setter methods that depend on properties, reduces keystrokes, and makes the source code simpler.

TextEditor:

import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
   @Autowired
   private SpellChecker spellChecker;
   public TextEditor(a) {
      System.out.println("Inside TextEditor constructor." );
   }  
   public SpellChecker getSpellChecker( ){
      return spellChecker;
   }  
   public void spellCheck(a){ spellChecker.checkSpelling(); }}Copy the code

Beans. XML:


      

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <! -- Definition for textEditor bean -->
   <bean id="textEditor" class="com.sap.TextEditor">
   </bean>

   <! -- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.sap.SpellChecker">
   </bean>

</beans>
Copy the code

Also, if the @AutoWired annotation is removed, dependency injection of the SpellChecker member variable will not occur and nullPointer will be raised.

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: