2017-11-25 9 views
0

に名前付きクエリと春データリポジトリを注入する私は、次のリポジトリを持っています。ここではこのような単純なサービス

package br.com.portal.repository; 

public interface UserRepository extends CrudRepository<User, Long> { 

    @Query("SELECT u FROM User WHERE u.login = :login") 
    User findByLogin(@Param("login") String login); 

} 

が、CrudRepositoryで定義されているすべての一般的なCRUD操作を継承しても公開することになっていますfindByLogin機能。 ほとんどの例は、すべてではないにしても、そのようなリポジトリに@Repository注釈で注釈を付けることはありません。何故ですか?このインターフェイスを実装する必要があるのですか、または@Queryが何とかそれを裏返しにしていますか?ここで

は、私が現在持っているものです。

package br.com.portal.service; 

public interface UserService { 
    User findByLogin(String login); 
} 

@Service 
public class UserServiceImpl implements UserService { 

    private UserRepository repository; 

    @Autowired 
    public UserServiceImpl(UserRepository repository) { 
     this.repository = repository; 
    } 

    User findByLogin(String login) { 
     return repository.findByLogin(login); 
    } 

} 

と春-mvc.xml:

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop.xsd"> 

    <!-- Defines the static resources location, otherwise resource requests will result in 404 errors (not found) --> 
    <mvc:resources mapping="/assets/**" location="/assets/" order="0" cache-period="31556926" /> 
    <mvc:resources mapping="/favicon.ico" location="/assets/icon/favicon.ico" cache-period="31556926" /> 

    <!-- Defines the custom Spring components packages --> 
    <context:component-scan base-package="br.com.portal.repository"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> 
    </context:component-scan> 
    <context:component-scan base-package="br.com.portal.service"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> 
    </context:component-scan> 
    <context:component-scan base-package="br.com.portal.controller"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan> 

    <!-- JPA --> 
    <bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory"> 
     <property name="persistenceUnitName" value="default" /> 
    </bean> 
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 

私は春のブートを使用していません。現在上記の情報では

、我々は次のエラーを再現することができるはずです。

Error creating bean with name 'userServiceImpl' defined in file xxx: Unsatisfied dependency expressed through constructor parameter 0; nested exception is NoSuchBeanDefinitionException: No qualifying bean of type 'UserRepository'

私は何かが足りないのですか?

答えて

-1

注釈@Repository

+0

それは問題を解決しません。 –

+0

リポジトリがスキャンされることを確認してください –

+0

UserRepositoryを実装すると動作しますが、 '@ Query'と' CrudRepository'という目的を失います。 –

1

春とUserRepositoryインタフェースが正しく見える「userServiceImpl」という名前のBeanは、ありません、あなたを語っています。そのテキスト(大文字と小文字を区別)は存在しません。 Beanに名前を付ける方法を見てください。 @Serviceアノテーションに名前を付けるだけでよいでしょう。

+0

その豆は正しいです。私のコメントを読んだら、 'UserRepository'インターフェースを実装するとうまくいくと思います。つまり、Springはそのリポジトリをリポジトリとして認識していません。 –

1

実装を提供するためにリポジトリインタフェースをスキャンするようにspringに指示する必要があります。 beansタグに

xmlns:jpa="http://www.springframework.org/schema/data/jpa" 

をしてから

<jpa:repositories base-package="br.com.portal.repository"/> 

内でこの行を含める:XMLを使用すると、名前空間を追加することによってこれを行います。 See this answer for more context

インターフェイスに@Repository注釈は必要ありません。それは基本的に暗黙のうちに拡張から来るCrudRepository

+0

ありがとう、@ジェン。私は自分の質問に答えるつもりだったが、あなたはそれを最初にやった。それは私には分かりません。また、答えを更新して、 'pom.xml'にSpringデータバージョン' 1.9.0.RELEASE'を含めるようにしてください。最近のバージョンでは、AbstractMethodErrorがスローされています(stackoverflowの答えによると、 'spring-data-jpa'と' spring-data-commons' jarのバージョン/依存関係間の競合に関連しています)。 –

+0

あなたは何を意味するのかよく分かりません。あなたも私もポンを参照しておらず、質問にも関係していないようです。あなたが言及したバージョンの競合に関するこのディスカッションへのリンクがありますか? –

+0

私はMavenプロジェクトを持っており、上記のサンプル(あなたの回答を含む)は 'Spring Data 1.9.0.RELEASE'でしか動作しません。最新バージョン(2.0.2.RELEASE)を使用すると、次のエラーでクラッシュします。 'org.springframework.beans.factory.BeanCreationException: 'userRepository'という名前のBeanを作成中にエラーが発生しました:Beanの初期化に失敗しました。ネストされた例外はjava.lang.AbstractMethodError'です。私はもう議論を見つけることができませんでしたbtw –

関連する問題