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

时间:2016-08-11 04:54:41

标签: java spring spring-mvc spring-jdbc nosuchmethoderror

运行服务器时,我在控制台上遇到以下错误:

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;

网页上有404错误。我检查了网址,资源已经存在。

以下是我的xml:

弹簧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类

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!!!");
   }
}

3 个答案:

答案 0 :(得分:1)

由于具有不同版本的多个相同jar文件,可能会发生这种情况。确保没有多个具有不同版本Spring的jar文件。

答案 1 :(得分:1)

您的bean尚未配置。 在spring-servlet.xml中配置它

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

答案 2 :(得分:1)

还要考虑以下事项。

  • 检查您的罐子是否有必要的方法。
  • 检查同一方法是否不能在多个jar中,以便它们都试图将其索引提供给jvm,&amp; JVM很难选择正确的。
  • 列出你的jar / dependency删除不必要的
  • 使用maven下载所需的依赖项或从spring.io/projects,grepcode。
  • 下载
  

有时候我们需要jar,但是我们没有必要的方法     可能是由于传统等,所以请为它选择合适的罐子。