Spring Boot起步依赖

 

Spring Boot起步依赖

Spring Boot起步依赖_第1张图片

  spring-boot-autoconfigure:包含大量第三方自动配置,还有一个文件名叫spring.factories的配置文件。

Spring Boot起步依赖_第2张图片

这里我们先看一下 mybatis.mapper.autoconfigure。引入mybatis基础得bean

@Configuration
//在SqlSessionFactory和SqlSessionFactoryBean存在时加载这个配置
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
//在DataSource这个bean存在时加载
@ConditionalOnBean({DataSource.class})
//将MybatisProperties引入
@EnableConfigurationProperties({MybatisProperties.class})
//在DataSourceAutoConfiguration加载后进行配置
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@AutoConfigureBefore(
    name = {"org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration"}
)
public class MapperAutoConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(MapperAutoConfiguration.class);
    private final MybatisProperties properties;
    private final Interceptor[] interceptors;
    private final ResourceLoader resourceLoader;
    private final DatabaseIdProvider databaseIdProvider;
    private final List configurationCustomizers;

    public MapperAutoConfiguration(MybatisProperties properties, ObjectProvider interceptorsProvider, ResourceLoader resourceLoader, ObjectProvider databaseIdProvider, ObjectProvider> configurationCustomizersProvider) {
        this.properties = properties;
        this.interceptors = (Interceptor[])interceptorsProvider.getIfAvailable();
        this.resourceLoader = resourceLoader;
        this.databaseIdProvider = (DatabaseIdProvider)databaseIdProvider.getIfAvailable();
        this.configurationCustomizers = (List)configurationCustomizersProvider.getIfAvailable();
    }

    @PostConstruct
    public void checkConfigFileExists() {
        if(this.properties.isCheckConfigLocation() && StringUtils.hasText(this.properties.getConfigLocation())) {
            Resource resource = this.resourceLoader.getResource(this.properties.getConfigLocation());
            Assert.state(resource.exists(), "Cannot find config location: " + resource + " (please add config file or check your Mybatis configuration)");
        }

    }

    @Bean
    //在这个bean不存在时再加载
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        if(StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
        }

 下面实现自定义SpringBoot起步依赖

@Configuration
@ConditionalOnClass({HttpClient.class})
@EnableConfigurationProperties(HttpClientProperties.class)
public class HttpClientAutoConfig {

    private final HttpClientProperties properties;

    public HttpClientAutoConfig(HttpClientProperties properties){
        this.properties = properties;
    }

    /**
     * httpClient bean定义
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(HttpClient.class)
    public HttpClient httpClient(){
        //构建 requestConfig
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(properties.getConnectTimeOut())
                .setSocketTimeout(properties.getSocketTimeOut())
                .build();
        HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
                .setUserAgent(properties.getAgent()).setMaxConnPerRoute(properties.getMaxConnPerRoute())
                .setConnectionReuseStrategy(new NoConnectionReuseStrategy()).build();
        return client;
    }
}
/**
 * 提供给用户进行配置 prefix 指定配置文件里的前缀
 * Created by xyzzg on 2020/2/26.
 */
@ConfigurationProperties(prefix = "spring.httpclient")
public class HttpClientProperties {

    private String agent = "agent";

    private Integer maxConnPerRoute = 10;

    private Integer maxConnTotaol = 50;

    private Integer connectTimeOut = 1000;

    private Integer socketTimeOut = 10000;
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceApplicationTests {

	@Autowired
	private HttpClient HttpClient;

	@Test
	public void contextLoads() throws IOException {

		System.out.println("___________________=====________________");
		System.out.println(
				EntityUtils.toString(
						HttpClient.execute(new HttpGet("https://www.baidu.com")).getEntity())
				);
		System.out.println("___________________=====________________");
	}

}

当然我们还可以在spring.factories的配置文件中,指定自定义config路径,使得spring能扫描到。

com.mooc.house.user.config.HttpClientAutoConfig

你可能感兴趣的:(Spring Boot起步依赖)