2015-04-07 127 views
0

[RMI TCP Connection(7)-127.0.0.1] ERROR org.springframework.web.servlet.DispatcherServlet - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.springapp.mvc.UserRepository com.springapp.mvc.UserController.userRepository; nested exception is java.lang.NoSuchMethodError:錯誤org.springframework.web.servlet.DispatcherServlet - 上下文初始化失敗org.springframework.beans.factory.BeanCreationException:錯誤

User.java

package com.springapp.mvc; 

import javax.persistence.*; 

@Entity(name = "account") 
public class User { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 

    @Basic 
    private String firstName; 

    @Basic 
    private String lastName; 

    @Basic 
    private String email; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String name) { 
     this.firstName = name; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
} 

UserRepository.java

package com.springapp.mvc; 

import org.springframework.data.jpa.repository.JpaRepository; 

public interface UserRepository extends JpaRepository<User, Long> { 
} 

UserRepositoryImpl.java

package com.springapp.mvc; 

import org.springframework.data.domain.Page; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.domain.Sort; 
import org.springframework.stereotype.Component; 

import java.util.List; 

@Component 
public class UserRepositoryImpl implements UserRepository{ 

    public UserRepositoryImpl() { 
    } 

    @Override 
    public <S extends User> S save(S entity) { 
     return null; 
    } 

    @Override 
    public User findOne(Long aLong) { 
     return null; 
    } 

    @Override 
    public boolean exists(Long aLong) { 
     return false; 
    } 

    @Override 
    public List<User> findAll() { 
     return null; 
    } 

    @Override 
    public List<User> findAll(Sort sort) { 
     return null; 
    } 

    @Override 
    public Page<User> findAll(Pageable pageable) { 
     return null; 
    } 

    @Override 
    public List<User> findAll(Iterable<Long> longs) { 
     return null; 
    } 

    @Override 
    public long count() { 
     return 0; 
    } 

    @Override 
    public void delete(Long aLong) { 

    } 

    @Override 
    public void delete(User entity) { 

    } 

    @Override 
    public void delete(Iterable<? extends User> entities) { 

    } 

    @Override 
    public void deleteAll() { 

    } 

    @Override 
    public void flush() { 

    } 

    @Override 
    public void deleteInBatch(Iterable<User> entities) { 

    } 

    @Override 
    public void deleteAllInBatch() { 

    } 

    @Override 
    public User getOne(Long aLong) { 
     return null; 
    } 

    @Override 
    public <S extends User> S saveAndFlush(S entity) { 
     return null; 
    } 

    @Override 
    public <S extends User> List<S> save(Iterable<S> entities) { 
     return null; 
    } 
} 

UserController.java

package com.springapp.mvc; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.*; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.validation.BindingResult; 

@Controller 
public class UserController { 
    @Autowired 
    private UserRepository userRepository; 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String listUsers(ModelMap model) { 
     model.addAttribute("user", new User()); 
     model.addAttribute("users", userRepository.findAll()); 
     return "users"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addUser(@ModelAttribute("user") User user, BindingResult result) { 

     userRepository.save(user); 

     return "redirect:/"; 
    } 

    @RequestMapping("/delete/{userId}") 
    public String deleteUser(@PathVariable("userId") Long userId) { 

     userRepository.delete(userRepository.findOne(userId)); 

     return "redirect:/"; 
    } 
} 

MVC-調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

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

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

    <jpa:repositories base-package="com.springapp.mvc"/> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="defaultPersistenceUnit"/> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

</beans> 

的web.xml

<web-app version="2.4" 
    xmlns="http ://java.sun.com/xml/ns/j2ee" xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

您能否包含完整的錯誤輸出? –

+0

完整的錯誤輸出在這裏https://www.dropbox.com/s/fmfj8254396vkod/outputlog.txt?dl=0 –

+0

似乎有一個版本不匹配,你可以包括你的pom.xml嗎? –

回答

2

完整的堆棧跟蹤顯示:

Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.findPropertyForMethod(Ljava/lang/reflect/Method;Ljava/lang/Class;)Ljava/beans/PropertyDescriptor; 

此問題是由使用(4.1.1)的彈簧芯版本,它只有一個類型的方法參數(檢查here)引起的。

存在依賴關係不匹配,請嘗試將Spring版本更改爲4.1.3或更高版本。

相關問題