Trying to use the Spring Data JPA to generate the DAO objects automatically using the <repositories /> with the base-package linking the package that contains the DAO interfaces like:
public interface UserDAO extends JpaRepository<User, String> {
}
but it fails wiring the DAO objects in the service beans, the exact error is:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ACLService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pkg.service.UserServ pkg.service.ACLServ.userServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServ': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pkg.repositories.UserDAO pkg.service.UserServ.userDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [pkg.repositories.UserDAO] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at pkg.config.WebAppInit.onStartup(WebAppInit.java:35)
The application bootstrap start at WebAppInit.Java since it implements the WebApplicationInitializer interface, the web.xml code is:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"></web-app>
The WebAppInit.Java code the onStartup method as:
public class WebAppInit implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context =
new AnnotationConfigWebApplicationContext();
context.register(ApplicationContextConfig.class);
context.refresh();
...
Then the ApplicationContextConfig class is annotated with @Configuration, code is:
@Configuration
@PropertySource("classpath:application.properties")
@ImportResource("classpath:*springDataConfig.xml")
@Import({BasicDataSourceConfig.class,PersistenceSpringDataJpaConfig.class})
@ComponentScan(basePackages={"pkg.service","pkg.utils"})
public class ApplicationContextConfig {
}
so this is only the main/entry point for the Java configuration, then it follow application.properties (Not included but just ask for it), then the springDataConfig.xml with code:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<repositories base-package="pkg.repositories" />
</beans:beans>
The BasicDataSourceConfig.Java configure the DataSource @Bean as:
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
...
The PersistenceSpringDataJpaConfig.Java configure the LocalContainerEntityManagerFactoryBean as:
@Configuration
public class PersistenceSpringDataJpaConfig {
...
@Autowired
DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
...
The other files are not directly related, if I cut off the dependency removing/commenting the code:
@Autorire
private UserDAO userDAO
in the UserService class; the application runs without error, I mean besides the null pointer exception when accessing the dao object in the service bean.
So the question is: Why the Spring Data JPA does not create the userDAO bean?
PS: I did deliberately get rid from all @Transactions management to simplify it, besides it should work without transaction, isn't it?