Disconf注解式实现

  前篇博客敲了一个简单的Disconf的Demo,这几天学习源码才发现,上一篇博客Demo实现方式是采用的XML配置方式,这篇博客介绍一下注解式实现方式,以及XML配置式和注解式的区别。参考内容是Disconf的官网:http://disconf.readthedocs.io/zh_CN/latest/index.html,介绍的角度会倾向于ITOO项目应用。

 

创建配置文件

 

  创建过程和上篇博客介绍的相同,这篇博客还是利用上篇博客创建好的配置文件。

 

创建Demo

 

  其他代码不变,只需要修改JedisConfig类和applicationContext.xml配置文件就好。

 

applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>  
  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  
    <aop:aspectj-autoproxy proxy-target-class="true"/>  
  
    <!-- 使用disconf必须添加以下配置 -->  
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"  
          destroy-method="destroy">  
        <property name="scanPackage" value="com.tgb.service"/>  
    </bean>  
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"  
          init-method="init" destroy-method="destroy">  
    </bean>   
  
</beans>

 

JedisConfig类

 

package com.tgb.service;  
  
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.springframework.context.annotation.Configuration;  
  
import com.baidu.disconf.client.common.annotations.DisconfFileItem;  
import com.baidu.disconf.client.common.update.IDisconfUpdate;  
  
@Configuration  
@DisconfFile(filename="redis.properties")
public class JedisConfig implements IDisconfUpdate {  
  
    protected static final Logger LOGGER = LoggerFactory  
            .getLogger(JedisConfig.class);  
  
    // 代表连接地址  
    private String host;  
  
    // 代表连接port  
    private int port;  
  
    /** 
     * 地址, 分布式文件配置 
     *  
     * @return 
     */  
    @DisconfFileItem(name = "redis.host", associateField = "host")  
    public String getHost() {  
  
        return host;  
    }  
  
    public void setHost(String host) {  
  
        this.host = host;  
    }  
  
    /** 
     * 端口, 分布式文件配置 
     *  
     * @return 
     */  
    @DisconfFileItem(name = "redis.port", associateField = "port")  
    public int getPort() {  
  
        return port;  
    }  
  
    public void setPort(int port) {  
  
        this.port = port;  
    }  
  
    public void reload() throws Exception {  
  
        LOGGER.info("host: " + host);  
    }  
}  

 

两者区别

 

XML式实现

 

  这种方式下,当项目启动,Disconf-Client开始扫描应用配置数据时,使用XML配置的配置文件是不会注入到仓库中的,也就是说Disconf-Client只会对这种配置文件有托管作用,当disconf-web上配置文件修改时,会下载到本地,但是不会自动reload到相应的bean,当然用户也可根据自己的需要,配置相应的reload,只不过非.properties文件的回调函数需要自己写。这种配置方式适合所有配置文件。

 

  采用XML式配置方式,写代码比较少,不需要在每一个需要的Bean都写上注解的配置文件,只需要在配置文件中一处配置就好。但是它的缺点是不能自动reload到Bean,也就是Bean在使用过程中可能会用到旧的配置文件。

 

注解式实现

 

  采用注解式实现方式,项目启动时,Disconf-Client会扫描应用中的注解信息,将这些注解数据注入到仓库中,此时Disconf-Client对配置文件不止有托管作用,当 本地配置文件修改时,它还能自动reload到相应的Bean。

 

  采用注解的方式,需要在所有的需要使用配置文件的Bean上都注解配置文件,写的代码会比较多。但是如果配置文件更改后,它能自动reload使用配置文件的Bean,使得Bean都使用最新配置信息。

 

总结

 

  在ITOO中,是把shiro-cas.properties配置文件放到disconf上管理,采用的是XML式实现方式。XML方式可以不自动reload,也可以自动reload。当需要自动reload时,需要把自动reload的Bean配置到Spring的配置文件中,而不自动reload也是针对非.properties文件,所以在ITOO中,是不需要配置相应的Bean。

  这是根据Disconf官网对这两种方式的简单理解,如果有不对的地方,还请过路的大牛指正,谢谢。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值