没有为依赖项找到UserRepository类型的限定bean:预期至少有1个bean符合此依赖项的autowire候选者

时间:2016-08-18 10:10:21

标签: java spring hibernate spring-data-jpa

嘿,我从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的接口:

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

请帮忙。

1 个答案:

答案 0 :(得分:3)

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

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

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

错误表示在指定位置找不到Bean pl.korbeldaniel.cms.server.dao.UserRepository

将豆子移动到上述位置也可以。