如何摆脱spring-config.xml bean映射中的constructor-args并动态提供值,或者从String args []参数中提供BattingStats类的主要方法?
我有以下玩家类
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() +
'}';
}
}
并将BattingStats分类如下:
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;
}
}
主要的课程是:
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());
}
}
spring-confix.xml如下
<?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>
答案 0 :(得分:2)
您可以使用@Configuration批注使用Java而不是XML构建bean,使您能够动态计算传递给bean构造函数所需的任何变量。
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 */);
}
}