导读
由于项目需要,原本由一个spring项目bean的xml配置,但是现在使用的SpringBoot项目,就将其改为SpringBoot配置类。
使用
Spring的XML配置
<beanid="eClient" class="com.alibaba.x.client.http.ExecutableClient"
factory-method="getInstance"init-method="init" destroy-method="destroy">
<propertyname="pal"value="https"></property>
<propertyname="dName"value="?"></property>
<propertyname="aKey"value="?"></property>
<propertyname="sKey"value="?"></property>
</bean>
SpringBoot的配置类
import com.alibaba.x.client.http.ExecutableClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ExecutableClientConfig {
@Bean(name = "eClient")
public ExecutableClient createDataSource() {
ExecutableClient executableClient = new ExecutableClient();
try {
executableClient.setPal("https");
executableClient.setDName("域名");
executableClient.setAKey("key密");
executableClient.setSKey("key钥");
return executableClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
具体应用场景
/**
* 注入上述配置文件的配置
*/
@Autowired
private ExecutableClientConfig clientConfig;
/**
* 注入配置文件的配置
*/
public String createInst(String code, String initData) {
String api = "/api/createInfo.json";
String apiResult = "";
try {
PostClient postClient = clientConfig.getExecutableClient().newPostClient(api);
postClient.addParameter("code", code);
postClient.addParameter("initData", initData);
apiResult = postClient.post();
} catch (Exception e) {
e.printStackTrace();
}
return apiResult;
}
END
搞定~