服务为何找不到我的存储库bean?

时间:2019-01-20 00:09:19

标签: spring-boot service repository javabeans

我正在制作一个简单的spring boot应用程序,由于某种原因,我的服务似乎没有注意到我的存储库bean。我检查了配置,这似乎还不错,我基本上也只是从教程中复制了将存储库注入的方式,所以我什至不知道问题可能出在哪里。

我的存储库:

@Repository
public interface ProjectRepository extends CrudRepository<Trucks, Integer> 
{

@Query("....")
List<Trucks> getTrucks();
}

我的服务:

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {


private ProjectRepository projectRepository;

@Autowired
public ProjectServiceImpl(ProjectRepository projectRepository) {
    this.projectRepository = projectRepository;
}

@Override
public List<Trucks> getTrucks() {
    return projectRepository.getTrucks();
}
}

配置:

@Configuration
@EnableJpaRepositories(basePackages = {
    "com.javar.domain"
})
@PropertySource("classpath:application.properties")
@EnableTransactionManagement
public class PersistanceContext {

private static final String[] ENTITY_PACKAGES = {
        "com.javar.domain"
};
private static final String PROPERTY_NAME_DB_DRIVER_CLASS 
="spring.datasource.driver-class-name";
private String PROPERTY_NAME_DB_URL="app.datasource.url";
private String PROPERTY_NAME_DB_USER="app.datasource.username";
private String PROPERTY_NAME_DB_PASSWORD="app.datasource.password";
private String 
PROPERTY_NAME_HIBERNATE_DIALECT="spring.jpa.properties.hibernate.dialect";
private String 
PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO="spring.jpa.hibernate.ddl-auto";
private String 
PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY="spring.jpa.hibernate.naming- 
strategy";
private String PROPERTY_NAME_HIBERNATE_SHOW_SQL="spring.jpa.show-sql";
private String 

PROPERTY_NAME_HIBERNATE_FORMAT_SQL=
"spring.jpa.properties.hibernate.format_sql";

@Bean(destroyMethod = "close")
DataSource dataSource(Environment env) {
    HikariConfig dataSourceConfig = new HikariConfig();

dataSourceConfig.setDriverClassName(env.getRequiredProperty
(PROPERTY_NAME_DB_DRIVER_CLASS));
    dataSourceConfig.setJdbcUrl(env.getRequiredPropert
(PROPERTY_NAME_DB_URL));
    dataSourceConfig.setUsername(env.getRequiredProperty
(PROPERTY_NAME_DB_USER));
    dataSourceConfig.setPassword(env.getRequiredProperty
(PROPERTY_NAME_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(ENTITY_PACKAGES);

    Properties jpaProperties = new Properties();

    //Configures the used database dialect. This allows Hibernate to create SQL
    //that is optimized for the used database.
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));

    //Specifies the action that is invoked to the database when the Hibernate
    //SessionFactory is created or closed.
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));

    //Configures the naming strategy that is used when Hibernate creates
    //new database objects and schema elements
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));

    //If the value of this property is true, Hibernate writes all SQL
    //statements to the console.
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));

    //If the value of this property is true, Hibernate will use prettyprint
    //when it writes SQL to the console.
    jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));

    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
}

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

Application.properties:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
app.datasource.url=jdbc:mysql://localhost:3306/zavrsni?useSSL=false
app.datasource.username=....
app.datasource.password=....

#Hibernate Configuration
spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.naming-strategy = 
org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

错误报告:

Description:

Parameter 0 of constructor in com.javar.serviceImpl.ProjectServiceImpl 
required a bean of type 'com.javar.repositoryy.ProjectRepository' that 
could not be found.

Action:

Consider defining a bean of type 'com.javar.repositoryy.ProjectRepository' 
in your configuration.

}

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

@Autowired
private ProjectRepository projectRepository;

并使用该注释删除该构造函数:

@Autowired
public ProjectServiceImpl(ProjectRepository projectRepository) {
    this.projectRepository = projectRepository;
}

如果这对您有帮助或您有任何疑问,请稍后再编辑此答案。

已更新

确保在找到存储库的位置软件包正确无误。

 @EnableJpaRepositories(basePackages = {"com.javar.domain"})

因此将包更新为“ com.javar.repositories”

答案 1 :(得分:0)

终于找到了答案,所以我只需要在Application类之上添加@EnableJpaRepositories(basePackages =“ com.javar.repositoryy”)。希望这对以后的人有所帮助。