I am using Spring and Hibernate and I am trying to "wire" the classes that are necessary in order to Autowire a Repository inside a Service.
The Repository class extends CrudRepository
StopRepository
@Repository
@RepositoryRestResource(collectionResourceRel = "stop", path = "stop")
public interface StopRepository extends CrudRepository<StopJPA, Long> {
StopJPA findById(@Param("id") Long id);
StopJPA findByIdStop(@Param("idStop") String idStop);
@Override
void delete(StopJPA deleted);
@Override
List<StopJPA> findAll();
// Optional<StopJPA> findOne(Long id);
@Override
StopJPA findOne(Long id);
@Override
StopJPA save(StopJPA persisted);
void flush();
}
The Entity class.
StopJPA
@Entity
@Table(name = "stop")
@EntityListeners(RepoListener.class)
public class StopJPA implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "stop_description")
private String stopDescription;
@Column(name = "id_stop", nullable = false)
private String idStop;
public StopJPA() {
}
public StopJPA(String stopDescription, String idStop) {
this.stopDescription = stopDescription;
this.idStop = idStop;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStopDescription() {
return stopDescription;
}
public void setStopDescription(String stopDescription) {
this.stopDescription = stopDescription;
}
public String getIdStop() {
return idStop;
}
public void setIdStop(String idStop) {
this.idStop = idStop;
}
}
And the Service class implementation:
StopService
@Service
final class RepoStopService {
@Service
final class RepoStopService {
private final StopRepository stopRepository;
@Autowired
RepoStopService(StopRepository stopRepository) {
this.stopRepository = stopRepository;
}
}
Unfortunately when i try to run it on server i get this exception:
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repoStopService' defined in file ...\RepoStopService.class:
Unsatisfied dependency expressed through constructor argument with index 0 of type [com.project.app.services.repositories.StopRepository]: : No qualifying bean of type [com.project.app.services.repositories.StopRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {};nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.project.app.services.repositories.StopRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} at .....
Does anyone know why Spring doesnt pick up the @Repository annotation?
My Configuration consists in 3 files. An AppInitializer class that implements WebApplicationInitializer , a WebMvcConfig class that extends WebMvcConfigurerAdapter and lastly a PersistentContext class.
AppInitializer
public class AppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "com.project.app.config";
private static final String MAPPING_URL = "/";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
WebApplicationContext context = getContext();
// Manage the lifecycle of the root application context
servletContext.addListener(new ContextLoaderListener(context));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",
new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(MAPPING_URL);
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
WebMvcConfig
@EnableWebMvc
@Configuration
//@EnableJpaRepositories
@ComponentScan(basePackages = { "com.project.app" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
private Environment env;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("hello");
}
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}
}
PersistentContext
@Component
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class PersistenceContext {
@Autowired
private Environment env;
@Bean
@Primary
public DataSource dataSource() throws ClassNotFoundException {
DataSource ds = new DataSource();
String url = env.getProperty(SystemSettings.AMTAB_DS_URL);
String user = env.getProperty(SystemSettings.AMTAB_DS_USERNAME);
String pass = env.getProperty(SystemSettings.AMTAB_DS_PASSWORD);
String driver = env.getProperty(SystemSettings.AMTAB_DS_DRIVER);
// ds.setDriverClassName("org.postgresql.Driver");
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(pass);
return ds;
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("com.project.app.services.entities");
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(SystemSettings.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(SystemSettings.HIBERNATE_HBM2DDL));
// Configures the naming strategy that is used when Hibernate creates
// new database objects and schema elements
// jpaProperties.put("hibernate.ejb.naming_strategy",
// env.getRequiredProperty(SystemSettings.HIBERNATE_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(SystemSettings.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(SystemSettings.HIBERNATE_FORMAT_SQL));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
/**
* Because we are using JPA, we have to create a transaction manager bean that integrates the
* JPA provider with the Spring transaction mechanism. We can do this by using the
* JpaTransactionManager class as the transaction manager of our application.
*
* We can configure the transaction manager bean by following these steps:
*
* -> Create a new JpaTransactionManager object. -> Configure the entity manager factory whose
* transactions are managed by the created JpaTransactionManager object.
**/
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
SOLUTION
I just needed to specify also the package of the repositories, because its a location that wont be searched by default --> @EnableJpaRepositories("com.project.app.services.repositories")