Java tutorial
/** * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. * * Copyright (C) 2014 Academic Medical Center of the University of Amsterdam */ package nl.amc.ebioscience.ecat; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.Database; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * This class configures the Spring context with the annotations * * @author s.shahand@amc.uva.nl */ @Configuration @EnableJpaRepositories @EnableTransactionManagement @PropertySources(value = { @PropertySource("classpath:jdbc.properties"), @PropertySource("classpath:hibernate.properties") }) public class EcatConfig { @Autowired private Environment env; @Bean public DataSource dataSource() { // instantiate a connection pool org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource(); // configure database connection ds.setDriverClassName(env.getRequiredProperty("jdbc.driverClassName")); ds.setUrl(env.getRequiredProperty("jdbc.url")); ds.setUsername(env.getRequiredProperty("jdbc.username")); ds.setPassword(env.getRequiredProperty("jdbc.password")); return ds; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setDataSource(dataSource); lef.setJpaVendorAdapter(jpaVendorAdapter); lef.setPackagesToScan("nl.amc.ebioscience.ecat.model"); return lef; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(Boolean.parseBoolean(env.getRequiredProperty("hibernate.showSql"))); hibernateJpaVendorAdapter .setGenerateDdl(Boolean.parseBoolean(env.getRequiredProperty("hibernate.generateDdl"))); hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); return hibernateJpaVendorAdapter; } @Bean public PlatformTransactionManager transactionManager() { return new JpaTransactionManager(); } }