title: Dynamic input parameter instead of <constructor-arg> in Spring for a class
body: <p>How can I get rid of the constructor-args in the spring-config.xml bean mapping and supply the value dynamically or from the String args[] parameter form the main method for the BattingStats class?</p>
  
  <p>I have the following Player class</p>
  
  <pre><code>    package com.scorer.game;
  
      public class Player {
          private BattingStats battingStats;
  
          public BattingStats getBattingStats() {
              return battingStats;
          }
  
          public void setBattingStats(BattingStats battingStats) {
              this.battingStats = battingStats;
          }
  
          @Override
          public String toString() {
              return "Player{" +
                      "runs scored=" + battingStats.getRunsScored() +
                      " balls faced=" + battingStats.getBallsFaced() +
                      " strike rate=" + battingStats.getStrikeRate() +
                      '}';
              }
          }
  </code></pre>
  
  <p>and class BattingStats as follows:</p>
  
  <pre><code>package com.scorer.game;
  
  public class BattingStats {
      private Integer battingPosition;
      private Integer ballsFaced;
      private Integer runsScored;
      private Integer fours;
      private Integer sixes;
      private Boolean isNotOut;
      private Boolean isRightHand;
      private Float strikeRate;
  
      public BattingStats(Integer battingPosition,
              Integer ballsFaced,
              Integer runsScored,
              Integer fours,
              Integer sixes,
              Boolean isNotOut,
              Boolean isRightHand) {
          this.battingPosition = battingPosition;
          this.ballsFaced = ballsFaced;
          this.runsScored = runsScored;
          this.fours = fours;
          this.sixes = sixes;
          this.isNotOut = isNotOut;
          this.isRightHand = isRightHand;
          this.strikeRate = (float)(((float)runsScored/(float)ballsFaced)*100);
      }
  
      public static BattingStats getInstance(Integer battingPosition,
                                             Integer ballsFaced,
                                             Integer runsScored,
                                             Integer fours,
                                             Integer sixes,
                                             Boolean isNotOut,
                                             Boolean isRightHand) {
          return new BattingStats(battingPosition, ballsFaced, runsScored, fours, sixes, isNotOut, isRightHand);
      }
  
      public Integer getBattingPosition() {
          return battingPosition;
      }
  
      public Integer getBallsFaced() {
          return ballsFaced;
      }
  
      public Integer getRunsScored() {
          return runsScored;
      }
  
      public Integer getFours() {
          return fours;
      }
  
      public Integer getSixes() {
          return sixes;
      }
  
      public Boolean getIsNotOut() {
          return isNotOut;
      }
  
      public Boolean getIsRightHand() {
          return isRightHand;
      }
  
      public Float getStrikeRate() {
          return strikeRate;
      }
  
      public void setBattingPosition(Integer battingPosition) {
          this.battingPosition = battingPosition;
      }
  
      public void setBallsFaced(Integer ballsFaced) {
          this.ballsFaced = ballsFaced;
      }
  
      public void setRunsScored(Integer runsScored) {
          this.runsScored = runsScored;
      }
  
      public void setFours(Integer fours) {
          this.fours = fours;
      }
  
      public void setSixes(Integer sixes) {
          this.sixes = sixes;
      }
  
      public void setIsNotOut(Boolean isNotOut) {
          this.isNotOut = isNotOut;
      }
  
      public void setIsRightHand(Boolean isRightHand) {
          this.isRightHand = isRightHand;
      }
  
      public void setStrikeRate(Float strikeRate) {
          this.strikeRate = strikeRate;
      }
  }
  </code></pre>
  
  <p>and the main class ass follows:</p>
  
  <pre><code>package com.scorer.app;
  
  import com.scorer.game.BattingStats;
  import com.scorer.game.Player;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class MainApp {
      public static void main(String args[]) {
          ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
          BattingStats battingStats = (BattingStats)context.getBean("battingStats");
          Player player = (Player)context.getBean("player");
          System.out.println(player.toString());
      }
  }
  </code></pre>
  
  <p>the spring-confix.xml is as follows</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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.xsd"&gt;
  
      &lt;bean id="player" class="com.scorer.game.Player" &gt;
          &lt;property name="battingStats" ref="battingStats"&gt;&lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="battingStats" class="com.scorer.game.BattingStats"&gt;
          &lt;constructor-arg type="java.lang.Integer" name="battingPosition" value = "1"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg type="java.lang.Integer" name="ballsFaced" value = "101"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg name ="runsScored" value = "52"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg name ="fours" value = "100"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg name ="sixes" value = "100"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg name ="isNotOut" value = "true"&gt;&lt;/constructor-arg&gt;
          &lt;constructor-arg name ="isRightHand" value = "true"&gt;&lt;/constructor-arg&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>