spring boot starter--使用SPI机制实现(spring.factories)

一、注解实现java配置代替xml配置

1 @Configuration:定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。比如:

@Configuration
public class MainAutoConfigure {

    @Bean
    public TestService1 testService1(){
        return new TestService1();
    }
}

相当于定义了一个applicationContext.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="testService1" class="TestService1"/>

</beans>

2 @Import(AutoConfigureImport.class):引入一个配置类,相当于applicationContext.xml中的

<import resource="autoConfigureImport.xml"/>

二、SPI机制告诉spring boot,java配置类路径

SPI ,全称为 Service Provider Interface,是一种服务发现机制。它通过在ClassPath路径下的META-INF/services文件夹查找文件,自动加载文件里所定义的类。

在Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。
这种自定义的SPI机制是Spring Boot Starter实现的基础。

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.autoconfigure.MainAutoConfigure

三、示例:myTest-spring-boot-starter

1 新建一个工程:myService,提供最简单的TestService服务,和spring没有一毛钱的关系

public class TestService1{
    public String sayHi() {
        return "hi TestService1!";
    }
}

public class TestService2 {
    public String sayHi() {
        return "hi TestService2!";
    }
}

2 再新建一个工程:myTest-spring-boot-starter,用来配置myService工程中的TestService服务

  • pom依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.2.5.RELEASE</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>myService</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
  • 配置类1:主配置,模拟applicationContext.xml
@Configuration
@Import(AutoConfigureImport.class)
public class MainAutoConfigure {

    @Bean
    public TestService1 testService1(){
        return new TestService1();
    }
}
  • 配置类2:模拟applicationContext.xml中的import标签
@Configuration
public class AutoConfigureImport {

    @Bean
    public TestService2 testService2(){
        return new TestService2();
    }
}
  • 新建resources/META-INF/spring.factories,让spring boot自动导入配置类
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.autoconfigure.MainAutoConfigure

3 最后新建一个工程:testProject,用来测试myTest-spring-boot-starter:

  • pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>myTest-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
  • 测试controller
@RestController
public class TestController {

    @Autowired
    private TestService1 testService1;

    @Autowired
    private TestService2 testService2;

    @GetMapping("/testService1")
    public String testService1(){
        return testService1.sayHi();
    }

    @GetMapping("/testService2")
    public String testService2(){
        return testService2.sayHi();
    }
}
  • 启动类:
@SpringBootApplication
public class StartApp {
    public static void main(String[] args) {
        SpringApplication.run(StartApp.class,args);
    }
}
  • 测试成功,如下:

工程Demo链接: https://pan.baidu.com/s/1wdMCoeVTo1LBYJy8B1QPpg

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值