Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener

I have a spring project and try to make it use spring boot and to run at embadded tomcat following :

https://spring.io/guides/gs/rest-service/

This is my Application

//@Configuration
//@EnableAspectJAutoProxy
@SpringBootApplication
@ComponentScan(basePackages = "gux.prome")
public class Application {

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

}

If I use maven command: mvn spring-boot:run, the project starts fine, but I need to debug , so I run this main method at InteliJ, exception occurs:

Exception in thread "main" java.lang.IllegalArgumentException: Cannot instantiate interface org.springframework.context.ApplicationListener : org.springframework.boot.logging.ClasspathLoggingApplicationListener
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:414)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:394)
    at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:385)
    at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:263)
    at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at gux.prome.config.Application.main(Application.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
....

This is the pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>gux</groupId>
  <artifactId>prome-data</artifactId>


  <version>1.0-SNAPSHOT</version>
  <name>prome-data Maven Webapp</name>
  <url>http://maven.apache.org</url>

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

  <dependencies>

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

    <!-- guava -->
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>18.0</version>
    </dependency>
    <!-- end of guava -->


    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.9.2</version>
    </dependency>

  </dependencies>


  <properties>
    <java.version>1.7</java.version>
  </properties>


  <build>

    <finalName>prome-data</finalName>

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

      <!--<plugin>-->
      <!-- use java 7 -->
        <!--<artifactId> maven-compiler-plugin</artifactId>-->
        <!--<version>3.1</version>-->
        <!--<configuration>-->
          <!--<source> 1.7</source>   -->
          <!--<target> 1.7</target>-->
        <!--</configuration>-->
      <!--</plugin>-->

    </plugins>
  </build>

  <!--<repositories>-->
    <!--<repository>-->
      <!--<id>spring-releases</id>-->
      <!--<url>http://repo.spring.io/libs-release</url>-->
    <!--</repository>-->
  <!--</repositories>-->

  <!--<pluginRepositories>-->
    <!--<pluginRepository>-->
      <!--<id>spring-releases</id>-->
      <!--<url>http://repo.spring.io/libs-release</url>-->
    <!--</pluginRepository>-->
  <!--</pluginRepositories>-->


</project>
like image 949
JaskeyLam Avatar asked Mar 07 '16 12:03

JaskeyLam


People also ask

What is applicationcontext class in Spring Boot?

The SpringApplication class will attempt to create the right ApplicationContext for us, depending on whether we are developing a web application or not. For example, the algorithm used to determine if a web application comes from some dependencies like spring-boot-starter-web.

Why is my Spring Boot application not opening?

For example, the algorithm used to determine if a web application comes from some dependencies like spring-boot-starter-web. With that being said, the absence of these dependencies can be one of the reasons behind our error. Another cause would be missing the @SpringBootApplication annotation in the Spring Boot entry point class.

What is the use of webapplicationtype react in Spring Boot?

It's used to disable the webserver. Bear in mind that starting from Spring Boot 2.0, we can also use SpringApplicationBuilder to explicitly define a specific type of web application: For a WebFlux project, we can use WebApplicationType.REACTIVE.

Why can’t I start servletwebserverapplicationcontext in Spring Boot?

“ Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean ” says it all. It simply tells us that there is no configured ServletWebServerFactory bean in the ApplicationContext. The error comes up mainly when Spring Boot fails to start the ServletWebServerApplicationContext.


1 Answers

This is a symptom of a version mismatch somewhere in your Spring dependencies, but not necessarily just Spring Boot and Spring itself. I had this between my Spring Boot parent:

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

and my Spring Cloud Config dependency:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-client</artifactId>
  <version>1.0.4.RELEASE</version>
</dependency>

For some reason, I am not able to define the spring-cloud-config-client dependency without an explicit version declaration.

Updating both to the latest release version (1.3.5.RELEASE and 1.1.1.RELEASE) solved it.

Conclusion
Update your dependencies to the latest correct ones

like image 86
ISparkes Avatar answered Oct 21 '22 20:10

ISparkes