0

All of a sudden I am getting:

[localhost-startStop-1] WARN  org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.accept.ContentNegotiationManager.getStrategies()Ljava/util/List;
[localhost-startStop-1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.web.accept.ContentNegotiationManager.getStrategies()Ljava/util/List;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

I don't know what I did/changed - it's just coming all of a sudden as I startup the server and request my website.

You can see the full stack trace with all exceptions here.

Any guess what's causing this all of a sudden?


My pom.xml (shortened to the essential dependencies):

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- ... -->

    <properties>
        <org.springframework-version>4.2.4.RELEASE</org.springframework-version>
        <org.springframework.security-version>4.0.3.RELEASE</org.springframework.security-version>
    </properties>

    <!-- ... -->

    <dependencies>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <!-- Spring Dependencies -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-acl</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

        <!-- Required by spring-security-acl -->
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org.springframework.security-version}</version>
        </dependency>

    </dependencies>

    <profiles>
        <!-- ... -->
    </profiles>

    <build>
        <plugins>
            <!-- ... -->
        </plugins>
    </build>
</project>
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

4

If you have worked on maven in your projects for dependency management, then you must have faced one problem at least once or may be more than that. And the problem is version mismatch. It generally happens when you got some dependencies which bring it’s related dependencies together with certain version. And if you have included those dependencies with different version numbers already, they can face undesired results in compile time as well as runtime also.

Ideally to avoid above issue you need to explicitly exclude the related dependency, but it is quite possible that you can forget to do so.

To solve version mismatch issue, you can use the concept of a “bill of materials” (BOM) dependency. A BOM dependency keep track of version numbers and ensure that all dependencies (both direct and transitive) are at the same version.

How to add BOM [Bill Of Materials] dependency

Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.2.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>

Similarly BOM for Spring security to use is

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-bom</artifactId>
    <version>4.0.3.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

In your case its problem of version mismatch which I too had faced earlier

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
  • I don't really understand. What version would I have to use for Spring Security then? I used that version-combination for a year now without any problems. I also do not see a difference to your solution to that what I am doing. Okay, now I can spare the `` tag but what does this change besides that? – Stefan Falk May 25 '16 at 20:11
  • You should use 4.0.3 version of Spring security, also did you changed Java version by any chance? – rajadilipkolli May 26 '16 at 04:11
  • I changed it to `4.0.3.RELEASE` and now it works. I don't get it why I have not had a problem a whole year. I didn't touch anything Spring related and I didn't change the Java version either. I tried `4.1.0.RELEASE` as well since this is the current version for Spring Security but that throws some other errors. I will upgrade someday but for now I just has to work. Thank you! – Stefan Falk May 26 '16 at 07:15
  • if you upgrade to spring security version 4.1.0 then you should also upgrade core spring to latest version , there will be some internal sping dependency which caused that issue going forward use Bill of materials for version conflicts. – rajadilipkolli May 26 '16 at 12:07