Q:

Dynamic input parameter instead of  in Spring for a class

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?
I have the following Player class
    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() +
                    '}';
            }
        }

and class BattingStats as follows:
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;
    }
}

and the main class ass follows:
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());
    }
}

the spring-confix.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean id="player" class="com.scorer.game.Player" >
        <property name="battingStats" ref="battingStats"></property>
    </bean>
    <bean id="battingStats" class="com.scorer.game.BattingStats">
        <constructor-arg type="java.lang.Integer" name="battingPosition" value = "1"></constructor-arg>
        <constructor-arg type="java.lang.Integer" name="ballsFaced" value = "101"></constructor-arg>
        <constructor-arg name ="runsScored" value = "52"></constructor-arg>
        <constructor-arg name ="fours" value = "100"></constructor-arg>
        <constructor-arg name ="sixes" value = "100"></constructor-arg>
        <constructor-arg name ="isNotOut" value = "true"></constructor-arg>
        <constructor-arg name ="isRightHand" value = "true"></constructor-arg>
    </bean>
</beans>

A:

You can use @Configuration annotation to construct your beans using Java instead of XML, enabling you to dynamically calculate whatever variables you need to pass into bean constructors.
package com.example;

import java.net.URI;
import java.net.URISyntaxException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.scorer.game.BattingStats;

@Configuration
public abstract class DatabaseConfiguration {

    @Bean
    public BasicDataSource createDataSource() throws URISyntaxException {
        URI dbUri = new URI(System.getenv("DATABASE_URL"));

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(dbUrl);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        basicDataSource.setTestOnBorrow(true);
        basicDataSource.setTestOnReturn(true);
        basicDataSource.setTestWhileIdle(true);
        basicDataSource.setTimeBetweenEvictionRunsMillis(1800000);
        basicDataSource.setNumTestsPerEvictionRun(3);
        basicDataSource.setMinEvictableIdleTimeMillis(1800000);
        basicDataSource.setValidationQuery("SELECT version();");

        return basicDataSource;
    }

    @Bean(name = "persistenceXmlLocation")
    public String persistenceXmlLocation() {
        return "classpath:META-INF/persistence.xml";
    }

    /* --------- UPDATE ---------------- */
    @Bean
    public BattingStats battingStats() {
        Integer battingPosition = methodForDynamicallyCalculatingBattingPosition();
        // call the rest of your "dynamic" methods here
        return new BattingStats(battingPosition, /* the rest of your dynamic arguments go here */);
    }
}

