我是Spring的新手,并且正在尝试使用Spring Data和Spring MVC创建一个简单的Web应用程序。
这是我的控制器:
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
}
感谢任何帮助。
答案 0 :(得分:1)
在你的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,因为它不需要autowire enoguh来做魔术。
<jpa:repositories base-package="org.springbyexample.web.orm" entity-manager-factory-ref="emf"/>
答案 1 :(得分:0)
必须在@Enable
类上指定@Configuration
注释。将它放在您的控制器上将无效。如果存储库接口与带注释的配置类位于不同的包树中,则可能还需要指定基本包。
(另外,您不应该注释存储库接口;当您希望Spring Data修改处理时,只需使用实际DAO类的注释。)
答案 2 :(得分:0)
如果有的话,可以尝试在具有public static void main(String ... args)的类中添加以下注释。或者只是尝试将其添加到控制器。
@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableAutoConfiguration
答案 3 :(得分:0)
在我的情况下,通过更改(移动)
来解决问题<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/root-context.xml
</param-value>
</context-param>
到
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>{code}