Rahul Agrawal
Rahul Agrawal

Reputation: 633

java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation

I am getting following error on console when I run the server:

Error creating bean with name  
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMa
pping#0': Invocation of init method failed; nested exception is 
java.lang.NoSuchMethodError:    
org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation
(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/Class;)Ljava/lang/annotation/Annotation;

And 404 error on webpage. I have checked the url and resource is existing.

Here are my xml's :

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.abhishek"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>


<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"></property>
    <property name="username" value="system"></property>
    <property name="password" value="pass"></property>
</bean>

<bean id="myJDBC" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="myDataSource"></property>
</bean>


</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">
  <display-name>EmployeeDetails</display-name>
  <servlet>
      <servlet-name>employeermvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>employeermvc</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

   <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring-servlet.xml</param-value>
   </context-param>
</web-app>

MyController class

package com.abhishek.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.abhishek.Dao.EmployeeDao;
import com.abhishek.bean.Employee;

@Controller
public class MyController {

    @Autowired
    private EmployeeDao dao;

    @RequestMapping("/")
    public String newEmployee(ModelMap model) {
        Employee employee = new Employee();
        model.addAttribute("employee", employee);
        return "create";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String insertEmployee(@ModelAttribute("employee") Employee employee) {

        dao.insert(employee);
        return "inserted";

    }

}

DAoImpl :

package com.abhishek.Dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.abhishek.bean.Employee;

@Component
public class EmployeeDaoImpl implements EmployeeDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public EmployeeDaoImpl(){

    }

    public EmployeeDaoImpl(JdbcTemplate jdbcTemplate) {
       super();
       this.jdbcTemplate = jdbcTemplate;
    }

    @Override
        public void insert(Employee emp) {
        String sql="insert into employee(e_id,name,desig,deptt,basic) values(?,?,?,?,?)";
       jdbcTemplate.update(sql, emp.getEid(),emp.getName(),emp.getDesg(),emp.getDept(),emp.getBasic());
       System.out.println("Record Inserted!!!");
   }
}

Upvotes: 2

Views: 12208

Answers (4)

Marreddy
Marreddy

Reputation: 21

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]: Factory method 'requestMappingHandlerMapping' threw exception; nested exception is java.lang.NoSuchMethodError: 'java.lang.annotation.Annotation org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation(java.lang.reflect.AnnotatedElement, java.lang.Class)'

I also faced the same issue while migrating our ant project to maven project.

As per the above data after debugging the issue for entire day. I observed that activemq-all 5.13.2 jar contains Spring jars internally.

My maven dependencies before the issue.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.20.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.20.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.13.2</version>
    </dependency>

In our ant project we are using activemq-all 5.9.0 jar. By using this jar, issue got fixed. As there are no spring jars in this version.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.20.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.20.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.9.0</version>
    </dependency>

Conclusion : Whenever we get this type of issue we need to inspect internal jars that our dependences are using.

Upvotes: 0

Lokesh_K
Lokesh_K

Reputation: 563

Also look into following things.

  • Check whether your jars has required method.
  • Check whether same method must not be in more than one jar so that they both are trying to give their index to jvm, & JVM confused to pick correct.
  • List your jars/dependency remove unnecessary
  • Use maven to download required dependency or download from spring.io/projects , grepcode.

LAST sometime we have required jar but we do not have required method may be due to legacy etc so please choose appropriate jar for it.

Upvotes: 1

Rajesh
Rajesh

Reputation: 662

your beans are not yet configured. configure it in your spring-servlet.xml

<bean id="employeeDao" class="com.abhishek.EmployeeDao">
    <property name="jdbcTemplate" ref="myJDBC"></property>
</bean>

Upvotes: 1

KPJAVA
KPJAVA

Reputation: 70

This may be occuring because of multiple same jar file with different versions. Make sure you don't have multiple jar files with different versions of Spring.

Upvotes: 2

Related Questions