Issue
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!!!");
}
}
Solution
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.
Answered By - KPJAVA
Answer Checked By - Marie Seifert (JavaFixing Admin)