Java - 配置文件读取,并制作SDK工具插件,供项目其他组件引入使用

    本来想做一个类似数据库连接池一样的插件工具,可以在任意的项目工程中启动的时候,直接加载配置的连接信息,而达到初始化连接池的目的;

方案一:使用java自带的文件读取工具类,配置读取方法,先读取配置文件,再初始化连接池(以redis连接池为例)

//私有构造方法
private JedisPoolUtil(){}
//保证内存中只有一个连接池对象
public static JedisPool pool = null;

//静态代码块
static{
    //读取资源文件
    ResourceBundle bundle = ResourceBundle.getBundle("application");//application.properties只能读取以.properties的配置文件
    //读取相应的值
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMinIdle(Integer.parseInt(bundle.getString("minIdle")));//最小空闲连接数
    config.setMaxIdle(Integer.parseInt(bundle.getString("maxIdle")));//最大空闲连接数
    config.setMaxTotal(Integer.parseInt(bundle.getString("maxTotal")));//最大连接数
    config.setMaxWaitMillis(Integer.parseInt(bundle.getString("maxWaitMillis")));//最大等待超时时间
    logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",bundle.getString("host"),Integer.parseInt(bundle.getString("port")),Integer.parseInt(bundle.getString("minIdle")),Integer.parseInt(bundle.getString("maxIdle")),Integer.parseInt(bundle.getString("maxTotal")),Integer.parseInt(bundle.getString("maxWaitMillis")));
    pool = new JedisPool(config, bundle.getString("host"), Integer.parseInt(bundle.getString("port")));
}

//获取连接
public static Jedis getJedis(){
    return pool.getResource();
}

//关闭连接
public static void closeJedis(Jedis jedis) {
    jedis.close();
}


application.properties
#REDIS CONNECTION INFORMATION
redis.host: 127.0.0.1
redis.port: 6379
redis.minIdle: 10
redis.maxIdle: 100
redis.maxTotal: 200
redis.maxWaitMillis:5000

方案二:使用spring自带的文件读取工具类,配置读取方法,先读取配置文件,再初始化连接池(以redis连接池为例)

//私有构造方法
private JedisPoolUtil(){}
//保证内存中只有一个连接池对象
public static JedisPool pool = null;

//静态代码块
static{
    //读取资源文件
    //spring自带的文件读取工具类
    Resource resource = new ClassPathResource("application.yml");
    Properties bundle = PropertiesLoaderUtils.loadProperties(resource);
    String host = bundle.getProperty("redis.host");
    //读取相应的值
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMinIdle(Integer.parseInt(bundle.getProperty("redis.minIdle")));//最小空闲连接数
    config.setMaxIdle(Integer.parseInt(bundle.getProperty("redis.maxIdle")));//最大空闲连接数
    config.setMaxTotal(Integer.parseInt(bundle.getProperty("redis.maxTotal")));//最大连接数
    config.setMaxWaitMillis(Integer.parseInt(bundle.getProperty("redis.maxWaitMillis")));//最大等待超时时间
    logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",bundle.getProperty("redis.host"),Integer.parseInt(bundle.getProperty("redis.port")),Integer.parseInt(bundle.getProperty("redis.minIdle")),Integer.parseInt(bundle.getProperty("redis.maxIdle")),Integer.parseInt(bundle.getProperty("redis.maxTotal")),Integer.parseInt(bundle.getProperty("redis.maxWaitMillis")));
    pool = new JedisPool(config, host, Integer.parseInt(bundle.getProperty("redis.port")));
}

//获取连接
public static Jedis getJedis(){
    return pool.getResource();
}

//关闭连接
public static void closeJedis(Jedis jedis) {
    jedis.close();
}


application.yml
#REDIS CONNECTION INFORMATION
redis.host: 127.0.0.1
redis.port: 6379
redis.minIdle: 10
redis.maxIdle: 100
redis.maxTotal: 200
redis.maxWaitMillis:5000

     以上是初始化redis的连接池工具类(加上@Configuration注解),可以打成一个jar包,引入到想要使用的项目POM文件中,并在启动类上加入需要加载的包路径 @ComponentScan(basePackages = { "xxx.yyy.zzz"}),application.yml及application.properties配置文件是放在想引入项目的resources目录下的,而不是工具类所在的目录下的哦,

    都配置好之后,就可以启动项目,会发现我们想要初始化连接池的操作,也已经被项目给顺便加入了spring管理了,并初始化完成。

方案三:既然我们都可以使用注解的方式成功加入到spring的管理中,为什么不直接使用注解来读取配置文件,像项目本身的一个类一样来配置和管理呢,不得不说,spring还真是强大啊。。。

    

application.yml
#REDIS CONNECTION INFOMATION
redis:
    pool:
       host: 127.0.0.1
       port: 6379


@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConnectConfig {
    /**
     * 从配置文件中读取的limitSizeMap开头的数据
     * 注意:名称必须与配置文件中保持一致
     * 在代码部分可直接使用pool.get("host")来得到“127.0.0.1”的数据
     */
    private Map<String, String> pool= new HashMap<>();

    public Map<String, String> getPool() {
        return pool;
    }

    public void setPool(Map<String, String> pool) {
        this.pool= pool;
    }

}

 

posted @ 2022-01-27 18:35  zhangdaopin  阅读(303)  评论(0编辑  收藏  举报