java.lang.IllegalState Exception LifecylceProcesso

2019-07-07 02:47发布

问题:

I have created a spring boot wrapper class as follows:

package package.for;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class AppBooter {

    public static void main(String args[]){
        SpringApplication.run("path/to/context.xml", args);
        System.out.println("============ done");
        do{

        }while(true);
    }
}

context.xml contains the bean pointing to the main class as follows:

<bean id="starter" class="package.for.AppBooter" />

as well as several other beans I require.

As for dependencies I have the following:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.1.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>axis</groupId>
        <artifactId>axis</artifactId>
        <version>1.4</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
            <exclusion>
                <groupId>axis</groupId>
                <artifactId>axis-wsdl4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId> wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.6.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.7</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>opensymphony</groupId>
        <artifactId>sitemesh</artifactId>
        <version>2.5</version>
    </dependency>

However when I run I get the following exceptions:

Exception in thread "main" 2014-10-07 16:12:54.267  INFO 4616 --- [       Thread-1] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6c4cbf96: startup date [Tue Oct 07 16:12:50 CEST 2014]; root of context hierarchy
java.lang.NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationDelegate.<init>(Lorg/springframework/data/repository/config/RepositoryConfigurationSource;Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/Environment;)V
    at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:58)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:319)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:139)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:324)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:608)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:463)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at package.for.AppBooter.main(AppBooter.java:12)
2014-10-07 16:12:54.303  WARN 4616 --- [       Thread-1] ationConfigEmbeddedWebApplicationContext : Exception thrown from ApplicationListener handling ContextClosedEvent

java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6c4cbf96: startup date [Tue Oct 07 16:12:50 CEST 2014]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:345)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:332)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:879)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.doClose(EmbeddedWebApplicationContext.java:141)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:808)

2014-10-07 16:12:54.304  WARN 4616 --- [       Thread-1] ationConfigEmbeddedWebApplicationContext : Exception thrown from LifecycleProcessor on context close

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6c4cbf96: startup date [Tue Oct 07 16:12:50 CEST 2014]; root of context hierarchy
    at org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:358)
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:887)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.doClose(EmbeddedWebApplicationContext.java:141)
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:808)

As for how I am packaging the jar here is the plugin snippet:

       <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>package.for.AppBooter</mainClass>
            </configuration>
            <version>1.1.5.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions
        </plugin>

回答1:

This was solved by upgrading spring-web and spring-webmvc dependencies to 4.0.7.RELEASE



回答2:

I was facing this error and I had already 4.3.2.RELEASE for spring-web and spring-web-mvc.

It is solved, this was due to defining spring-data-releasetrain. Removing that solved the problem.



回答3:

I think the root cause of the problem is a NoSuchMethodError:

java.lang.NoSuchMethodError: org.springframework.data.repository.config.RepositoryConfigurationDelegate.<init>(Lorg/springframework/data/repository/config/RepositoryConfigurationSource;Lorg/springframework/core/io/ResourceLoader;Lorg/springframework/core/env/Environment;)V
    at org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport.registerBeanDefinitions(AbstractRepositoryConfigurationSourceSupport.java:58)

You haven't posted any details of your Spring Data dependencies, but looks like you're trying to use a version of one of the Spring Data projects that isn't compatible with the version of Spring Boot that you're using. Spring Boot provides dependency management for then entire Spring Data release train so your best option is probably to just remove the version from any Spring Data dependencies in your pom and let Spring Boot's dependency management take control of the version.



回答4:

Had same issue and was not dependencies related.

I had the following code:

@Configuration
public class ConfigurationClass {
    private final MyProperties MY_PROPERTIES;

    ConfigurationClass(MyProperties myProperties) {
        this.MY_PROPERTIES = myProperties
    }

    @Bean
    @LoadBalanced
    public RestTemplate testRestTemplate(RestTemplateBuilder builder) {
        return builder.setConnectTimeout(MY_PROPERTIES.getTimeout()).build();
    }
}

Changing it to the following made it work:

@Configuration
public class ConfigurationClass {

    @Bean
    @LoadBalanced
    public RestTemplate testRestTemplate(RestTemplateBuilder builder,
                                         MyProperties myProperties) {
        return builder.setConnectTimeout(myProperties.getTimeout()).build();
    }
}

I am not sure why this was causing an issue though. Maybe you can't use constructor injection when you want to use it inside beans. Hope someone can answer in the comments