自定义spring-boot-stater

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、spring-boot 自动装配揭秘?

  1. 复合注解 @SpringBootApplication包含以下三个注解:
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan

  2. 注解@EnableAutoConfiguration 包含注解@Import(AutoConfigurationImportSelector.class)会引用执行AutoConfigurationImportSelector中selectImports()方法

	@Override
	public String[] selectImports(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return NO_IMPORTS;
		}
		AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
		return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
	}

该方法会调用org.springframework.core.io.support.SpringFactoriesLoader#loadSpringFactories(),默认自动加载装配META-INF/spring.factories文件中配置的类。

二、自定义spring-boot-starter

**命名规则**

Spring官方建议非官方Starter命名应遵循 {name}-spring-boot-starter 的格式

1.HelloServiceProperties配置映射

代码如下(示例):

@ConfigurationProperties(prefix = "hello.service")
public class HelloServiceProperties {
    
    String name;
    
    int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

2. 自动加载配置HelloServiceAutoConfiguration

代码如下(示例):

@Configuration
@ConditionalOnClass(HelloService.class)
@EnableConfigurationProperties(HelloServiceProperties.class)
public class HelloServiceAutoConfiguration {


    @Bean
    public HelloService helloService(HelloServiceProperties properties) {
        System.out.println("@Bean执行");
        return new HelloService(properties.getName(), properties.getAge());
    }
}

3. 自动装配类HelloService

代码如下(示例):

public class HelloService {

    private String name;

    private int age;

    public HelloService() {
    }

    public HelloService(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String sayHello() {
        return "hello " + name + "年龄:" + age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4. spring.factories配置文件

代码如下(示例):

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.kerry.hello.service.HelloService

5. 引用自定义spring-boot-starter

  1. pom依赖如下:
 <dependency>
            <groupId>com.kerry</groupId>
            <artifactId>hello-service-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
 </dependency>
  1. 注意修改Application启动类中组件扫描的路径配置,
    若未正确配置新引入的starter路径可能无法正常执行

@ComponentScan({"com.kerry.netty.study01.*","com.kerry.hello.*"})

三、总结

spring-boot自动装配原理:

springboot启动类上都有一个@SpringBootApplication注解。该注解是复合注解,包含@EnableAutoConfiguration注解,它会引入AutoConfigurationImportSelector.class这个类,这个类会加载jar包里META-INF/spring.factories配置文件里面的配置类。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值