Announcement Announcement Module
No announcement yet.
Strange behavior for @ComponentScan, SessionFactory in 3.1.0RC1 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strange behavior for @ComponentScan, SessionFactory in 3.1.0RC1

    I'll switch from XML to Java based configuration. I've always migrate my SpringMVC config into a @EnableWebMvc annotated class without -much- problems. Now I want to migrate the main application context configuration containing all the services, DAO's, database settings etc.
    First, my test class for the new java based configuration:
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes=AppConfig.class)
    public class AppContextTest extends AbstractJUnit4SpringContextTests
        public void testDAOWiring()
            CustomerDAO dao = applicationContext.getBean(CustomerDAO.class);
    And here the actual java based configuration class:
    @ComponentScan(basePackages = "webapp.customer", excludeFilters = {@ComponentScan.Filter(Configuration.class), @ComponentScan.Filter(Controller.class)})
    public class AppConfig
        public SessionFactory sessionFactory() throws Exception
            AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
            bean.setPackagesToScan(new String[] {"webapp"});
            return bean.getObject();
        private Properties hibernateProps()
            Properties jpaProperties = new Properties();
            jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            jpaProperties.put("", "create-drop");
            jpaProperties.put("hibernate.show_sql", false);
            jpaProperties.put("hibernate.format_sql", true);
            return jpaProperties;
        @Value("${db.driverClass}") private String driverClass;
        @Value("${db.jdbcUrl}") private String jdbcUrl;
        @Value("${db.user}") private String user;
        @Value("${db.password}") private String password;    
        public DataSource dataSource()
            return new DriverManagerDataSource(driverClass, jdbcUrl, user, password);
    I exclude this configuration class itself and any SpringMVC @Controller class from component scan. The import-resources contains the JDBC property file definition.
    With this implementation, the test failed with
    'sessionFactory' or 'hibernateTemplate' is required
    This is strange, due to the existing @Bean annotated method sessionFactory(). If DEBUG loglevel is enabled, I saw that there was a singleton created for the sessionFactory. Another strange fact was, that I can't debug this method as long as @ComponentScan was enabled for the class. Any breakpoint within the method wasn't hit. After disabling @ComponentScan, my breakpoint was hit, the method executed and the sessionFactory properly created. Also all the Hibernate stuff (connect to database, create DB schema etc.) happened. But -of course- my test for CustomerDAO failed due to the missing component scan, the @Repository annotated class CustomerDAOHibernateImpl wasn't created !

    It seems, that here under Spring 3.1.0.RC1 the combination of @ComponentScan and Hibernate/sessionFactory configuration based on JavaConfig don't work properly. I also try the example from Spring 3.1 reference " Java based bean metadata". This was without @ComponentScan ... and it works as expected.

    Has anybody any idea, what went wrong with my implementation mentioned here ?
    Thanks in advance