Spring Boot - BeanDefinitionParsingException:上下文中只能存在一个ScheduledAnnotationBeanPostProcessor

时间:2016-09-01 11:24:10

标签: java spring spring-boot spring-scheduled

这是重现问题的真实场景的简化。配置过程可能看起来很奇怪,但是因为我已将所有配置类放在同一个项目中,但实际上它们在应用程序和一些库之间是分开的。

这是我的 Spring Boot应用程序主类

@SpringBootApplication(scanBasePackageClasses={ SomeConfig.class})
public class MySpringBootApplication {

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

SomeConfig.java:

@Configuration
@ImportResource("classpath:library.mail-executor.xml")
public class SomeConfig{}

library.mail-executor.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd ">

    <bean class=xxx.MyAsyncExecutorConfig"/>

</beans>

MyAsyncExecutorConfig.java:

@Configuration
@ImportResource("classpath:library.jobs-executor.xml")
public class MyAsyncExecutorConfig {}

}

library.jobs-executor.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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <task:annotation-driven executor="mainWorkExecutor"/>

    <bean id="mainWorkExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="${jobs.corePoolSize:5}" />
        <property name="maxPoolSize" value="${jobs.maxPoolSize:10}" />
        <property name="queueCapacity" value="${jobs.queueCapacity:25}" />
    </bean>

</beans>

总结一下,SomeConfig导入library.mail-executor.xml导入MyAsyncExecutorConfig进口library.jobs-executor.xml

这是异常:配置问题:上下文中只能存在一个ScheduledAnnotationBeanPostProcessor

2016-09-01T13:02:56.935 ( -  -  -  -  - ) o.s.b.SpringApplication ERROR - Application startup failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Only one ScheduledAnnotationBeanPostProcessor may exist within the context.
Offending resource: class path resource [es/mma/architecture/spring/library.jobs-executor.xml]
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:72) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.scheduling.config.AnnotationDrivenBeanDefinitionParser.parse(AnnotationDrivenBeanDefinitionParser.java:90) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1411) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1401) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:168) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:138) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromImportedResources(ConfigurationClassBeanDefinitionReader.java:346) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]

事实:

  

我在任何地方都没有@EnableScheduling

     

非常奇怪,如果我摆脱MyAsyncExecutorConfig并直接导入library.jobs-executor.xml SomeConfig,它就可以了。 !!! ???       例如:

SomeConfig.java:

@Configuration
@ImportResource("classpath:library.jobs-executor.xml")
public class SomeConfig{}
  

如果我直接导​​入library.jobs-executor.xml   library.mail-executor.xml它也有效。 !!! ???例如:

SomeConfig.java:

@Configuration
@ImportResource("classpath:library.mail-executor.xml")
public class SomeConfig{}

library.mail-executor.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd ">

    <import resource="classpath:library.jobs-executor.xml"/>
</beans>

因此,只有在上一个library.jobs-executor.xml课程中导入@Configuration时才会出现此问题。

请注意,我无法修改此流程。为什么后处理器似乎被注册了两次????

1 个答案:

答案 0 :(得分:0)

问题在于执行器。它包含一个使用@EnableScheduler并创建冲突的后处理器的度量标准导出器。

您可以使用spring.metrics.export.enabled: false停用它,也可以添加@Condition,以便在library.jobs-executor.xml存在时不导入它:

public class InternalScheduledAnnotationProcessorDoesntExistCondition implements ConfigurationCondition{

    @Override
    public ConfigurationPhase getConfigurationPhase() {
        return ConfigurationPhase.PARSE_CONFIGURATION;
    }

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        try{
            context.getRegistry().getBeanDefinition("org.springframework.context.annotation.internalScheduledAnnotationProcessor");
            return false;
        }catch(NoSuchBeanDefinitionException nsde){
            return true;
        }
    }

}

<强> MyAsyncExecutorConfig.java:

@Configuration
@Conditional(InternalScheduledAnnotationProcessorDoesntExistCondition.class)
@ImportResource("classpath:library.jobs-executor.xml")
public class MyAsyncExecutorConfig {}