Я делаю простое приложение с весенней загрузкой, и мой сервис почему-то не замечает мой 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.
}