Announcement Announcement Module
Collapse
No announcement yet.
BeanCurrentlyInCreationException without circular dependency Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanCurrentlyInCreationException without circular dependency

    I am writing a fairly standard web applications using Spring MVC. When starting it, Spring raises an exception, claiming that there is a circular dependency. For the life of me, I can find the circular dependency.


    The architecture

    There's a class, UserController that provides web pages, that relies on a service UserService that provides access to domain objects, that relies on a repository UserRepository that uses Hibernate for persistance. The repository relies on a configuration class InfrastructureContextConfiguration that provides transaction services. Most of the architecture and code is inspired by the "Pro Spring MVC: With Web Flow" book.

    As far as I can tell, there are no circular dependencies. Yet, Spring claims that there's one and crashes.

    Upon startup Spring fails with the following exception:

    Code:
    BeanCreationException: Error creating bean with name 'userController':
            Injection of autowired dependencies failed; nested exception is
    
    BeanCreationException: Could not autowire field: private app.service.UserService
            app.web.UserController.userService; nested exception is
    
    BeanCreationException: Error creating bean with name 'userServiceImpl':
            Injection of autowired dependencies failed; nested exception is
    
    BeanCreationException: Could not autowire field: app.repository.UserRepository
            app.service.UserServiceImpl.userRepository; nested exception is
    
    BeanCreationException: Error creating bean with name 'userRepository':
            Injection of persistence dependencies failed; nested exception is
    
    BeanCreationException: Error creating bean with name 'infrastructureContextConfiguration':
            Injection of autowired dependencies failed; nested exception is
    
    BeanCreationException: Could not autowire field: private javax.persistence.EntityManagerFactory
            app.web.config.InfrastructureContextConfiguration.entityManagerFactory; nested exception is
    
    BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory':
            Requested bean is currently in creation: Is there an unresolvable circular reference?
    As far as I can tell there are no circular dependencies, yet Spring refuses to start. How can I fix this?
    The code

    Here's a trimmed down version of my code:

    First, the web-facing controller:

    Code:
    package app.controller;
    
    @Controller
    public class UserController {
    
        @Autowired
        private UserService userService;
    
        /* web code */
    }
    The controller needs this service:

    Code:
    package app.service;
    
    @Service
    @Transactional(readOnly = true)
    public class UserServiceImpl implements UserService {
    
        @Autowired
        UserRepository userRepository;
    
        /* code */
    }
    The service needs this repository:

    Code:
    package app.repository;
    
    @Repository("userRepository")
    public class JpaUserRepository implements UserRepository {
    
        @PersistenceContext
        private EntityManager entityManager;
    
    }
    Finally, this Configuration class provides some low level details:

    Code:
    package app.web.config;
    
    @Configuration
    @EnableTransactionManagement
    @ComponentScan(basePackages = { "app.service", "app.repository", "app.domain" })
    public class InfrastructureContextConfiguration {
    
        @Autowired
        private DataSource dataSource;
    
        @Autowired
        private EntityManagerFactory entityManagerFactory;
    
        @Bean
        public JpaVendorAdapter jpaVendorAdapter() {
            return new HibernateJpaVendorAdapter();
        }
    
        @Bean
        public FactoryBean<EntityManagerFactory> entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
            emfb.setDataSource(dataSource);
            emfb.setJpaVendorAdapter(jpaVendorAdapter());
            return emfb;
        }
    
        @Bean
        public PlatformTransactionManager transactionManager() {
            JpaTransactionManager txManager = new JpaTransactionManager();
            txManager.setEntityManagerFactory(entityManagerFactory);
            txManager.setDataSource(dataSource);
            return txManager;
        }
    
        @Bean
        public DataSource dataSource() {
    
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            builder.setType(EmbeddedDatabaseType.H2);
            return builder.build();
        }
    }
    The root MVC class is:

    Code:
    package app.web.config;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { "app.web" })
    public class WebMvcContextConfiguration extends WebMvcConfigurerAdapter {
        /* other stuff that shouldn't matter */
    }
    All these classes have only the default constructor that takes no argument, so I don understand how there can be a circular dependecy.

    I'm unable to see where the circular dependency is. How can I resolve this problem and or deep deeper to debug it?

    I am using Spring 3.2.3.RELEASE running on Tomcat 7.0.41 and Oracle Java 1.7.0_21. I tried older Spring releases (back to 3.2.1.RELEASE and Tomcat 7.0.35) without success.
Working...
X