SpringBoot-定义自己的auto-configuration

1.前言

Spring Boot内部定义了各种各样的xxxAutoConfiguration配置类,预先定义好了各种所需的Bean。只有在特定的情况下这些配置类才会被起。 那我们如何定义一个自己的Configuration呢?顺便了解一下原理?


2.那我们开始吧,哈哈哈

2.1 新建一个Maven工程

SpringBoot-定义自己的auto-configuration_第1张图片

<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-helloartifactId>
<version>1.0-SNAPSHOTversion>

2.2 修改pom文件

<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.0modelVersion>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-helloartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
            <version>1.4.3.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <version>1.4.3.RELEASEversion>
            <optional>trueoptional>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

2.3 创建HelloServiceProperties类,自动属性配置类

SpringBoot-定义自己的auto-configuration_第2张图片

package com.cxx.springboot.starter.hello;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * User: lanxinghua
 * Date: 2018/10/7 21:36
 * Desc: hello 自动配置属性类
 */
@ConfigurationProperties(prefix = "hello")
public class HelloServiceProperties {
    private static final String MSG = "world";
    private String msg = MSG;

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}


2.4 创建HelloService,服务类
package com.cxx.springboot.starter.hello;

/**
 * User: lanxinghua
 * Date: 2018/10/7 21:38
 * Desc: 服务类
 */
public class HelloService {
    private String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String sayHello(){
        return "hello" + msg;
    }
}


2.5 创建HelloServiceAutoConfiguration,自动配置类
package com.cxx.springboot.starter.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * User: lanxinghua
 * Date: 2018/10/7 21:40
 * Desc: 自动配置类
 */
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class) //判断该类在类路径下是否存在
@ConditionalOnProperty(prefix = "hello", value = "ebabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}

2.6 注册配置

1、在src/main/resources新建META-INF文件夹
2、在META-INF文件夹下新建spring.factories文件
3、注册配置自动配置类
SpringBoot-定义自己的auto-configuration_第3张图片

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.cxx.springboot.starter.hello.HelloServiceAutoConfiguration

2.7 将上面构建的starter安装到本地
mvn clean install

3.使用自定义的Configuration

新建一个springboot项目,添加依赖
SpringBoot-定义自己的auto-configuration_第4张图片

SpringBoot-定义自己的auto-configuration_第5张图片

测试一下:

package com.example;
import com.cxx.springboot.starter.hello.HelloService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@SpringBootApplication
public class DesignsApplication {
    @Resource
    private HelloService helloService;
    @RequestMapping("/test")
    public String test(){
        return helloService.sayHello();
    }
    public static void main(String[] args) {
        SpringApplication.run(DesignsApplication.class, args);
    }
}

SpringBoot-定义自己的auto-configuration_第6张图片

你可能感兴趣的:(工作实习)