簡體   English   中英

Spring Data JPA沒有類型的限定bean ...找到依賴項

[英]Spring Data JPA No qualifying bean of type … found for dependency

我有測試Spring Data JPA的示例測試程序,但似乎沒有生成存儲庫。

我的配置:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee.xsd">

    <import resource="securityConfig.xml" />

    <context:annotation-config />
    <context:component-scan base-package="com.test">
        <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/test"/>

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.test.security" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf" />
    </bean>
</beans>

用戶實體:

package com.test.security;

import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table
public class UserPrincipal implements UserDetails, CredentialsContainer, Cloneable {
    private static final long serialVersionUID = 1L;

    private long id;
....
}

UserRespository:

package com.test.security;

import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<UserPrincipal, Long>
{
    UserPrincipal getByUsername(String username);
}

UserService:

package com.test.security;

@Service
public class UserService implements UserDetailsService {
    @Inject
    UserRepository userRepository;

    @Override
    @Transactional
    public UserPrincipal loadUserByUsername(String username) {
        UserPrincipal principal = userRepository.getByUsername(username);
        // make sure the authorities and password are loaded
        principal.getAuthorities().size();
        principal.getPassword();
        return principal;
    }
}

我收到此錯誤:

org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.filterChains'的bean時出錯:在設置bean屬性'sourceList'時無法解析對bean'org.springframework.security.web.DefaultSecurityFilterChain#0'的引用用鍵[0]; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.web.DefaultSecurityFilterChain#0'的bean時出錯:無法解析對bean的引用'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#使用鍵[3]設置構造函數參數時為0'; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0'的bean時出錯:無法解析對bean的引用'org.springframework.security.authentication.ProviderManager#設置bean屬性'authenticationManager'時為0'; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.authentication.ProviderManager#0'的bean時出錯:無法解析對bean的引用'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#設置構造函數參數時為0'; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0'的bean時出錯:FactoryBean在創建對象時拋出異常; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.authenticationManager'的bean時出錯:設置時無法解析bean'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的引用帶鍵[0]的構造函數參數; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0'的bean時出錯:在設置bean屬性'userDetailsS​​ervice'時無法解析對bean'userService'的引用; 嵌套異常是org.springframework.beans.factory.BeanCreationException:創建名為'userService'的bean時出錯:注入自動連接的依賴項失敗; 嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配字段:com.test.security.UserRepository com.test.security.UserService.userRepository; 嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有為依賴項找到類型為[com.test.security.UserRepository]的限定bean:期望至少有一個bean可以作為此依賴項的autowire候選者。 依賴注釋:{@ javax.inject.Inject()}

找不到[com.test.security.UserRepository]類型的限定bean用於依賴:預期至少有1個bean符合此依賴關系的autowire候選者。 依賴注釋:{@ javax.inject.Inject()}

抓取spring數據jpa命名空間(來自spring-data-jpa jar)

xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=
            http://www.springframework.org/schema/data/jpa 
            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

並使用jpa命名空間的<repositories>元素來掃描存儲庫

<jpa:repositories base-package="com.test.security"
                  entity-manager-factory-ref="myEmf"
                  transaction-manager-ref="transactionManager"/> 

有關創建存儲庫實例的更多信息

這是一個關於<repositories>標簽的片段:

Spring被指示掃描[ com.test.security ]及其所有子包,以查找擴展Repository或其子接口之一的接口。 對於找到的每個接口,基礎結構注冊特定於持久性技術的FactoryBean以創建處理查詢方法調用的相應代理。

這是命名空間信息的鏈接

對於Java配置,您可以使用@EnableJpaRepositories注釋實現相同的@EnableJpaRepositories 您可以在上面的相同鏈接中閱讀更多相關信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM