我是Spring的新手,並試圖使用Spring Data和Spring MVC啓動並運行一個簡單的Web應用程序。Spring不能自動在控制器中存儲庫
這裏是我的控制器:
package org.springbyexample.web.servlet.mvc;
@Controller
@EnableJpaRepositories
public class PersonController {
@Autowired
private UserRepository userRepository;
public UserRepository getRepository() {
return userRepository;
}
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
// code removed
}
這裏是我的倉庫:
package org.springbyexample.web.orm.repository;
@Repository
public interface UserRepository extends JpaRepository<Users, String> {
Users findByUsername(String username);
}
這裏是我的webmvc-context.xml的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<context:component-scan base-package="org.springbyexample.web.servlet.mvc" />
<context:component-scan base-package="org.springbyexample.web.security" />
<jpa:repositories base-package="org.springbyexample.web.orm org.springbyexample.web.beans" entity-manager-factory-ref="emf"/>
<mvc:annotation-driven />
<mvc:view-controller path="/index.html" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/demodb" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="org.springbyexample.web.orm" />
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
在運行這個使用maven-碼頭-plugin,我收到以下錯誤:
2015-04-02 16:08:39.272:WARN::Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springbyexample.web.orm.repository.UserRepository org.springbyexample.web.servlet.mvc.PersonController.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springbyexample.web.orm.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springbyexample.web.orm.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
這是我的用戶實體:
package org.springbyexample.web.beans;
@Entity
public class Users {
private Timestamp activationDate;
private Timestamp registrationDate;
private int isActive;
private String role;
private String email;
private int userId;
@Id
private String username;
private String password;
private String fullname;
private String tranauth;
private String clientPIN;
// Getters and Setters for all of them
}
得到任何幫助。
發表您的回購IMPL,並嘗試移動@Repo註釋您IMPL而解決比界面。通常,impl被註釋。 – minion 2015-04-02 14:26:29
您的組件掃描是否提取了您的Repository軟件包? – Steve 2015-04-02 14:30:42
@minion Spring數據存儲庫接口通常沒有明確的實現。 – chrylis 2015-04-02 14:30:46