spring boot properties類型安全以及自動配置

spring boot properties類型安全以及自動裝配bean

  1. 將要配置的屬性名寫入到application.properties文件中,例如:
reids.host=10.1.10.22
redis.port=6319
redis.other.user=jdw
redis.other.pass=jdw_001
redis.host-url=localhost
  1. 增加類型安全的配置java文件RedisProperties.java文件,代碼如下:
package com.jiangdengwei.redis;

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

/**
 * @author jackychiang
 */
@ConfigurationProperties(prefix = "redis") //配置項前綴
public class RedisProperties {
    private String host;
    private Integer port;
    //這裏是支持host-url類型的配置
    private String hostUrl;
    private Other other;
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public String getHostUrl{
        return this.hostUrl;
    }

    public void setHostUrl(String hostUrl){
        this.hostUrl = hostUrl;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public Other getOther{
        return this.other;
    }

    public void setOther(Other other){
        this.other = other
    }
    //這裏是支持other層級配置項
    public static class Other{
        private String user;
        private String pass;
        public String getUser{
            return this.user;
        }
        public void setUser(String user){
            this.user = user;
        }
        public String getPass(){
            return this.pass;
        }

        public void setPass(String pass){
            this.pass = pass;
        }
    }
}
  1. 增加自動裝配類RedisAutoConfiguration.java
package com.jiangdengwei.redis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

/**
 * @author jackychiang
 */
@Configuration  //指定爲配置類
@ConditionalOnClass(Jedis.class) //只有在存在Jedis.class的時候纔有效
@EnableConfigurationProperties(RedisProperties.class) //引入RedisProperties.class類型安全的配置
public class RedisAutoConfiguration {

    @Bean  //標記bean
    @ConditionalOnMissingBean //只有在容器中沒有名字爲jedis的時候纔會產生jedis的bean
    public Jedis jedis(RedisProperties redisProperties) {
        return new Jedis(redisProperties.getHost(), redisProperties.getPort());
    }

}
  1. 增加Redis配置啓用開關注解
package com.jiangdengwei.redis;

import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(RedisAutoConfiguration.class)
public @interface EnableRedis {
}
  1. 使用方法
package com.jiangdengwei.device;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableCaching
@EnableScheduling
@SpringBootApplication
@EnableRedis
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

ps: 小白剛寫博客,歡迎大家指教

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章