SpringBoot自动配置全攻略(一)——如何实现自动配置

一、前言

SpringBoot 通过约定大于配置的做法,内置了很多AutoConfiguration,让我们能够快速搭建Spring的Web应用,大大提升了开发效率。

该系列文章会分为如何编写,测试,集成,原来4块来逐步讲解SpringBoot自动配置相关的知识。

这里就让我们来看看,如何编写属于自己的AutoConfiguration吧。Let`s Go !!!

 

二、工程结构

三、修改pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-msg</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring.boot.version>2.0.2.RELEASE</spring.boot.version>

    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

四、编写自动配置类


@Configuration
@EnableConfigurationProperties(MsgProperties.class)   
@ConditionalOnClass(MsgService.class)
@ConditionalOnProperty(prefix = "msg" , name = "enable" , matchIfMissing = true)
public class MsgAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(MsgService.class)
    public MsgService msgBean(){
        return new MsgService();
    }
}

五、编写注入的Property类

package com.faker.framework.configure;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @Auther: 
 * @Date: 18/11/26 15:21
 * @Description:
 */
@ConfigurationProperties(prefix = "msg")
public class MsgProperties {
    /**
     * 用户名
     */
    private String username;

    private String pwd;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

六、编写自动配置注入的Serivce

package com.faker.framework.configure;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Auther:
 * @Date: 18/6/13 15:12
 * @Description:
 */

public class MsgService implements InitializingBean {

    @Autowired
    private MsgProperties msgProperties;


    public void afterPropertiesSet() throws Exception {
        System.out.println("username: "+msgProperties.getUsername());
        System.out.println("pwd: "+msgProperties.getPwd());
    }
}

七、在resource目录下添加打包配置

创建META-INF文件夹,在文件夹下创建spring.factories配置文件

并在改配置文件中添加内容与自己的自动配置类对应,这样在打包被引入后,SpringBoot能

识别你自定义的自动配置类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.faker.framework.configure.MsgAutoConfiguration

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值