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

  • Strange behavior for @ComponentScan, SessionFactory in 3.1.0RC1

    Hi,
    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:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class, classes=AppConfig.class)
    public class AppContextTest extends AbstractJUnit4SpringContextTests
    {
        @Test
        public void testDAOWiring()
        {
            CustomerDAO dao = applicationContext.getBean(CustomerDAO.class);
            assertNotNull(dao);
        }
    }
    And here the actual java based configuration class:
    Code:
    @Configuration
    @ComponentScan(basePackages = "webapp.customer", excludeFilters = {@ComponentScan.Filter(Configuration.class), @ComponentScan.Filter(Controller.class)})
    @ImportResource({"classpath*:properties-config.xml"})
    public class AppConfig
    {
        @Bean
        public SessionFactory sessionFactory() throws Exception
        {
            AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
            bean.setDataSource(dataSource());
            bean.setPackagesToScan(new String[] {"webapp"});
            bean.setHibernateProperties(hibernateProps());
            bean.afterPropertiesSet();
            return bean.getObject();
        }
    
        private Properties hibernateProps()
        {
            Properties jpaProperties = new Properties();
            jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
            jpaProperties.put("hibernate.hbm2ddl.auto", "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;    
    
        @Bean
        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
    Code:
    '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 "2.5.3.1 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
    Dominik
Working...
X