Here’s an example:

public class TextEditor {
   private SpellChecker spellChecker;
   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );
      this.spellChecker = spellChecker;
   }
   // a getter method to return spellChecker
   public SpellChecker getSpellChecker(a) {
      return spellChecker;
   }
   public void spellCheck(a) { spellChecker.checkSpelling(); }}Copy the code

Another dependent class file, spellChecker.java, contains:

public class SpellChecker {
   public SpellChecker(a){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(a) {
      System.out.println("Inside checkSpelling."); }}Copy the code

Beans.xml contents:


      

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

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

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

</beans>
Copy the code

Execution Result:

Why is the Spring IOC container so smart about calling the setSpellChecker method of TextEditor?

Call the set method using Java reflection:

How is SpellChecker’s writeMethod resolved?

Here you can see, GenericTypeAwarePropertyDescriptor trying to parse the Bean class set method as writeMethod:

Change setSpellChecker to setSpellChecker, will encounter exceptions: org. Springframework. Beans. NotWritablePropertyException: Invalid property ‘spellChecker’ of bean class [com.sap.texteditor]: Bean property ‘spellChecker’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Complete call stack:

Jul 24.2020 4:13:04 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ee285c6: startup date [Fri Jul 24 16:13:04 CST 2020]; root of context hierarchy
Jul 24.2020 4:13:05 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Inside SpellChecker constructor.
Jul4:13:05 24, 2020PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml] :Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor] :Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'textEditor' defined in class path resource [Beans.xml] :Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor] :Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java: 1650).at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java: 1357).at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java: 582).at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java: 502).at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0 (AbstractBeanFactory.java: 312).at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java: 228).at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java: 310).at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java: 200).at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java: 758).at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java: 868).at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java: 549).at org.springframework.context.support.ClassPathXmlApplicationContext. <init> (ClassPathXmlApplicationContext.java: 144).at org.springframework.context.support.ClassPathXmlApplicationContext. <init> (ClassPathXmlApplicationContext.java: 85).at com.sap.MainApp.main(MainApp.java11) :Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'spellChecker' of bean class [com.sap.TextEditor] :Bean property 'spellChecker' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
	at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java: 247).at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java: 426).at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java: 278).at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java: 266).at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java: 97).at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java: 77).at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1646)
	... 13 more
Copy the code

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