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:
Second, write RedisProperties.class in this project, to read redis configuration information from application.properties
Third, RedisAutoConfiguration.class prepared in this project to the Jedis spring loaded into the bean container
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
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:
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
Use:
Sixth, the new Blog Project, the introduction of spring-boot-starter-redis dependent
Seven projects in Blog, start writing class BlogApplication
Eight, start redis
Redis start a virtual machine or a cloud server,
First start Spring-boot-starter-redis starter, and then start a specific project spring-boot-starter-apps