<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.tmall.flow.flowrule"/> </bean> <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond" init-method="init" destroy-method="destroy"> </bean> <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload)--> <bean id="configproperties_disconf" class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean"> <property name="locations"> <list> <value>activityrule.xml</value> </list> </property> </bean> <bean id="propertyConfigurer" class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer"> <property name="ignoreResourceNotFound" value="true"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="propertiesArray"> <list> <ref bean="configproperties_disconf"/> </list> </property> </bean>
disconf.xml配置如上,没什么大问题,按官方给的demo来好了
配置类如下:
import com.baidu.disconf.client.common.annotations.DisconfFile; import org.springframework.stereotype.Service; /** * 空的分布式配置文件,用途有两种:<br/> * 1. 对配置文件里的内容不感兴趣,只是单纯的下载<br/> * 2. 当配置文件更新时,可以自动下载到本地 */ @Service @DisconfFile(filename = "activityrule.xml") public class ActivityRuleConfig { }
配置类回调加载类
@Service @DisconfUpdateService(classes = { ActivityRuleConfig.class}) public class ActivityRuleCallBack implements IDisconfUpdate { @Override public void reload() throws Exception { FlowXmlDom.parseXMLData("/activityrule.xml"); } }
main启动类:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.example.disconf.demo.task.DisconfDemoTask; /** * @author liaoqiqi * @version 2014-6-17 */ public class DisconfDemoMain { protected static final Logger LOGGER = LoggerFactory.getLogger(DisconfDemoMain.class); private static String[] fn = null; // 初始化spring文档 private static void contextInitialized() { fn = new String[] {"applicationContext.xml"}; } /** * @param args * * @throws Exception */ public static void main(String[] args) throws Exception { contextInitialized(); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(fn); DisconfDemoTask task = ctx.getBean("disconfDemoTask", DisconfDemoTask.class); int ret = task.run(); System.exit(ret); } }
maven中spring版本使用
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>RELEASE</version>
</dependency>
最新为5.0版本
启动后总是会报错,目前还是头一次使用spring5遇到问题,
Exception in thread "main" java.lang.NoSuchMethodError: com.baidu.disconf.client.addons.properties.DefaultPropertyPlaceholderConfigurer.parseStringValue(Ljava/lang/String;Ljava/util/Properties;Ljava/util/Set;)Ljava/lang/String; at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.parseStringValue(ReloadingPropertyPlaceholderConfigurer.java:97) at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer$PlaceholderResolvingBeanDefinitionVisitor.resolveStringValue(ReloadingPropertyPlaceholderConfigurer.java:457) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanClassName(BeanDefinitionVisitor.java:107) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:80) at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.processProperties(ReloadingPropertyPlaceholderConfigurer.java:382) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:164) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:693) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:95) at com.example.disconf.demo.DisconfDemoMain.main(DisconfDemoMain.java:32)
反反复复核对线上文档,发现没有问题
再一次集成到spring-web空项目中,还是一样报错
[11:26:13:410] [WARN] - com.baidu.disconf.client.store.inner.DisconfCenterStore.storeOneFile(DisconfCenterStore.java:73) - There are two same fileName key!!!! activityrule.xml [11:26:13:443] [ERROR] - org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:321) - Context initialization failed java.lang.NoSuchMethodError: com.baidu.disconf.client.addons.properties.DefaultPropertyPlaceholderConfigurer.parseStringValue(Ljava/lang/String;Ljava/util/Properties;Ljava/util/Set;)Ljava/lang/String; at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.parseStringValue(ReloadingPropertyPlaceholderConfigurer.java:97) ~[disconf-client-2.6.36.jar:?] at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer$PlaceholderResolvingBeanDefinitionVisitor.resolveStringValue(ReloadingPropertyPlaceholderConfigurer.java:457) ~[disconf-client-2.6.36.jar:?] at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanClassName(BeanDefinitionVisitor.java:107) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:80) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] at com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer.processProperties(ReloadingPropertyPlaceholderConfigurer.java:382) ~[disconf-client-2.6.36.jar:?] at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) ~[spring-beans-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) ~[spring-context-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:164) ~[spring-context-5.0.2.RELEASE.jar:5.0.2.RELEASE]
我把spring的版本换回4.3.13.RELEASE,启动不报错了。
这里先记录下,诸位大神看看是什么问题,目前小弟赶项目进度,没时间研究,放给大家看看