Search code examples
spring-bootservicerepositoryjavabeans

Why can't Service find my Repository bean?


I'm making a simple spring boot application and my service doesn't seem to notice my repository bean for some reason. I checked my configuration and it seems fine, I also basically just copied way that the repository is injected from a tutorial so i really don't even have an idea where might the problem be.

My Repository:

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

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

My service:

@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:

@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

And the error report:

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.

}


Solution

  • You can try this:

    @Autowired
    private ProjectRepository projectRepository;
    

    and remove this contructor with that annotation:

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

    If this helps you or you have some questions, let me edit this answer later.

    UPDATED

    Make sure that the package is correct where repository is found.

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

    So update the package to "com.javar.repositories"