Java tutorial
/* * Copyright 2016 Victor Andreenko * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jblogcms.core.config; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.annotation.Resource; import javax.sql.DataSource; import java.util.Properties; /** * @author Victor Andreenko */ @Configuration @EnableJpaRepositories(basePackages = { "org.jblogcms.core.*.repository" }) @ComponentScan(basePackages = { "org.jblogcms.core.*.model", "org.jblogcms.core.*.repository" }) @EnableTransactionManagement @PropertySource("classpath:application.properties") public class PersistenceContext { private static final String[] PACKAGES_TO_SCAN = { "org.jblogcms.core.account.model", "org.jblogcms.core.blog.model", "org.jblogcms.core.comment.model", "org.jblogcms.core.common.model", "org.jblogcms.core.post.model", "org.jblogcms.core.security.model" }; private static final String DATABASE_DRIVER = "database.driver"; private static final String DATABASE_PASSWORD = "database.password"; private static final String DATABASE_URL = "database.url"; private static final String DATABASE_USERNAME = "database.username"; private static final String HIBERNATE_DIALECT = "hibernate.dialect"; private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql"; private static final String HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto"; private static final String HIBERNATE_EJB_NAMING_STRATEGY = "hibernate.ejb.naming_strategy"; private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql"; @Resource private Environment environment; @Bean(destroyMethod = "close") DataSource dataSource() { HikariConfig dataSourceConfig = new HikariConfig(); dataSourceConfig.setDriverClassName(environment.getRequiredProperty(DATABASE_DRIVER)); dataSourceConfig.setJdbcUrl(environment.getRequiredProperty(DATABASE_URL)); dataSourceConfig.setUsername(environment.getRequiredProperty(DATABASE_USERNAME)); dataSourceConfig.setPassword(environment.getRequiredProperty(DATABASE_PASSWORD)); return new HikariDataSource(dataSourceConfig); } @Bean public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setPackagesToScan(PACKAGES_TO_SCAN); Properties jpaProperties = new Properties(); jpaProperties.put(HIBERNATE_DIALECT, environment.getRequiredProperty(HIBERNATE_DIALECT)); jpaProperties.put(HIBERNATE_FORMAT_SQL, environment.getRequiredProperty(HIBERNATE_FORMAT_SQL)); jpaProperties.put(HIBERNATE_HBM2DDL_AUTO, environment.getRequiredProperty(HIBERNATE_HBM2DDL_AUTO)); jpaProperties.put(HIBERNATE_EJB_NAMING_STRATEGY, environment.getRequiredProperty(HIBERNATE_EJB_NAMING_STRATEGY)); jpaProperties.put(HIBERNATE_SHOW_SQL, environment.getRequiredProperty(HIBERNATE_SHOW_SQL)); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; } @Bean public DataSourceInitializer databasePopulator() { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScript( new ClassPathResource("org/springframework/social/connect/jdbc/JdbcUsersConnectionRepository.sql")); populator.setContinueOnError(true); DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDatabasePopulator(populator); initializer.setDataSource(dataSource()); return initializer; } }