Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2015 Mrcio M. Noda * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package br.com.devmedia.cleancode.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.Resource; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; @EnableTransactionManagement(proxyTargetClass = true) @Configuration class ConfiguracaoBancoDados { @Autowired private EntityManagerFactory entityManagerFactory; @Value("/massa-de-dados/clientes.sql") private Resource scriptCliente; @Value("/massa-de-dados/numero-pedidos.sql") private Resource scriptNumerosPedidos; @Value("/massa-de-dados/produtos.sql") private Resource scriptProdutos; @Value("/massa-de-dados/pedidos.sql") private Resource scriptPedidos; @Value("/massa-de-dados/itens-pedidos.sql") private Resource scriptItensPedidos; @Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; } private DatabasePopulator databasePopulator() { final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScript(scriptCliente); populator.addScript(scriptNumerosPedidos); populator.addScript(scriptProdutos); populator.addScript(scriptPedidos); populator.addScript(scriptItensPedidos); return populator; } @Bean public PlatformTransactionManager transactionManager() { return new JpaTransactionManager(entityManagerFactory); } }