package com.mooc1.house.autoconfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="spring.httpclient")
public class HttpClientProperties {
private Integer connectTimeOut = 1000;
private Integer socketTimeOut = 10000;
private String agent = "agent";
private Integer maxConnPerRoute = 10;
private Integer maxConnTotaol = 50;
public Integer getConnectTimeOut() {
return connectTimeOut;
}
public void setConnectTimeOut(Integer connectTimeOut) {
this.connectTimeOut = connectTimeOut;
}
public Integer getSocketTimeOut() {
return socketTimeOut;
}
public void setSocketTimeOut(Integer socketTimeOut) {
this.socketTimeOut = socketTimeOut;
}
public String getAgent() {
return agent;
}
public void setAgent(String agent) {
this.agent = agent;
}
public Integer getMaxConnPerRoute() {
return maxConnPerRoute;
}
public void setMaxConnPerRoute(Integer maxConnPerRoute) {
this.maxConnPerRoute = maxConnPerRoute;
}
public Integer getMaxConnTotaol() {
return maxConnTotaol;
}
public void setMaxConnTotaol(Integer maxConnTotaol) {
this.maxConnTotaol = maxConnTotaol;
}
}
说明:
1)这里相当于配置文件中定义的配置;
2)每个属性相当于配置文件中spring.httpclient.属性名=对应值;
package com.mooc1.house.autoconfig;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass({HttpClient.class})
@EnableConfigurationProperties(HttpClientProperties.class)
public class HttpClientAutoConfiguration {
private final HttpClientProperties properties;
public HttpClientAutoConfiguration(HttpClientProperties properties){
this.properties = properties;
}
/**
* httpclient bean 的定义
* 有三种方式都可以让HttpClientAutoConfiguration这个自动配置生效
* 1.通过将自动配置所在package成为注解了@SpringBootApplication启动类的子package
* 2.通过定义META-INF/spring.factories文件,里面添加EnableAutoConfiguration与自动配置的映射关系
* 3.通过在启动类中添加注解EnableHttpClient,EnableHttpClient要@Import(HttpClientAutoConfiguration.class)
* @return
*/
@Bean
@ConditionalOnMissingBean(HttpClient.class)
public HttpClient httpClient(){
//构建requestConfig
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(properties.getConnectTimeOut()) // 设置连接超时时间,默认1秒
.setSocketTimeout(properties.getSocketTimeOut()).build(); // 设置读超时时间,默认10秒
HttpClient client = HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig) // 设置requestConfig
.setUserAgent(properties.getAgent()) // 设置User-Agent
.setMaxConnPerRoute(properties.getMaxConnPerRoute()) // 设置一个远端IP最大的连接数
.setMaxConnTotal(properties.getMaxConnTotaol()) // 设置总的连接数
// .setConnectionReuseStrategy(new NoConnectionReuseStrategy()) // 不重用连接,默认是重用,建议保持默认开启连接池,节省建立连接开销
.build();
return client;
}
}
说明:
1)@Configuration:从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
2)@ConditionalOnClass:参考https://blog.csdn.net/andy_zhang2007/article/details/81284875
3)@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效。
package com.mooc1.house.autoconfig;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(HttpClientAutoConfiguration.class)
public @interface EnableHttpClient {
}
说明:
1)@Target(ElementType.TYPE) // 接口、类、枚举、注解
2)@Retention(RetentionPolicy.RUNTIME) // 注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
3)@Import注解就是之前xml配置中的import标签,可以用于依赖第三方包中bean的配置和加载
package com.mooc.house;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import com.mooc1.house.autoconfig.EnableHttpClient;
@SpringBootApplication
@EnableHttpClient
@EnableAsync
public class HouseApplication {
public static void main(String[] args) {
SpringApplication.run(HouseApplication.class, args);
}
}
说明:
1)通过在启动类中添加注解EnableHttpClient,EnableHttpClient要@Import(HttpClientAutoConfiguration.class);
package com.mooc.house;
import java.io.IOException;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HouseApplicationTest {
/**
* 检测我们编写的自动配置HttpClientAutoConfiguration有没有生效
*/
@Autowired
private HttpClient httpClient;
/**
* httpclient bean 的定义
* 有三种方式都可以让HttpClientAutoConfiguration这个自动配置生效
* 1.通过将自动配置所在package成为注解了@SpringBootApplication启动类的子package
* 2.通过定义META-INF/spring.factories文件,里面添加EnableAutoConfiguration与自动配置的映射关系
* 3.通过在启动类中添加注解EnableHttpClient,EnableHttpClient要@Import(HttpClientAutoConfiguration.class)
* @return
*/
@Test
public void testHttclient() throws ParseException, ClientProtocolException, IOException{
//访问百度,输出百度网页
System.out.println(EntityUtils.toString(httpClient.execute(new HttpGet("http://www.hao123.com")).getEntity()));
}
}
说明:
1)检测我们编写的自动配置HttpClientAutoConfiguration有没有生效
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================