SpringBoot custom starter starter

In the actual project development, we often use a variety of starter, starter and some of these have been integrated springboot official and some basic functions, such as: springboot-starter , there are some with the frame by a third party springboot provided to us for fast and efficient development of the customized integration, such as: MyBatis -spring-the Boot-Starter .

In the daily development, due to the project requirements as well as efficient development, deployment, we can springboot be customized private, need to generate their own starter, re-introduced into actual projects, improve the efficiency of development.

For example: When we use the java redis in springboot controls jedis, need to create an instance of Jedis and pass the appropriate parameters such as  ip, port , etc., springboot after use, can be customized starter, the Jedis use ip, port instantiated of, and managed by the container Bean injection SpringBoot, made launcher.

The starter introduced in the need to redis function of the project, so that you can improve the efficiency of development.

Regular starter is an independent project, and then publish the new warehouse in maven registered in that other developers can use up your starter.

The realization of a customizer starter for the redis.

First, create a new maven project spring-boot-starter-redis (starter)

Introducing the following dependency:

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

Second, write RedisProperties.class in this project, to read redis configuration information from application.properties

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

Third, RedisAutoConfiguration.class prepared in this project to the Jedis spring loaded into the bean container

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

As we explain the meaning of these annotations

  • @SpringBootConfiguration: This is a configuration representative of the class
  • @EnableConfigurationProperties (RedisProperties.class): The RedisProperties spring loaded into the container. Reference Spring-Boot @ Enable * annotations of works
  • @ConditionalOnClass (Jedis.class): When the project references the Jedis of the jar, just load this configuration class. Reference Spring-Boot autoconfigure of Condition

Fourth, in this project file in the resource directory New application.properties

redis.host=127.0.0.1
redis.port=6379

Five, Bean injection method:

The first: automatic injection, to create a dependency configuration spring.factories

New in the resource directory META-INF directory, and create spring.factories file in the directory, add the following data files:

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

Current data for loading to generate the Bean container where the current starter manage, and when introduced into specific projects being scanned items in containers to and call.

The second: manual injection, create EnableRedis class

According @Import be configured section notes the role of manually added to the project startup class

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

Use:

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

Sixth, the new Blog Project, the introduction of spring-boot-starter-redis dependent

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

Seven projects in Blog, start writing class 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()); } } 

Eight, start redis

Redis start a virtual machine or a cloud server,

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

First start Spring-boot-starter-redis starter, and then start a specific project spring-boot-starter-apps


Guess you like

Origin www.cnblogs.com/wushaopei/p/12283626.html