Java tutorial
/* * * Springstrap * * @author Jan Philipp Knller <info@pksoftware.de> * * Homepage: http://ui5strap.com/springstrap * * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de> * * 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. * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt * */ package de.pksoftware.springstrap.core.config; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.domain.AuditorAware; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; import org.springframework.instrument.classloading.LoadTimeWeaver; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; import de.pksoftware.springstrap.core.domain.BasicAuditorBean; import de.pksoftware.springstrap.core.domain.IAccount; @Configuration @EnableTransactionManagement(proxyTargetClass = true) @EnableJpaAuditing(auditorAwareRef = "auditorBean") @EnableJpaRepositories({ "de.pksoftware.springstrap.core.repository", "de.pksoftware.springstrap.cms.repository" }) //@EnableLoadTimeWeaving public abstract class RdbmsConfigBase { protected List<String> packagesToScan = new ArrayList<String>(); @Value("${rdbms.eclipselink.logging.level:}") protected String eclipselinkLoggingLevel; @Value("${rdbms.eclipselink.weaving:}") protected String eclipselinkWeaving; @Value("${rdbms.eclipselink.ddl-generation:}") protected String eclipselinkDdlGeneration; /** * Add a entity package name to the list. * @param packageName */ protected void addEntityPackage(String packageName) { packagesToScan.add(packageName); } /** * Build the list of entity packages. */ protected void addEntityPackages() { addEntityPackage("de.pksoftware.springstrap.core.entity"); addEntityPackage("de.pksoftware.springstrap.cms.entity"); } /** * Create the Auditor Bean. * TODO Currently the Bean requires a BasicSecurityService. This is not optimal. * @return */ @Bean protected AuditorAware<IAccount> auditorBean() { return new BasicAuditorBean(); } /** * Creates the EntityManagerFactory instance. * @return */ @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { addEntityPackages(); LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setLoadTimeWeaver(loadTimeWeaver()); entityManagerFactoryBean.setDataSource(dataSource()); entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter()); entityManagerFactoryBean.setPackagesToScan(packagesToScan.toArray(new String[packagesToScan.size()])); entityManagerFactoryBean.setJpaProperties(jpaProperties()); // entityManagerFactoryBean.afterPropertiesSet(); return entityManagerFactoryBean; } /** * Creates and exposes the JpaTransactionManager Bean. * @return */ @Bean public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } /** * Creates the JpaVendorAdapter instance. */ protected JpaVendorAdapter vendorAdapter() { EclipseLinkJpaVendorAdapter eclipseLink = new EclipseLinkJpaVendorAdapter(); return eclipseLink; } /** * Creates the JPA Properties. * @return */ protected Properties jpaProperties() { Properties properties = new Properties(); if (null != eclipselinkLoggingLevel && !eclipselinkLoggingLevel.isEmpty()) { properties.put("eclipselink.logging.level", eclipselinkLoggingLevel); } else { properties.put("eclipselink.logging.level", SpringstrapConfiguration.DEFAULT_RDBMS_ECLIPSELINK_LOGGING_LEVEL); } if (null != eclipselinkWeaving && !eclipselinkWeaving.isEmpty()) { properties.put("eclipselink.weaving", eclipselinkWeaving); } else { properties.put("eclipselink.weaving", SpringstrapConfiguration.DEFAULT_RDBMS_ECLIPSELINK_WEAVING); } if (null != eclipselinkDdlGeneration && !eclipselinkDdlGeneration.isEmpty()) { properties.put("eclipselink.ddl-generation", eclipselinkDdlGeneration); } else { properties.put("eclipselink.ddl-generation", SpringstrapConfiguration.DEFAULT_RDBMS_ECLIPSELINK_DDL_GENERATION); } return properties; } /** * Creates the LoadTimeWeaver. * @return */ protected LoadTimeWeaver loadTimeWeaver() { //return new SimpleLoadTimeWeaver(); return new InstrumentationLoadTimeWeaver(); } protected abstract DataSource dataSource(); }