Skip to content

Instantly share code, notes, and snippets.

@geine

geine/AppConf Secret

Created June 18, 2013 00:32
Show Gist options
  • Save geine/3044f9ad2f95051421c8 to your computer and use it in GitHub Desktop.
Save geine/3044f9ad2f95051421c8 to your computer and use it in GitHub Desktop.
AppConf
package com.blogspot.sharingebook.spring.zk;
import org.springframework.beans.factory.annotation.Value;
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.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.web.PageableArgumentResolver;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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 org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter;
import javax.sql.DataSource;
import java.util.List;
import java.util.Properties;
/**
* Created with IntelliJ IDEA.
* User: altaire
* Date: 6/10/13
* Time: 9:25 AM
* To change this template use File | Settings | File Templates.
*/
@Configuration
@EnableWebMvc
@EnableJpaRepositories("com.blogspot.sharingebook.spring.zk")
@ComponentScan(basePackages = {"com.blogspot.sharingebook.spring.zk"})
@Import(ServletConf.class)
@EnableTransactionManagement
public class AppConf extends WebMvcConfigurerAdapter {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${hibernate.dialect}")
private String dialect;
@Value("${hibernate.show_sql}")
private String showSql;
@Value("${hibernate.format_sql}")
private String formatSql;
@Value("${hibernate.hbm2ddl.auto}")
private String hbm2ddlauto;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableArgumentResolver resolver = new PageableArgumentResolver();
resolver.setFallbackPagable(new PageRequest(1, 10));
argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(1000000);
return multipartResolver;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
@Bean
public Properties getJpaProperties(){
Properties prop = new Properties();
prop.put("hibernate.dialect", dialect);
prop.put("hibernate.show_sql", showSql);
prop.put("hibernate.format_sql", formatSql);
prop.put("hibernate.hbm2ddl.auto", hbm2ddlauto);
return prop;
}
@Bean
public JpaTransactionManager transactionManager(){
JpaTransactionManager tm = new JpaTransactionManager();
tm.setEntityManagerFactory(entityManagerFactory().getObject());
return tm;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter() ;
vendorAdapter.setGenerateDdl(true);
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.blogspot.sharingebook.spring.zk");
factory.setJpaProperties(getJpaProperties());
factory.afterPropertiesSet();
return factory;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment