簡體   English   中英

沒有為依賴項找到UserRepository類型的限定bean:預期至少有1個bean符合此依賴項的autowire候選者

[英]No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

嘿我有從Spring data擴展存儲庫的問題。

我有與服務層對話的控制器:

@RestController
public class UserController {
    @Autowired
    public UserService userService;

    @RequestMapping(value = ServerRouting.UserService.getList, method = RequestMethod.GET)
    public @ResponseBody Iterable<UserEntity> getList() {
        return userService.getList();
    }
}

這是服務層:

@Service
public class UserService {
    @Autowired
    public UserRepository repository;

    @Transactional
    public Iterable<UserEntity> getList() {
        return repository.findAll();
    }
}

服務層與存儲庫/ dao層對話。 存儲庫是一個擴展org.springframework.data.repository;CrudRepository的接口org.springframework.data.repository;CrudRepository來自spring數據的org.springframework.data.repository;CrudRepository

@Repository
public interface UserRepository extends CrudRepository<UserEntity, Long> {}

在這個crud存儲庫中是我想要使用的方法,fe findAll()但是當我在tomcat上運行這個項目時,我收到一個錯誤:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.korbeldaniel.cms.server.service.UserService pl.korbeldaniel.cms.server.controller.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.korbeldaniel.cms.server.dao.UserRepository pl.korbeldaniel.cms.server.service.UserService.repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.korbeldaniel.cms.server.dao.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)}

我認為問題與此有關: No qualifying bean of type [pl.korbeldaniel.cms.server.dao.UserRepository] found for dependency 因為我沒有可以注入的存儲庫實現,但對我來說,這是使用spring-data的重點:只是為了創建簡單的接口,如本例所示

這是我的持久性配置:

package pl.korbeldaniel.cms.server.config;

import java.util.Properties;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(basePackages = { "pl.korbeldaniel.cms.server;" })
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
class PersistenceContext {
    @Resource
    private Environment env;

    @Bean(destroyMethod = "close")
    DataSource dataSource(Environment env) {
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
        dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
        dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
        dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
        return new HikariDataSource(dataSourceConfig);
    }
    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("pl.korbeldaniel.cms.server");
        Properties jpaProperties = new Properties();
        //Configures the used database dialect. This allows Hibernate to create SQL
        //that is optimized for the used database.
        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
        //Specifies the action that is invoked to the database when the Hibernate
        //SessionFactory is created or closed.
        jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
        //Configures the naming strategy that is used when Hibernate creates
        //new database objects and schema elements
        jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
        //If the value of this property is true, Hibernate writes all SQL
        //statements to the console.
        jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
        //If the value of this property is true, Hibernate will format the SQL
        //that is written to the console.
        jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }
    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }
}

請幫忙。

@EnableJpaRepositories(basePackages = { "pl.korbeldaniel.cms.server;" })

刪除分號並清理並構建應用程序,

 @EnableJpaRepositories(basePackages = { "pl.korbeldaniel.cms.server"})

錯誤說在給定位置pl.korbeldaniel.cms.server.dao.UserRepository找不到Bean

將豆子移動到上述位置也可以。

暫無
暫無

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

相關問題 未找到依賴項的類型為[PATHTOCLASS]的合格Bean:至少應有1個符合此依賴項自動候選條件的Bean Crudrepository - 沒有為依賴找到符合條件的 bean:預計至少有 1 個 bean 有資格作為這個依賴的自動裝配候選者 未找到依賴項[**。Clients]的合格bean:至少應有1個合格為autowire候選對象的bean。 依賴注釋:{} 沒有找到依賴 [CountryRepository] ​​的合格 bean:預計至少有 1 個 bean 有資格作為自動裝配候選 沒有可用的“ru.spb.repository.UserRepository”類型的合格 bean:預計至少有 1 個 bean 有資格作為自動裝配候選 找不到相依路徑類型為path.repositoryClass的相符bean,至少應有1個符合此相依條件的自動裝配候選 NoSuchBeanDefinitionException: 沒有為依賴找到 [Repository] ​​類型的合格 bean:預計至少有 1 個符合自動裝配條件的 bean 沒有匹配的 bean 類型……為依賴找到……預計至少有 1 個符合自動裝配候選者的 bean 沒有可用的“java.lang.String”類型的合格 bean:預計至少有 1 個有資格作為自動裝配候選者的 bean。 依賴注解: 未找到依賴項:預計至少有 1 個 bean 有資格作為此依賴項的自動裝配候選者。 依賴注解:
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM