繁体   English   中英

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

[英]java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation

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

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 错误。 我已经检查了 url 和资源是否存在。

这是我的xml:

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 类

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

这可能是由于多个具有不同版本的相同jar文件引起的。 确保您没有使用不同版本的Spring的多个jar文件。

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

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

还应注意以下事项。

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

上一次,我们需要罐子,但由于遗留原因,我们没有所需的方法,因此请为其选择合适的罐子。

org.springframework.beans.factory.BeanCreationException:创建类org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration中定义的名称为“requestMappingHandlerMapping”的bean时出错:通过工厂方法进行的bean实例化失败; 嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]:工厂方法“requestMappingHandlerMapping”抛出异常; 嵌套异常是 java.lang.NoSuchMethodError: 'java.lang.annotation.Annotation org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation(java.lang.reflect.AnnotatedElement, java.lang.Class)'

在将我们的 ant 项目迁移到 maven 项目时,我也遇到了同样的问题。

调试一整天后,根据上述数据。 我观察到 activemq-all 5.13.2 jar 内部包含 Spring jar。

问题之前我的 Maven 依赖项。

    <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>

在我们的 ant 项目中,我们使用的是 activemq-all 5.9.0 jar。 通过使用这个 jar,问题得到了解决。 因为这个版本没有弹簧罐。

    <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>

结论:每当我们遇到此类问题时,我们都需要检查我们的依赖项正在使用的内部 jar。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM