Java tutorial
/* * Copyright 2012 the original author or authors. * * 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 com.github.carlomicieli.config; import java.util.Arrays; import javax.sql.DataSource; import org.hibernate.SessionFactory; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.ImprovedNamingStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; import org.springframework.orm.hibernate4.HibernateTransactionManager; import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.github.carlomicieli.service.hibernate.SessionEventInterceptor; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * The application configuration class. * * @author Carlo P. Micieli * */ @Configuration @EnableTransactionManagement @Import({ ApplicationDevConfig.class, ApplicationTestConfig.class }) @ComponentScan(basePackages = "com.github.carlomicieli") public class ApplicationConfig { private @Autowired Environment env; private static final Logger log = LoggerFactory.getLogger(ApplicationConfig.class); /** * Return the data source bean. * @return the data source bean. */ public @Bean DataSource dataSource() { log.info(Arrays.asList(env.getActiveProfiles()).toString()); ComboPooledDataSource ds = new ComboPooledDataSource(); try { ds.setDriverClass(env.getProperty(AvailableSettings.DRIVER)); } catch (Exception e) { throw new RuntimeException(e); } ds.setJdbcUrl(env.getProperty(AvailableSettings.URL)); ds.setUser(env.getProperty(AvailableSettings.USER)); ds.setPassword(env.getProperty(AvailableSettings.PASS)); return ds; } /** * Return the Hibernate session factory bean. * @return the Hibernate session factory bean. * @throws PropertyVetoException */ public @Bean(destroyMethod = "close") SessionFactory sessionFactory() { LocalSessionFactoryBuilder fb = new LocalSessionFactoryBuilder(dataSource()); // set the naming convention that adds underscores in place of uppercase letters // in mixed-case table and column names fb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE); // scan these packages for entity classes fb.scanPackages("com.github.carlomicieli.model"); // set the session events interceptor fb.setInterceptor(new SessionEventInterceptor()); return fb.buildSessionFactory(); } /** * Return the Hibernate transaction manager bean. * @return the Hibernate transaction manager bean. */ public @Bean HibernateTransactionManager transactionManager() { HibernateTransactionManager txm = new HibernateTransactionManager(); txm.setSessionFactory(sessionFactory()); return txm; } }