ApplicationEventMulticaster未初始化 - 在通过上下文组播事件之前调用'refresh':AnnotationConfigApplicationContext

时间:2017-11-08 07:53:42

标签: java spring spring-boot

我有两个配置文件具有相同的bean定义和不同的@Qualifier,如下所示。并且想要在我的应用程序中使用那两个bean。为了避免在Spring启动时覆盖bean定义,我使用了ApplicationContextInitializer,并得到了以下异常。

首次配置

@Bean(destroyMethod = "shutdown")
    @Qualifier("batchAmazonSQSAsync")
    public AmazonSQSAsync amazonSQS(@Qualifier("batchClientConfiguration") ClientConfiguration clientConfiguration,
            @Qualifier("batchAWSCredentialsProviderChain") AWSCredentialsProviderChain credentialsProviderChain) {



        AmazonSQSAsync amazonSQSAsyncClient = AmazonSQSAsyncClientBuilder.standard()
                .withClientConfiguration(clientConfiguration).withCredentials(credentialsProviderChain)
                .withRegion(Regions.fromName(region)).build();
        return new AmazonSQSBufferedAsyncClient(amazonSQSAsyncClient);
    }

    @Bean
    @Qualifier("batchClientConfiguration")
    ClientConfiguration clientConfiguration() {     
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProxyHost(proxyHost);
        clientConfiguration.setProxyPort(proxyHostPort);
        clientConfiguration.setProxyUsername(proxyUserName);
        clientConfiguration.setProxyPassword(proxyPassword);
        return clientConfiguration;
    }

    @Bean
    @Qualifier("batchAWSCredentialsProviderChain")
    AWSCredentialsProviderChain credentialsProviderChain() {

        return new AWSCredentialsProviderChain(
                new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
    }

第二种配置

@Bean(destroyMethod = "shutdown")
    @Qualifier("priorityAmazonSQSAsync")
    public AmazonSQSAsync amazonSQS(@Qualifier("priorityClientConfiguration") ClientConfiguration clientConfiguration,
            @Qualifier("priorityAWSCredentialsProviderChain") AWSCredentialsProviderChain credentialsProviderChain) {       

        AmazonSQSAsync amazonSQSAsyncClient = AmazonSQSAsyncClientBuilder.standard()
                .withClientConfiguration(clientConfiguration).withCredentials(credentialsProviderChain)
                .withRegion(Regions.fromName(region)).build();
        return new AmazonSQSBufferedAsyncClient(amazonSQSAsyncClient);
    }

    @Bean
    @Qualifier("priorityClientConfiguration")
    ClientConfiguration clientConfiguration() { 

        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setProxyHost(proxyHost);
        clientConfiguration.setProxyPort(proxyHostPort);
        clientConfiguration.setProxyUsername(proxyUserName);
        clientConfiguration.setProxyPassword(proxyPassword);
        return clientConfiguration;
    }

    @Bean
    @Qualifier("priorityAWSCredentialsProviderChain")
    AWSCredentialsProviderChain credentialsProviderChain() {    

        return new AWSCredentialsProviderChain(
                new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)));
    }

春季启动应用程序

public static void main(String[] args) {

        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(SqslistenerApplication.class)
                .initializers(new ApplicationContextInitializer<GenericApplicationContext>() {
                    @Override
                    public void initialize(GenericApplicationContext applicationContext) {
                        applicationContext.setAllowBeanDefinitionOverriding(false);
                    }
                }).run(args);
        ctx.refresh();

        // SpringApplication.run(SqslistenerApplication.class, args);

    }
运行应用程序时出现

异常

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@e056f20: startup date [Wed Nov 08 13:06:32 IST 2017]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:414) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:961) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:230) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:968) [spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1030) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:556) [spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:134) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at demo.sqs.SqslistenerApplication.main(SqslistenerApplication.java:20) [classes/:na

的pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd">     4.0.0

<groupId>demo.sqs</groupId>
<artifactId>sqslistener</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>sqslistener</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-messaging</artifactId>
    </dependency>



    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

0 个答案:

没有答案