In the SpringBoot framework, there are many annotations similar to @enablexx, such as @enableAsync, @enablesCheduling, etc., a simple annotation can implement the function of the corresponding function introduction, isn’t that amazing? So how do we customize the @enable annotation to use these operations?
Found that regular
Take a few random notes:
The @enable annotation has an @import annotation. The @import annotation is similar to the tag in the XML configuration, which is used to introduce beans. So, the @enable annotation rules are found.
@ EableRedis custom
- Custom annotations
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({RedisConfigure.class})
public @interface EnableRedis {
}
Copy the code
- Write the configuration class redisconfigure.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisClientConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; import java.time.Duration; /** * @author wangkun */ @EnableConfigurationProperties(RedisConfig.class) public class RedisConfigure { @Autowired private RedisConfig redisConfig; @bean public JedisPoolConfig JedisPoolConfig () {JedisPoolConfig JedisPoolConfig = new JedisPoolConfig(); / / maximum number of connections jedisPoolConfig. SetMaxTotal (redisConfig. GetMaxActive ()); / / when when there is no available connection in the pool, the maximum waiting time jedisPoolConfig. SetMaxWaitMillis (redisConfig. GetMaxWaitMillis ()); / / maximum number of idle connections jedisPoolConfig. SetMinIdle (redisConfig. GetMaxIdle ()); / / minimum number of idle connections jedisPoolConfig. SetMinIdle (redisConfig. GetMinIdle ()); Return jedisPoolConfig; } /** * Jedis connection * @param jedisPoolConfig * @return */ @bean public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) { JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling() .poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(redisConfig.getTimeout())).build(); RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(redisConfig.getHost()); redisStandaloneConfiguration.setPort(redisConfig.getPort()); redisStandaloneConfiguration.setPassword(RedisPassword.of(redisConfig.getPassword())); redisStandaloneConfiguration.setDatabase(redisConfig.getDatabase()); return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig())); return redisTemplate; }}Copy the code
- The configuration class RedisConfig. Java
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "redis.common")
@Data
public class RedisConfig {
private int database;
private String host;
private int port;
private String password;
private int timeout;
private int maxActive;
private long maxWaitMillis;
private int maxIdle;
private int minIdle;
}
Copy the code
The use of the @ EnableRedis
- Start the class
import com.antelope.gate.common.redis.EnableRedis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /** * @author Aissue */ @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableRedis public class GapServerApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(GapServerApplication.class); springApplication.run(args); }}Copy the code
- Configuration properties
Host =127.0.0.1 redis.common.database=0 redis.common.port=6379 redis.common.password= redis.common.timeout=300000 redis.common.max-active=10 redis.common.max-idle=8 redis.common.max-wait=-1 redis.common.min-idle=0 redis.common.cidToGBId=CID-GBIDCopy the code
summary
In fact, @enablexx custom annotations are relatively simple, if you do, you can make your code flexible and elegant.