Skip to content

Instantly share code, notes, and snippets.

@jdmr
Last active December 13, 2015 21:58
Show Gist options
  • Save jdmr/4980854 to your computer and use it in GitHub Desktop.
Save jdmr/4980854 to your computer and use it in GitHub Desktop.
Spring 3.2 Configuration Files
package org.davidmendoza.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan(basePackages = "org.davidmendoza.demo")
@PropertySource("file:${user.home}/.demo.properties")
public class ComponentConfig {
}
package org.davidmendoza.demo.config;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
public class DataConfig {
private static final Logger log = LoggerFactory.getLogger(DataConfig.class);
@Autowired
private SessionFactory sessionFactory;
@Bean
public PlatformTransactionManager transactionManager() {
return new HibernateTransactionManager(sessionFactory);
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Configuration
@Profile("production")
@Import(PropertyPlaceholderConfig.class)
static class Production {
@Value("${hibernate.dialect}")
protected String hibernateDialect;
@Value("${hibernate.show_sql}")
protected String hibernateShowSql;
@Value("${hibernate.hbm2ddl.auto}")
protected String hibernateHbm2DDL;
@Value("${hibernate.cache.use_second_level_cache}")
protected String hibernateSecondLevelCache;
@Value("${hibernate.cache.provider_class}")
protected String hibernateCacheClass;
@Value("${hibernate.default_schema}")
protected String hibernateSchema;
@Value("${jdbc.driverClassName}")
protected String jdbcDriver;
@Value("${jdbc.username}")
protected String jdbcUsername;
@Value("${jdbc.password}")
protected String jdbcPassword;
@Value("${jdbc.url}")
protected String jdbcUrl;
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBean factoryBean;
try {
factoryBean = new LocalSessionFactoryBean();
Properties pp = new Properties();
pp.setProperty("hibernate.dialect", hibernateDialect);
pp.setProperty("hibernate.show_sql", hibernateShowSql);
pp.setProperty("hibernate.hbm2ddl.auto", hibernateHbm2DDL);
pp.setProperty("hibernate.cache.use_second_level_cache", hibernateSecondLevelCache);
pp.setProperty("hibernate.cache.provider_class", hibernateCacheClass);
pp.setProperty("hibernate.default_schema", hibernateSchema);
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan("org.davidmendoza.demo.model");
factoryBean.setHibernateProperties(pp);
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
} catch (Exception e) {
log.error("Couldn't configure the sessionFactory bean", e);
}
throw new RuntimeException("Couldn't configure the sessionFactory bean");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(jdbcDriver);
ds.setUsername(jdbcUsername);
ds.setPassword(jdbcPassword);
ds.setUrl(jdbcUrl);
return ds;
}
}
@Configuration
@Profile("tests")
@Import(PropertyPlaceholderConfig.class)
static class Tests {
@Value("${test.hibernate.dialect}")
protected String testHibernateDialect;
@Value("${test.hibernate.show_sql}")
protected String testHibernateShowSql;
@Value("${test.hibernate.hbm2ddl.auto}")
protected String testHibernateHbm2DDL;
@Value("${test.hibernate.cache.use_second_level_cache}")
protected String testHibernateSecondLevelCache;
@Value("${test.hibernate.cache.provider_class}")
protected String testHibernateCacheClass;
@Value("${test.hibernate.default_schema}")
protected String testHibernateSchema;
@Value("${test.jdbc.driverClassName}")
protected String testJdbcDriver;
@Value("${test.jdbc.username}")
protected String testJdbcUsername;
@Value("${test.jdbc.password}")
protected String testJdbcPassword;
@Value("${test.jdbc.url}")
protected String testJdbcUrl;
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBean factoryBean;
try {
factoryBean = new LocalSessionFactoryBean();
Properties pp = new Properties();
pp.setProperty("hibernate.dialect", testHibernateDialect);
pp.setProperty("hibernate.show_sql", testHibernateShowSql);
pp.setProperty("hibernate.hbm2ddl.auto", testHibernateHbm2DDL);
pp.setProperty("hibernate.cache.use_second_level_cache", testHibernateSecondLevelCache);
pp.setProperty("hibernate.cache.provider_class", testHibernateCacheClass);
pp.setProperty("hibernate.default_schema", testHibernateSchema);
factoryBean.setDataSource(dataSource());
factoryBean.setPackagesToScan("edu.swau.forms.model");
factoryBean.setHibernateProperties(pp);
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
} catch (Exception e) {
log.error("Couldn't configure the sessionFactory bean", e);
}
throw new RuntimeException("Couldn't configure the sessionFactory bean");
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(testJdbcDriver);
ds.setUsername(testJdbcUsername);
ds.setPassword(testJdbcPassword);
ds.setUrl(testJdbcUrl);
return ds;
}
}
}
package org.davidmendoza.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
@PropertySource("file:${user.home}/.demo.properties")
public class PropertyPlaceholderConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
package org.davidmendoza.demo.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import java.util.List;
import java.util.Locale;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
/**
* Messages to support internationalization/localization.
*/
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
// additional webmvc-related beans
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setCache(false);
resolver.setViewClass(JstlView.class);
return resolver;
}
/**
* Supports FileUploads.
*/
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(500000);
return multipartResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
return localeChangeInterceptor;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en_US"));
return localeResolver;
}
// implementing WebMvcConfigurer
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/img/**").addResourceLocations("/img/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/crossdomain.xml").addResourceLocations("/crossdomain.xml");
registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico");
registry.addResourceHandler("/robots.txt").addResourceLocations("/robots.txt");
registry.addResourceHandler("/humans.txt").addResourceLocations("/humans.txt");
registry.addResourceHandler("/404.html").addResourceLocations("/404.html");
registry.addResourceHandler("/405.html").addResourceLocations("/405.html");
registry.addResourceHandler("/apple-touch-icon-114x114-precomposed.png").addResourceLocations("/apple-touch-icon-114x114-precomposed.png");
registry.addResourceHandler("/apple-touch-icon-144x144-precomposed.png").addResourceLocations("/apple-touch-icon-144x144-precomposed.png");
registry.addResourceHandler("/apple-touch-icon-57x57-precomposed.png").addResourceLocations("/apple-touch-icon-57x57-precomposed.png");
registry.addResourceHandler("/apple-touch-icon-72x72-precomposed.png").addResourceLocations("/apple-touch-icon-72x72-precomposed.png");
registry.addResourceHandler("/apple-touch-icon-precomposed.png").addResourceLocations("/apple-touch-icon-precomposed.png");
registry.addResourceHandler("/apple-touch-icon.png").addResourceLocations("/apple-touch-icon.png");
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJacksonHttpMessageConverter());
}
@Override
public Validator getValidator() {
LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
factory.setValidationMessageSource(messageSource);
return factory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment