Hi,
I am trying to do lazy init my database configuration so that I can override default configuration at runtime. I have tried different combinations using Lazy and Prototype scopes, but with out any success.
Here is my JPA configuration:
Here is my test:Code:@Configuration @AnnotationDrivenConfig @AnnotationDrivenTx @ComponentScan(value = "test.dynaconfig") public abstract class JpaConfig extends ConfigurationSupport { public @Bean(scope=DefaultScopes.PROTOTYPE) DataSource dataSource() { ComboPooledDataSource dataSource = null; try { dataSource = new ComboPooledDataSource(); DefaultConfig config = defaultConfig(); dataSource.setDriverClass(config.getDriverClass()); dataSource.setJdbcUrl(config.getUrl()); dataSource.setUser(config.getUsername()); dataSource.setPassword(config.getPassword()); } catch (Exception e) { } return dataSource; } public @Bean DefaultConfig defaultConfig() { DefaultConfig config = new DefaultConfig(); config.setDriverClass("org.hsqldb.jdbcDriver"); config.setUrl("jdbc:hsqldb:hsql://localhost:9001"); config.setUsername("sa"); config.setPassword(""); return config; } public @Bean DialectConfig dialectConfig() { DialectConfig dialectConfig = new DialectConfig(); dialectConfig.setDialect("org.hibernate.dialect.HSQLDialect"); return dialectConfig; } public @Bean Properties jpaProperties() { Properties jpaProperties = new Properties(); jpaProperties.put("hibernate.dialect", dialectConfig().getDialect()); jpaProperties.put("hibernate.hbm2ddl.auto", "update"); return jpaProperties; } public @Bean EntityManagerFactory entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setJpaProperties(jpaProperties()); em.setDataSource(dataSource()); em.setJpaVendorAdapter(jpaVendorAdapter()); em.setLoadTimeWeaver(loadTimeWeaver()); return this.getObject(em, EntityManagerFactory.class); } abstract @AutoBean HibernateJpaVendorAdapter jpaVendorAdapter(); abstract @AutoBean InstrumentationLoadTimeWeaver loadTimeWeaver(); public @Bean PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } }
DefaultConfig and DialectConfig classes are just java beans.Code:public class TestDynaConfig { @Test public void testDefaultConfig() throws Exception { JavaConfigApplicationContext context = new JavaConfigApplicationContext(JpaConfig.class); DataSource dataSource = context.getBean(DataSource.class); assertEquals("HSQL Database Engine", dataSource.getConnection().getMetaData().getDatabaseProductName()); DefaultConfig config = context.getBean(DefaultConfig.class); config.setDriverClass("com.mysql.jdbc.Driver"); config.setUrl("jdbc:mysql://localhost:3306/foo"); config.setUsername("root"); config.setPassword("mysql"); DialectConfig dialect = context.getBean(DialectConfig.class); dialect.setDialect("org.hibernate.dialect.MySQLInnoDBDialect"); dataSource = context.getBean(DataSource.class); assertEquals("MySQL", dataSource.getConnection().getMetaData().getDatabaseProductName());//SUCCESS Dao dao = context.getBean(Dao.class); Foo foo = new Foo(); foo.setName("foo bar"); long id = dao.insertFoo(foo); assertEquals("foo bar", dao.getFoo(id).getName()); } }
This test runs fine, but it always uses HSQL database for persistence. Interestingly, the test succeeds when it tries to assert the database vendor and indeed the datasource is MySQL at that point. But, it always inserts into HSQL table.
Appreciate any inputs. I may be doing something stupid here.
Thanks!
Arul


