2015-04-02 23 views
0

我是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 

}

得到任何幫助。

+0

發表您的回購IMPL,並嘗試移動@Repo註釋您IMPL而解決比界面。通常,impl被註釋。 – minion 2015-04-02 14:26:29

+0

您的組件掃描是否提取了您的Repository軟件包? – Steve 2015-04-02 14:30:42

+0

@minion Spring數據存儲庫接口通常沒有明確的實現。 – chrylis 2015-04-02 14:30:46

回答

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"/> 
+0

由於存儲庫變量是私有的,你不應該需要getter和setter嗎? – n3o 2015-04-02 14:46:18

+0

在PersonController類中,您必須使用'userRepository'實例。我的意思是說,在同一個班級中,或者你在'PersonController'之外訪問'userRepository'。如果它是這樣的話,那就意味着它的編碼標準不好。 – 2015-04-02 14:48:30

+0

現在我得到一個新的異常:java.lang.IllegalArgumentException:在控制器中啓動存儲庫時,不是託管類型:類org.springbyexample.web.beans.Users。 – n3o 2015-04-02 14:48:31

0

@Enable註釋必須在@Configuration類中指定。將它放在你的控制器上將不起作用。如果存儲庫接口與註釋配置類位於不同的包樹中,則可能還需要指定基本包。

(此外,你不應該註釋庫接口,只需用實際DAO類註釋,當你希望Spring數據修改操作。)

+0

我沒有@Configuration類。它是爲了什麼?我應該創建一個嗎?是的,我的存儲庫與控制器位於不同的包中,但是我的webmvc-context.xml中的配置沒有處理這個問題嗎? – n3o 2015-04-02 14:37:14

+0

@ n3o不需要。 – 2015-04-02 14:43:06

+1

@ n3o'@ Configuration'是XML配置的基於Java的替代方法,'@ EnableJpaRepositories'僅在用@ Configuration配置註解的類上使用時纔有效。由於看起來您在XML中包含'jpa:repositories'(我錯過了它),所以根本不需要註釋。 – chrylis 2015-04-02 15:11:17

0

你可以嘗試在具有公共靜態無效的主要(字符串參數... args)如果你有一個類中添加下面的註釋。或者只是嘗試將其添加到控制器。

@Configuration 
@ComponentScan 
@EnableJpaRepositories 
@EnableAutoConfiguration 
+0

它不是必需的。 – 2015-04-02 14:41:39

+0

仍然是同樣的錯誤。 – n3o 2015-04-02 14:50:58

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} 
相關問題