title: Getting org.springframework.beans.NotWritablePropertyException
body: <p>I am currently learning Spring. I am in dependency injection topic and I am trying to practice injecting Reference obejct into setters through DI. But I am facing a issue.</p>
  <p>Client.java</p>
  <pre><code>package bean.driver;
  
  import bean.Car;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class Client {
  
      public static void main(String [] args)
      {
          String files[] = new String[]{&quot;engine.xml&quot;,&quot;car.xml&quot;};
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext(files);
  
          Car car = (Car) applicationContext.getBean(&quot;car&quot;);
          //car.printdata();
          car.printcardata();
  
      }
  }
  </code></pre>
  <p>Car.java</p>
  <pre><code>package bean;
  
  public class Car {
  
      private String carname;
      private Engine engine;
      private int cost;
      private int yearofRelease;
      private int maxspeed;
      private Inventory inventory;
      private String engineversion;
  
  
      /*Car(String carname, Engine engine)
      {
          this.carname = carname;
          this.engine = engine;
      }*/
      Car(String name,Engine engine,Inventory inventory, int maxspeed)
      {
          this.carname = name;
          this.maxspeed = maxspeed;
          this.engine = engine;
          this.inventory = inventory;
      }
  
      public void setCost(int cost)
      {
          this.cost = cost;
      }
      public void setYearofRelease(int yearofRelease)
      {
          this.yearofRelease = yearofRelease;
          System.out.println(engine.getEngineversion());
      }
      /*public void setEngine(Engine engine2)
      {
          this.engineversion = engine2.getEngineversion();
      }
      public String getEngineversion()
      {
          return engineversion;
      }*/
  
      public void setEngineVersion(Engine engine)
      {
          this.engineversion = engine.getEngineversion();
      }
      public void printdata()
      {
          System.out.println(carname + &quot; &quot;+ engine.getModel() +&quot; &quot;+cost+&quot; &quot;+yearofRelease);
      }
  
      public void printcardata()
      {
          System.out.println(&quot;Carname:-&quot;+carname+&quot;\n&quot;+&quot;Cost:-&quot;+cost+&quot;\n&quot;+&quot;MaxSpeed:-&quot;+maxspeed+&quot;\n&quot;+
                  &quot;Year of Release:-&quot;+yearofRelease+&quot;\n&quot;+&quot;Engine model:-&quot;+engine.getModel()+&quot;\n&quot;+
                  &quot;No of Units:-&quot;+inventory.getNoofinitalunits()+&quot;\n&quot;+&quot;Engine Version:-&quot;+engineversion);
      }
  
  }
  </code></pre>
  <p>Engine.java</p>
  <pre><code>package bean;
  
  public class Engine {
  
      private String model;
      private String engineversion;
  
      Engine(String model)
      {
          this.model = model;
      }
  
      public void setEngineversion(String engineversion) {
          this.engineversion = engineversion;
      }
  
      public String getModel()
      {
          return model;
      }
  
      public String getEngineversion()
      {
          return engineversion;
      }
  }
  </code></pre>
  <p>car.xml</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN 2.0//EN&quot;
          &quot;https://www.springframework.org/dtd/spring-beans-2.0.dtd&quot;&gt;
  
  &lt;beans&gt;
  
  
      &lt;bean id=&quot;car&quot; class=&quot;bean.Car&quot;&gt;
  
          &lt;!--dependecy injection through constructor of secondary type--&gt;
           &lt;constructor-arg ref=&quot;engine&quot;/&gt;&lt;!-- we are passing object as a reference --&gt;
  
           &lt;constructor-arg&gt;
           &lt;ref bean=&quot;inventory&quot;&gt;&lt;/ref&gt;
          &lt;/constructor-arg&gt;&lt;!-- passing object as a refernece --&gt;
  
          &lt;!-- pass object as a value --&gt;
  
          &lt;!--&lt;constructor-arg&gt;
              &lt;bean class=&quot;bean.Engine&quot;&gt;
                  &lt;constructor-arg&gt;
                      &lt;value&gt;S10&lt;/value&gt;&lt;/constructor-arg&gt;
              &lt;/bean&gt;
          &lt;/constructor-arg&gt;--&gt;
  
          &lt;!--dependency injection through constructor of primary type--&gt;
          &lt;constructor-arg value=&quot;Suzuki&quot;/&gt;
          &lt;constructor-arg&gt;
              &lt;value&gt;230&lt;/value&gt;
          &lt;/constructor-arg&gt;
  
          &lt;!--dependency injection through setters of primary type--&gt;
          &lt;property name=&quot;cost&quot; value=&quot;10&quot;/&gt;
          &lt;property name=&quot;yearofRelease&quot;&gt;
              &lt;value&gt;1994&lt;/value&gt;
          &lt;/property&gt;
  
          &lt;!--dependecy injection through setter of secondary type--&gt;
  
          &lt;property name=&quot;engine&quot; ref=&quot;engine&quot;&gt;&lt;/property&gt;
  
  
      &lt;/bean&gt;
  
      &lt;bean id=&quot;inventory&quot; class=&quot;bean.Inventory&quot;&gt;
          &lt;constructor-arg&gt;
              &lt;value&gt;1000&lt;/value&gt;
          &lt;/constructor-arg&gt;
      &lt;/bean&gt;
  
      &lt;bean id=&quot;engine2&quot; class=&quot;bean.Engine&quot;&gt;
          &lt;constructor-arg value=&quot;S88&quot;/&gt;
          &lt;property name=&quot;engineversion&quot; value=&quot;V11&quot;/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  <p>engine.xml</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE beans PUBLIC &quot;-//SPRING//DTD BEAN 2.0//EN&quot;
          &quot;https://www.springframework.org/dtd/spring-beans-2.0.dtd&quot;&gt;
  
  &lt;beans&gt;
  
      &lt;bean id=&quot;engine&quot; class=&quot;bean.Engine&quot;&gt;
  
          &lt;constructor-arg value=&quot;S88&quot;/&gt;
          &lt;property name=&quot;engineversion&quot; value=&quot;V11&quot;/&gt;
  
  
      &lt;/bean&gt;
  
      &lt;bean id=&quot;engine2&quot; class=&quot;bean.Engine&quot;&gt;
  
          &lt;constructor-arg value=&quot;S88&quot;/&gt;
          &lt;property name=&quot;engineversion&quot; value=&quot;V11&quot;/&gt;
  
  
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  <p>Error</p>
  <pre><code>&quot;C:\Program Files\Java\jdk-15.0.1\bin\java.exe&quot; &quot;-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\lib\idea_rt.jar=51693:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin&quot; -Dfile.encoding=UTF-8 -classpath D:\Spring-2\target\classes;C:\Users\chris\Downloads\spring-beans-5.3.15.jar;C:\Users\chris\Downloads\spring-context-5.3.15.jar;C:\Users\chris\Downloads\spring-core-5.3.15.jar;C:\Users\chris\Downloads\spring-expression-5.3.15.jar;C:\Users\chris\Downloads\commons-logging-1.2.jar bean.driver.Client
  V11
  Feb 06, 2022 1:53:20 PM 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 'car' defined in class path resource [car.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'engine' of bean class [bean.Car]: Bean property 'engine' 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 &quot;main&quot; org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'car' defined in class path resource [car.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'engine' of bean class [bean.Car]: Bean property 'engine' 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:1744)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:144)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:95)
      at bean.driver.Client.main(Client.java:12)
  Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'engine' of bean class [bean.Car]: Bean property 'engine' 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:243)
      at org.springframework.beans.AbstractNestablePropertyAccessor.processLocalProperty(AbstractNestablePropertyAccessor.java:432)
      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:104)
      at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:79)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1740)
      
  
  Process finished with exit code 1
  </code></pre>
  <p>See the property name is engine and also in xml document I have mentioned as engine only. But still getting the error.</p>