【问题标题】:No qualifying bean of type UserRepository found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency没有找到用于依赖的 UserRepository 类型的合格 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者
【发布时间】:2016-08-18 10:10:20
【问题描述】:

嘿,我在从 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 层对话。 Repository 是从 spring 数据中扩展 org.springframework.data.repository;CrudRepository 的接口:

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

在这个 crud 存储库中是我想使用的方法,f.e 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 的重点:只是为了创建简单的接口,如example

这是我的持久化配置:

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;
    }
}

请帮忙。

【问题讨论】:

  • 我怀疑是不是这个问题,但是@Repository不是用于spring数据接口repositories的,其实有@NoRepositoryBean是为了防止接口被当成bean使用。

标签: java spring hibernate spring-data-jpa


【解决方案1】:
@EnableJpaRepositories(basePackages = { "pl.korbeldaniel.cms.server;" })

删除分号并清理并构建您的应用程序,

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

错误说在给定位置找不到 Bean pl.korbeldaniel.cms.server.dao.UserRepository

将您的 bean 移动到上述位置也可以。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2017-10-24
    • 2020-04-02
    • 1970-01-01
    相关资源
    最近更新 更多