Programmer essential skills of SpringBoot automatic assembly principle, very detailed, recommended collection!!

In the actual project, we need to hand-write Starter components in relatively few scenarios, but for the principle of custom Starter components we still need to understand, the first is to increase their self-confidence, The second is to master the principle of automatic assembly after the Starter component implementation is actually very simple things. We will introduce the Redis client program to you. There are many Java clients available for Redis services, but how do we use them in SpringBoot?

Write the SpringBoot Starter component by hand

1. Customize the Starter

Create a regular Maven project named Reddison-spring-boot-starter. Note that the third-party starter convention is XXXX +spring-boot-starter.

Add related dependencies

<dependency> <groupId>org.redisson</groupId> </artifactId> <version>3.15.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> < version > from 2.4.8 < / version > < optional > true < / optional > < / dependency > < / dependencies >Copy the code

Adding a Property Class

ConfigurationProperties(prefix = "bobo.redisson") public class RedissonProperties { private String host = "localhost"; private int port = 6379; private int timeout = 0; private boolean ssl = false; 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 int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public boolean isSsl() { return ssl; } public void setSsl(boolean ssl) { this.ssl = ssl; }}Copy the code

Then create the corresponding configuration class.

@ ConditionalOnClass (Redisson. Class)/assembly / / / conditions associated attributes configuration class @ EnableConfigurationProperties (RedissonProperties. Class) @Configuration public class RedissonAutoConfiguration { @Bean public RedissonClient redissonClient(RedissonProperties redissonProperties){ Config config = new Config(); String prefix = "redis://"; if(redissonProperties.isSsl()){ prefix = "rediss://"; } / / single node is connected with the config. UseSingleServer () setAddress (prefix + redissonProperties. GetHost () + ":" + redissonProperties. GetPort ()) .setConnectTimeout(redissonProperties.getTimeout()); return Redisson.create(config); }}Copy the code

That’s all we need for our custom Starter, and then we need to create the spring.factories file. To implement automatic assembly with the SpringBoot project.

Finally, for the purpose of adding a properties file, there is a prompt message. We can lead in dependencies

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> The < version > from 2.4.8 < / version > < / dependency >Copy the code

Then add the JSON file, which must be named ready-to-spring -configuration-metadata.json

{ "properties": [ { "name": "bobo.redisson.host", "type": "java.lang.String", "description": "Redis server address ", "defaultValue": "localhost"},{"name": "bobo.redson.port ", "type": "Java.lang.Integer", "description": "Redis server port ", "defaultValue": 6379}]}Copy the code

Here you can install, and then you can use.

2.SpringBoot project

Once the custom Starter project is created, we can use it in the SpringBoot project. Lead in the corresponding dependency.

Then we configure the connection information of Redis in the properties file

Complete configuration information

Spring-datasource. Driver-class-name = com.mysql.cj.jdbc.driver spring.datasource.url=jdbc:mysql://localhost:3306/mybatisdb? ServerTimezone = UTC spring. The datasource. The username = root spring. The datasource. The password = 123456 # connection pool configuration Spring. The datasource. Type = com. Alibaba. Druid. Pool. DruidDataSource # # specify the location of the mapping file Mybatis. Mapper-locations =classpath:mapper/*.xml ## customisable Start attribute Redis set bobo.redson. host=192.168.100.120 bobo.redisson.port=6379 bobo.redisson.timeout=5000Copy the code

Then create the controller test.

@RestController public class UserController { @Autowired private IUserService userService; @Autowired private RedissonClient redissonClient; @GetMapping("/hello") public List<User> hello(){ return userService.query(); } @GetMapping("/query") public String query(){ RBucket<Object> name = redissonClient.getBucket("name"); if(name.get() == null){ name.set("BOBO"); } return name.get().toString(); }}Copy the code

Starting a service test:

Read three things ❤️

If you found this post helpful, I’d like to invite you to do three small favors for me:

  1. Likes, retweets, and your “likes and comments” are what drive me to create.

  2. Follow the public account “Java rotten pigskin”, irregularly share original knowledge.

  3. In the meantime, look forward to a follow-up article at ing🚀

  4. 【666】 Scan the code to obtain the learning materials package