如题,配置文件如下:
#注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: sxd client: serviceUrl: defaultZone: http://es1:18000/eureka/,http://es2:18001/eureka/ #Spring 配置 spring: aop: auto: true proxy-target-class: false application: name: pisen-cloud-luna-ms-unions #Sring cloud 配置 jpa: showSql: true properties: hibernate: hbm2ddl: auto: update server: port: 58860 druid: url: jdbc:log4jdbc:mysql://lunadbs:33060/ms-goods?useSSL=false&useUnicode=true&characterEncoding=UTF-8 driverClass: net.sf.log4jdbc.DriverSpy username: root password: 123456 initialSize: 1 minIdle: 1 maxActive: 20 testOnBorrow: true mybatis: mapperLocations: classpath:mapper/*Mapper.xml typeAliasesPackage : com.pisen.cloud.luna.ms.unions.base.domain #日志 logging: level: debug #namesrvAddr地址 #生产者group名称 #事务生产者group名称 #消费者group名称 #生产者实例名称 #消费者实例名称 #事务生产者实例名称 #一次最大消费多少数量消息 #广播消费 #消费的topic:tag #启动的时候是否消费历史记录 #启动顺序消费 zebra: rocketmq: namesrvAddr: 127.0.0.1:9876 producerGroupName: producerGroupName transactionProducerGroupName: transactionProducerGroupName consumerGroupName: consumerGroupName producerInstanceName: producerInstanceName consumerInstanceName: consumerInstanceName producerTranInstanceName: producerTranInstanceName consumerBatchMaxSize: 1 consumerBroadcasting: false subscribe[0]: TopicTest1:TagA enableHisConsumer: false enableOrderConsumer: true
然后使用注解@ConfigurationProperties读取配置文件
package com.pisen.cloud.luna.ms.unions.rocketmq.properties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * 解析 配置文件 */ @ConfigurationProperties(prefix = RocketmqProperties.PREFIX) public class RocketmqProperties { public static final String PREFIX = "zebra.rocketmq"; private String namesrvAddr; private String producerGroupName; private String transactionProducerGroupName; private String consumerGroupName; private String producerInstanceName; private String consumerInstanceName; private String producerTranInstanceName; private int consumerBatchMaxSize; private boolean consumerBroadcasting; private boolean enableHisConsumer; private boolean enableOrderConsumer; private List subscribe = new ArrayList<>(); public String getNamesrvAddr() { return namesrvAddr; } public void setNamesrvAddr(String namesrvAddr) { this.namesrvAddr = namesrvAddr; } public String getProducerGroupName() { return producerGroupName; } public void setProducerGroupName(String producerGroupName) { this.producerGroupName = producerGroupName; } public String getTransactionProducerGroupName() { return transactionProducerGroupName; } public void setTransactionProducerGroupName(String transactionProducerGroupName) { this.transactionProducerGroupName = transactionProducerGroupName; } public String getConsumerGroupName() { return consumerGroupName; } public void setConsumerGroupName(String consumerGroupName) { this.consumerGroupName = consumerGroupName; } public String getProducerInstanceName() { return producerInstanceName; } public void setProducerInstanceName(String producerInstanceName) { this.producerInstanceName = producerInstanceName; } public String getConsumerInstanceName() { return consumerInstanceName; } public void setConsumerInstanceName(String consumerInstanceName) { this.consumerInstanceName = consumerInstanceName; } public String getProducerTranInstanceName() { return producerTranInstanceName; } public void setProducerTranInstanceName(String producerTranInstanceName) { this.producerTranInstanceName = producerTranInstanceName; } public int getConsumerBatchMaxSize() { return consumerBatchMaxSize; } public void setConsumerBatchMaxSize(int consumerBatchMaxSize) { this.consumerBatchMaxSize = consumerBatchMaxSize; } public boolean isConsumerBroadcasting() { return consumerBroadcasting; } public void setConsumerBroadcasting(boolean consumerBroadcasting) { this.consumerBroadcasting = consumerBroadcasting; } public boolean isEnableHisConsumer() { return enableHisConsumer; } public void setEnableHisConsumer(boolean enableHisConsumer) { this.enableHisConsumer = enableHisConsumer; } public boolean isEnableOrderConsumer() { return enableOrderConsumer; } public void setEnableOrderConsumer(boolean enableOrderConsumer) { this.enableOrderConsumer = enableOrderConsumer; } public List getSubscribe() { return subscribe; } public void setSubscribe(List subscribe) { this.subscribe = subscribe; } }
报错如下:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc
2018-07-20 15:30:25.059 ERROR 7344 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependency expressed through field 'properties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zebra.rocketmq-com.pisen.cloud.luna.ms.unions.rocketmq.properties.RocketmqProperties': Could not bind properties to RocketmqProperties (prefix=zebra.rocketmq, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at com.pisen.cloud.luna.ms.unions.PisenCloudLunaMsUnionsApplication.main(PisenCloudLunaMsUnionsApplication.java:12) [classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zebra.rocketmq-com.pisen.cloud.luna.ms.unions.rocketmq.properties.RocketmqProperties': Could not bind properties to RocketmqProperties (prefix=zebra.rocketmq, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:334) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:291) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE] ... 19 common frames omitted Caused by: java.lang.NullPointerException: null at org.springframework.boot.bind.RelaxedDataBinder.extendCollectionIfNecessary(RelaxedDataBinder.java:340) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.RelaxedDataBinder.initializePath(RelaxedDataBinder.java:290) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.RelaxedDataBinder.normalizePath(RelaxedDataBinder.java:259) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.RelaxedDataBinder.modifyProperty(RelaxedDataBinder.java:240) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.RelaxedDataBinder.modifyProperties(RelaxedDataBinder.java:155) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.RelaxedDataBinder.doBind(RelaxedDataBinder.java:128) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.validation.DataBinder.bind(DataBinder.java:740) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE] at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:272) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:329) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE] ... 32 common frames omitted
错误原因:
这里使用的配置文件是yml文件而不是properties文件,
所以读取资源文件的时候,
如果是读取yml文件,则应该配置成:
public static final String PREFIX = "zebra:rocketmq";
如果是读取properties文件,则应该配置成:
public static final String PREFIX = "zebra.rocketmq";
是真的恶心!!!!!