Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Spring 3.1 Configurable Class :)
package com.bemobi.wap.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.ejb.HibernatePersistence;
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.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jndi.support.SimpleJndiBeanFactory;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:wapb.properties")
@ComponentScan("com.bemobi.wap")
public class SpringConfig {
private final Logger logger = LoggerFactory.getLogger(SpringConfig.class);
@Autowired
Environment env;
@Bean
public EntityManagerFactory entityManagerFactory() {
logger.debug("Configurando entityManagerFactory()");
final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(datasource());
entityManagerFactory.setJpaProperties(getHibernateProperties());
entityManagerFactory.setPackagesToScan(new String[] { env.getProperty("spring.package.toscan") });
entityManagerFactory.setPersistenceProvider(new HibernatePersistence());
entityManagerFactory.afterPropertiesSet();
logger.debug("entityManagerFactory configurado com sucesso!");
return entityManagerFactory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
logger.debug("Configurando transactionManager()");
final JpaTransactionManager transactionManager = new JpaTransactionManager(entityManagerFactory());
transactionManager.setDataSource(datasource());
transactionManager.setJpaDialect(new HibernateJpaDialect());
logger.debug("transactionManager configurado com sucesso!");
return transactionManager;
}
@Bean
public DataSource datasource() {
logger.debug("Configurando datasource()");
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClass"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.username"));
dataSource.setPassword(env.getProperty("jdbc.password"));
logger.debug("datasource configurado com sucesso!");
return dataSource;
}
@Bean
public SimpleJndiBeanFactory JndiBeanFactory() {
logger.debug("Configurando JndiBeanFactory()");
final SimpleJndiBeanFactory jndiFactory = new SimpleJndiBeanFactory();
final Properties jndiProperties = getRemoteEJBProperties();
jndiFactory.setJndiEnvironment(jndiProperties);
logger.debug("JndiBeanFactory configurado com sucesso, usando as propriedades: " + jndiProperties);
return jndiFactory;
}
private Properties getRemoteEJBProperties() {
final Properties jndiProperties = new Properties();
jndiProperties.put("core.java.naming.provider.url", env.getProperty("core.java.naming.provider.url"));
jndiProperties.put("jboss.java.naming.factory.initial", env.getProperty("jboss.java.naming.factory.initial"));
jndiProperties.put("jboss.java.naming.factory.url.pkgs", env.getProperty("jboss.java.naming.factory.url.pkgs"));
return jndiProperties;
}
private Properties getHibernateProperties() {
final Properties prop = new Properties();
prop.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
prop.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
prop.setProperty("hibernate.use_outer_join", env.getProperty("hibernate.use_outer_join"));
prop.setProperty("hibernate.max_fetch_depth", env.getProperty("hibernate.max_fetch_depth"));
prop.setProperty("hibernate.jdbc.batch_size", env.getProperty("hibernate.jdbc.batch_size"));
prop.setProperty("hibernate.transaction.flush_before_completion", env.getProperty("hibernate.transaction.flush_before_completion"));
prop.setProperty("hibernate.connection.release_mode", env.getProperty("hibernate.connection.release_mode"));
return prop;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.