分享

Spring Boot(八):自定义启动器

 侯培彬 2020-03-10

1. 启动器构成

启动器一般由两部分组成,包括启动器项目和启动器配置项目。一般启动器项目是一个空的项目,引入了配置依赖,和一些辅助依赖,通常主要配置信息都是写在配置项目中。对于这两个项目,命名方式一般是:

启动器只用来做依赖导入; 专门来写一个自动配置模块; 启动器依赖自动配置;别人只需要引入启动器(starter) mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter
  • 1
  • 2
  • 3
  • 4

原理:

@Configuration //指定这个类是自动配置类,回去加载它
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //就可以给容器中,添加当前starter需要的组件了

@ConfigurationPropertie //结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效并加入到容器中

//对于自动配置类如何被加载,则依赖springboot的约定了,将需要启动就加载的自动配置类,
//配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=xxx.AutoConfigurter
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2. 启动器项目

创建maven项目 myredis-spring-boot-starter,作为starter项目,不需要写任何代码。更改pom.xml,引入下面创建的autoconfigurer项目:

<?xml version='1.0' encoding='UTF-8'?> <project xmlns='http://maven./POM/4.0.0' xmlns:xsi='http://www./2001/XMLSchema-instance' xsi:schemaLocation='http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>cn.mh</groupId> <artifactId>myredis-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <description>redis的启动器,用于注入jedis和jedispool的连接</description> <!--该项目是一个空的项目,用于引入一些辅助依赖的东西--> <dependencies> <dependency> <groupId>cn.mh</groupId> <artifactId>myredis-spring-boot-starter-autoconfigurer</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

3. 启动器配置项目

创建maven项目 myredis-spring-boot-autoconfigurer,更改配置文件,添加父项目和引入启动器的依赖。

<?xml version='1.0' encoding='UTF-8'?>
<project xmlns='http://maven./POM/4.0.0'
         xmlns:xsi='http://www./2001/XMLSchema-instance'
         xsi:schemaLocation='http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd'>
    <modelVersion>4.0.0</modelVersion>

    <!--所有的启动器都必须引入这个父项目,这个父项目中定义了依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <groupId>cn.mh</groupId>
    <artifactId>myredis-spring-boot-starter-autoconfigurer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>redis启动器的自动配置类项目。</description>

    <!--引入springboot-starter这是所有starter的依赖,定义了配置注解等-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.3</version>
        </dependency>
    </dependencies>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

4. 编写自动配置类

1.添加配置文件类,绑定配置前缀

package cn.mh.starter; import org.springframework.boot.context.properties.ConfigurationProperties; /** * 配置类信息 * * @author: mahao * @date: 2019/11/30 */ @ConfigurationProperties(prefix = 'my.redis') public class RedisProperties { private String host = '127.0.0.1'; private int port = 6379; private String password = '000000'; private int maxTotal = 128; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getMaxTotal() { return maxTotal; } public void setMaxTotal(int maxTotal) { this.maxTotal = maxTotal; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

2.编写自动配置类,引入场景组件:

package cn.mh.starter;
 
/**
 * redis的自动配置类;
 *
 * @author: mahao
 * @date: 2019/11/30
 */
@Configuration
//@ConditionalOnWebApplication
@ConditionalOnClass(Jedis.class)
@EnableConfigurationProperties(RedisProperties.class)//引入配置文件
public class MyRedisAutoConfiguration {

    private final RedisProperties redisProperties;

    //使用spring的依赖注入,通过构造方法注入
    public MyRedisAutoConfiguration(RedisProperties redisProperties) {
        this.redisProperties = redisProperties;
    }

    //添加jedis池组件
    @Bean
    @ConditionalOnMissingBean(JedisPool.class)
    public JedisPool jedisPool() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxTotal(redisProperties.getMaxTotal());
        JedisPool pool = new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), Protocol.DEFAULT_TIMEOUT, redisProperties.getPassword(),
                Protocol.DEFAULT_DATABASE, null);
        System.out.println('jedispool被创建了');
        return pool;
    }

	
    //jedis组件,多例模式下;
    @Scope('prototype')
    @Bean
    @ConditionalOnClass(JedisPool.class)
    public Jedis jedis(JedisPool jedisPool) {
        return jedisPool.getResource();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

**3.将自动配置类,添加到springboot的约定目录: **

resources/META-INF下新建文件,命名spring.factories,添加的内容是:

# Auto Configure,用于引入自动配置类 org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.mh.starter.MyRedisAutoConfiguration
  • 1
  • 2
  • 3

4. 安装到maven仓库:
将启动器和配置项目安装到maven仓库中,供其他项目使用;

5. 编写测试项目

创建springboot项目,编写测试类:

修改配置文件:

my.redis.host=127.0.0.1
my.redis.port=6379
my.redis.password=123456
  • 1
  • 2
  • 3
/** * @author: mahao * @date: 2019/11/30 */ @RunWith(SpringRunner.class) @SpringBootTest public class RedisStarterTest { @Autowired JedisPool jedisPool; @Autowired ApplicationContext context; @Test public void contextLoads() { Jedis resource = jedisPool.getResource(); System.out.println(resource); } @Test public void contextLoads2() { Jedis jedis = (Jedis) context.getBean('jedis'); Jedis jedis2 = (Jedis) context.getBean('jedis'); System.out.println(jedis '----' jedis2); Set<String> keys = jedis.keys('*'); System.out.println(keys); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

如何没有错误,将会打印出结果;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多