私は、使用しているBeanがプロパティファイルからいくつかのフィールドを取り込み、他は動的に(APIコールから)読み込む必要があるシナリオを考えました。オブジェクトの新規作成の自動化
@Configuration
@ConfigurationProperties(prefix="studio")
public class Studio {
private String areaCode; // loads from application.properties
private String hours; // loads from application.properties
private String groupCode; // loads from application.properties
private Address address; // loads from a api
private String id; // loads from a api
public Studio(String id, String city, String subdivision,
String addressLine1, String postalCode) {
Address address = Address.builder()
.street(addressLine1)
.city(city)
.postalCode(postalCode)
.state(subdivision)
.build();
this.id = id;
this.address = address;
}
}
今すぐ動的なフィールドを移入する方法は、このようなものです::そのクラスの
private List<Studio> getStudioDataFromApi(ResponseEntity<String> exchange)
throws Exception {
List<Studio> infoList = $(exchange.getBody())
.xpath("Area[TypeCode=\"CA\"]")
.map(
Area -> new Studio(
$(Area).child("Id").text(String.class),
$(Area).child("Address").child("City").text(String.class),
$(Area).child("Address").child("Subdivision").text(String.class),
$(Area).child("Address").child("AddressLine1").text(String.class),
$(Area).child("Address").child("PostalCode").text(String.class))
);
return infoList;
}
I Autowireメーカー は、ここに私のBeanです。これを実行するたびに、プロパティファイルからnullに設定されたフィールドが取得されます。私はその理由を知ることができます。新しい、autowired beanについて何も知らないのです。私の質問はどのように私は両方を使用することができますか?すなわち、新しいものがアップされたときに常に設定からいくつかのフィールドが設定されているBeanを使用する。 コンテキストの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" xmlns:beans="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean class="org.springframework.batch.core.scope.StepScope" />
<bean id="ItemReader" class="com.sdm.studio.reader.StudioReader" scope="step">
<property name="studio" ref="Studio" />
</bean>
<bean id="Studio" class="com.sdm.studio.domain.Studio" />
</bean>
:
と私たちのapplication.propertiesファイル2つのクラスに分かれています。 1つのクラスは、構成データを保持するためのConfigurationPropertiesクラスでなければなりません。もう1つは通常のBeanで、スタジオ – Gary
@Garyなどの依存関係をAutowireできる場合は、それらを結合する最も良い方法は何ですか?私はこれらのフィールドを外部のapiに送る必要があります – Yana