我是Spring的新手,正在尝试使用Spring Data和Spring MVC来启动并运行一个简单的Webapp。
这是我的控制器:
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-jetty-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
}
感谢任何帮助。
最佳答案
在您的xml中
<jpa:repositories base-package="org.springbyexample.web.orm org.springbyexample.web.beans" entity-manager-factory-ref="emf"/>
是不正确的。
如果您的存储库仅位于org.springbyexample.web.orm包下
然后只需删除org.springbyexample.web.beans,使其看起来像
同样,在控制器中,也要删除getter和setter,因为它不需要自动接线enoguh来完成魔术。
<jpa:repositories base-package="org.springbyexample.web.orm" entity-manager-factory-ref="emf"/>