SpringBoot 定制 starter 启动器

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

在实际项目开发中,我们常常会用到各种各样的 starter,这些starter 有的是有 springboot官方提供并已经整合一些基本功能的,如:spring-boot-starter,也有一些是由 第三方将框架与springboot进行定制整合后提供给我们进行快捷高效开发的,如:MyBatis-Spring-Boot-Starter

在日常开发中,由于项目的要求以及高效开发、部署等,我们可以对springboot进行私有定制,生成自己需要的启动器,再引入到实际项目中,提高开发的效率。

举例: 我们在springboot 中使用 redis 的 java控件jedis 时,需要 进行创建  Jedis 的实例并传入相应参数如  ip 、port 等,在使用springboot后,可以进行定制化starter,将Jedis 用ip、port 进行实例化,并将 Bean 注入容器由 SpringBoot 进行管理,制成启动器。

在需要使用到 redis 功能的工程项目中引入该启动器,这样就能够提高开发的效率。

正规的starter是一个独立的工程,然后在maven中新仓库注册发布,其他开发人员就可以使用你的starter了。

以下实现一个针对 redis 的 定制器 starter。

一、新建一个maven项目spring-boot-starter-redis (启动器)

引入如下依赖:

                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<version>1.5.7.RELEASE</version>
		</dependency>
                <dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>
                <!--自动配置依赖-->
                <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

二、在此项目中编写 RedisProperties.class ,用以从 application.properties 中读取 redis 的配置信息

package com.example.demo.properties;

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

/**
 * @ClassName JedisProperties
 * @Description TODO
 * @Author wushaopei
 * @Date 2020/2/2 18:30
 * @Version 1.0
 */
@Component
@ConfigurationProperties(prefix="jedis")
public class RedisProperties {
    private String host;
    private int port;

    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;
    }

}

三、在此项目中编写RedisAutoConfiguration.class,用以将 Jedis 的bean 装载进 spring 容器中

package com.example.demo.autoConfigure;

import com.example.demo.properties.RedisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

/**
 * @ClassName JedisAutoConfigure
 * @Description TODO
 * @Author wushaopei
 * @Date 2020/2/2 18:32
 * @Version 1.0
 */
@Configuration
@ConditionalOnClass({ Jedis.class})
@EnableConfigurationProperties({RedisProperties.class})
@AutoConfigureAfter({RedisProperties.class})
public class RedisAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public Jedis Jedis(RedisProperties redisProperties) {
         //spring会自动将RedisProperties这个bean注入进来,
        return new Jedis(redisProperties.getHost(),redisProperties.getPort(),10000);
    }
}

为大家解释一下这些注解的含义

四、在此项目中resource目录下新建 application.properties 文件

redis.host=127.0.0.1
redis.port=6379

五、Bean 注入方式:

第一种:自动注入,创建 依赖配置 spring.factories

在resource 目录下新建 META-INF 目录,并在该目录下创建 spring.factories 文件,在文件添加以下数据:

org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.example.demo.autoConfigure.RedisAutoConfiguration

当前数据用于将加载生成的Bean 交给当前启动器所在的容器进行管理,并在被引入到具体的工程项目时被项目中的容器所扫描到而调用。

第二种:手动注入,创建EnableRedis

根据@Import注解 的作用进行切面配置,手动添加到 工程项目启动类

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(RedisAutoConfiguration.class)
public @interface EnableRedis {
}

使用方式:

@SpringBootApplication
@EnableRedis//关键的一步
public class BlogApplication {
......    
                        }

六、新建Blog 工程项目,引入 spring-boot-starter-redis 依赖

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

七、在Blog项目中,编写启动类BlogApplication

package com.imooc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ConfigurableApplicationContext;
import redis.clients.jedis.Jedis;


/**
 * @ClassName BlogApplication
 * @Description TODO
 * @Author wushaopei
 * @Date 2020/2/2 23:15
 * @Version 1.0
 */
@SpringBootApplication
public class BlogApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
        Jedis jedis = context.getBean(Jedis.class);
        System.out.println(jedis);
        //如果成功连接上了redis,jedis.ping()会返回一个pong
        System.out.println(jedis.ping());
    }
}

八、启动redis

启动虚拟机或云服务器的redis ,

[root@wsp /]# redis-server /etc/redis.conf 
[root@wsp /]# redis-cli 

先启动 Spring-boot-starter-redis 启动器 ,再启动具体的项目工程 spring-boot-starter-apps


原文地址:https://www.cnblogs.com/wushaopei/p/12283626.html