gpt4 book ai didi

java - Spring 中类的动态输入参数而不是

转载 作者:行者123 更新时间:2023-12-02 06:24:25 25 4
gpt4 key购买 nike

如何摆脱 spring-config.xml bean 映射中的构造函数参数并动态提供值或从 BattingStats 类的 main 方法中的 String args[] 参数提供值?

我有以下 Player 类

    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>

最佳答案

您可以使用 @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 */);
}
}

关于java - Spring 中类的动态输入参数而不是 <constructor-arg>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com