@Test public void testCreateAndDestroyNested() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/jdbc/config/jdbc-destroy-nested-config.xml"); try { DataSource dataSource = context.getBean(DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); assertEquals(1, template.queryForInt("select count(*) from T_TEST")); context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); expected.expect(BadSqlGrammarException.class); // Table has been dropped assertEquals(1, template.queryForInt("select count(*) from T_TEST")); } finally { context.close(); } }
@Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class); setDatabaseType(element, builder); DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder); useIdAsDatabaseNameIfGiven(element, builder); builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); return builder.getBeanDefinition(); }
@Override protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class); setGenerateUniqueDatabaseNameFlag(element, builder); setDatabaseName(element, builder); setDatabaseType(element, builder); DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder); builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); return builder.getBeanDefinition(); }
@Test public void createAndDestroyNestedWithHsql() throws Exception { ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config.xml"); try { DataSource dataSource = context.getBean(DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); assertNumRowsInTestTable(template, 1); context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); expected.expect(BadSqlGrammarException.class); // Table has been dropped assertNumRowsInTestTable(template, 1); } finally { context.close(); } }
@Test public void createAndDestroyNestedWithH2() throws Exception { ClassPathXmlApplicationContext context = context("jdbc-destroy-nested-config-h2.xml"); try { DataSource dataSource = context.getBean(DataSource.class); JdbcTemplate template = new JdbcTemplate(dataSource); assertNumRowsInTestTable(template, 1); context.getBean(EmbeddedDatabaseFactoryBean.class).destroy(); expected.expect(BadSqlGrammarException.class); // Table has been dropped assertNumRowsInTestTable(template, 1); } finally { context.close(); } }
@Bean public DataSource dataSource() { EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean(); ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.addScript(new ClassPathResource("kr/debop4j/data/hibernate/config/java/schema.sql")); bean.setDatabasePopulator(databasePopulator); bean.afterPropertiesSet(); // EmbeddedDatabaseFactoryBean가 FactoryBean이므로 필요합니다. return bean.getObject(); }
@Bean public DataSource dataSource() { ClassPathResource classPathResource = new ClassPathResource("/crm-schema-h2.sql"); ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(); resourceDatabasePopulator.addScript(classPathResource); EmbeddedDatabaseFactoryBean embeddedDatabaseFactoryBean = new EmbeddedDatabaseFactoryBean(); embeddedDatabaseFactoryBean.setDatabasePopulator(resourceDatabasePopulator); embeddedDatabaseFactoryBean.setDatabaseName("crm"); embeddedDatabaseFactoryBean.setDatabaseType(EmbeddedDatabaseType.H2); embeddedDatabaseFactoryBean.afterPropertiesSet(); return embeddedDatabaseFactoryBean.getObject(); }
@Bean public DataSource dataSource() { EmbeddedDatabaseFactoryBean databaseFactoryBean = new EmbeddedDatabaseFactoryBean(); databaseFactoryBean.setDatabaseType(EmbeddedDatabaseType.H2); databaseFactoryBean.afterPropertiesSet(); return databaseFactoryBean.getObject(); }