导读


由于项目需要,原本由一个spring项目bean的xml配置,但是现在使用的SpringBoot项目,就将其改为SpringBoot配置类。

使用


Spring的XML配置

  1. <beanid="eClient" class="com.alibaba.x.client.http.ExecutableClient"
  2. factory-method="getInstance"init-method="init" destroy-method="destroy">
  3. <propertyname="pal"value="https"></property>
  4. <propertyname="dName"value="?"></property>
  5. <propertyname="aKey"value="?"></property>
  6. <propertyname="sKey"value="?"></property>
  7. </bean>

SpringBoot的配置类

  1. import com.alibaba.x.client.http.ExecutableClient;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. public class ExecutableClientConfig {
  6. @Bean(name = "eClient")
  7. public ExecutableClient createDataSource() {
  8. ExecutableClient executableClient = new ExecutableClient();
  9. try {
  10. executableClient.setPal("https");
  11. executableClient.setDName("域名");
  12. executableClient.setAKey("key密");
  13. executableClient.setSKey("key钥");
  14. return executableClient;
  15. } catch (Exception e) {
  16. throw new RuntimeException(e);
  17. }
  18. }
  19. }

具体应用场景

  1. /**
  2. * 注入上述配置文件的配置
  3. */
  4. @Autowired
  5. private ExecutableClientConfig clientConfig;
  6. /**
  7. * 注入配置文件的配置
  8. */
  9. public String createInst(String code, String initData) {
  10. String api = "/api/createInfo.json";
  11. String apiResult = "";
  12. try {
  13. PostClient postClient = clientConfig.getExecutableClient().newPostClient(api);
  14. postClient.addParameter("code", code);
  15. postClient.addParameter("initData", initData);
  16. apiResult = postClient.post();
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. return apiResult;
  21. }

END


搞定~