4. 看看 Spring Boot 自动配置的原理?

首先我们从构建一个自定义的Starter入手:

这边先提一下一些关键点, 包括

  1. 自动注入的代码模块。
    2 . starter : 一些自动注入的依赖包引入module, 更多时候,它充当的作用就是引入jars。

一些规范点 :

  1. 命名规范, Spring boot 官方的starter是以 spring-boot-*-starter, 因此不要以 spring-boot 开头来命名你自定义的starter.

好了我们开始创建我们自己的Starter。

(1) 创建 property bean, 用来装载配置的属性。

package com.shopping.configs;

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

/**
 * @program: shopping
 * @author: Eric
 * @create: 2019-05-04 18:46
 **/


@ConfigurationProperties(prefix = "hello")
public class HelloProperties {


private String message = "Hello";


public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}
}

(2)定义一个向外配置的服务类。

package com.shopping.service;

/**
 * @program: shopping
 * @author: Eric
 * @create: 2019-05-04 20:27
**/

/**
 * 用来对外提供服务。
 */
public class HelloService {

private String message="defaultMessage";

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}
}

(3)定义一个自动配置的装配类,外部引用可以直接注入该bean, 将转载属性之后的HelloProperties bean 注入到 HelloService, 并且暴露HelloService。

package com.shopping.autoconfiguration;

import com.shopping.configs.HelloProperties;
import com.shopping.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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;

/**
 * @program: shopping
 * @author: Eric
 * @create: 2019-05-04 20:31
 **/


@Configuration
@ConditionalOnClass(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloAutoConfiguration {


@Autowired
private HelloProperties helloProperties;


@Bean
public HelloService getHelloService() {

    HelloService service = new HelloService();
    service.setMessage(helloProperties.getMessage());
    return service;
}
}

(4)在spring.factories 中添加EnableAutoConfiguration 配置,注册(3)自动配置类。

org.spring.boot.autoConfigure.EnableAutoConfiguration=\
om.shopping.autoconfiguration.HelloAutoConfiguration

(5)创建一个测试项目进行测试。

一 . 首先引入maven 依赖 :

        
        com.cloud.project
        shopping-spring-boot-starter
        1.0-SNAPSHOT
      

二 .在application.properties 文件中添加我们在Starter中需要注入的属性值, 注意prefix。

hello.message=hahaaaa

三 . 然后在main 类中注入使用:

@SpringBootApplication
@RestController
public class StarterDemo {


@Autowired
private HelloService helloService;

public static void main(String[] arg) {

    SpringApplication.run(StarterDemo.class);

}


@RequestMapping(value = "/message")
public String getMessage(){
    return helloService.getMessage();
}
}

四 : 通过访问localhost:8080/message;

页面显示 hahaaaa

如果我们修改application.properties 中的key 变成: errorhello.message , 这样就不符合上面定义的prefix,因此会输出默认值 : defaultMessage.

github : https://github.com/1991lin/shopping

你可能感兴趣的:(4. 看看 Spring Boot 自动配置的原理?)