gpt4 book ai didi

java - 如何使用 xml 配置构建以下 bean?

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

如何使用 xml 配置构建以下 bean?

@Configuration
public class RestConfig {
@Bean
public ResteasyClient restClient() {
return new ResteasyClientBuilder()
.establishConnectionTimeout(connectionTimeout, TimeUnit.SECONDS)
.socketTimeout(socketTimeout, TimeUnit.SECONDS)
.build();
}
}

最佳答案

您可以尝试实现FactoryBean接口(interface):

public class ResteasyClientFactoryBean implements FactoryBean<ResteasyClient>{

private int connectionTimeout;
private int socketTimeout;


public ResteasyClient getObject() throws Exception {
return new ResteasyClientBuilder()
.establishConnectionTimeout(connectionTimeout, TimeUnit.SECONDS)
.socketTimeout(socketTimeout, TimeUnit.SECONDS)
.build();;
}

public Class<? extends ResteasyClient> getObjectType() {
return ResteasyClient.class;
}

public boolean isSingleton() {
return true;
}

public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
}

并添加以下 bean 定义到配置中:

<beans ..."> 
<bean name="myResteasyClient" class="ResteasyClientFactoryBean">
<property name="connectionTimeout" value="xxx"/>
<property name="socketTimeout" value="xxx"/>
</bean>
</beans>

然后你可以将此bean注入(inject)到另一个bean中,它将被解析为ResteasyClient实例。

关于java - 如何使用 xml 配置构建以下 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028872/

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