|
| 1 | +package org.woehlke.beachbox.config; |
| 2 | + |
| 3 | +import org.springframework.context.annotation.*; |
| 4 | +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; |
| 5 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 6 | +import org.springframework.data.web.PageableArgumentResolver; |
| 7 | +import org.springframework.jdbc.datasource.DriverManagerDataSource; |
| 8 | +import org.springframework.orm.jpa.JpaDialect; |
| 9 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 10 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 11 | +import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor; |
| 12 | +import org.springframework.orm.jpa.vendor.HibernateJpaDialect; |
| 13 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 14 | +import org.springframework.transaction.PlatformTransactionManager; |
| 15 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 16 | +import org.springframework.validation.Validator; |
| 17 | +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| 18 | +import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
| 19 | +import org.springframework.web.servlet.ViewResolver; |
| 20 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 21 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 22 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
| 23 | +import org.springframework.web.servlet.mvc.method.annotation.ServletWebArgumentResolverAdapter; |
| 24 | +import org.springframework.web.servlet.view.InternalResourceViewResolver; |
| 25 | +import org.springframework.web.servlet.view.JstlView; |
| 26 | + |
| 27 | +import javax.persistence.EntityManagerFactory; |
| 28 | +import javax.sql.DataSource; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Properties; |
| 31 | + |
| 32 | +/** |
| 33 | + * Created by tw on 22.04.14. |
| 34 | + */ |
| 35 | +@Configuration |
| 36 | +@ComponentScan("org.woehlke.beachbox") |
| 37 | +@EnableWebMvc |
| 38 | +@EnableTransactionManagement |
| 39 | +@EnableJpaRepositories(basePackages="org.woehlke.beachbox") |
| 40 | +public class WebConfig extends WebMvcConfigurerAdapter { |
| 41 | + |
| 42 | + @Bean |
| 43 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory(){ |
| 44 | + LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); |
| 45 | + bean.setDataSource(dataSource()); |
| 46 | + bean.setPersistenceUnitName("beachboxPU"); |
| 47 | + HibernateJpaVendorAdapter jva = new HibernateJpaVendorAdapter(); |
| 48 | + jva.setShowSql(true); |
| 49 | + jva.setGenerateDdl(true); |
| 50 | + jva.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect"); |
| 51 | + bean.setJpaVendorAdapter(jva); |
| 52 | + Properties jpaProperties = new Properties(); |
| 53 | + jpaProperties.setProperty("hibernate.hbm2ddl.auto","update"); |
| 54 | + jpaProperties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect"); |
| 55 | + jpaProperties.setProperty("hibernate.show_sql","false"); |
| 56 | + jpaProperties.setProperty("hibernate.generate_statistics","true"); |
| 57 | + jpaProperties.setProperty("jpa.showSql","false"); |
| 58 | + jpaProperties.setProperty("jpa.database","MYSQL"); |
| 59 | + jpaProperties.setProperty("hibernate.connection.useUnicode","true"); |
| 60 | + jpaProperties.setProperty("hibernate.connection.characterEncoding","UTF-8"); |
| 61 | + bean.setJpaProperties(jpaProperties); |
| 62 | + return bean; |
| 63 | + } |
| 64 | + |
| 65 | + @Bean |
| 66 | + public DataSource dataSource(){ |
| 67 | + DriverManagerDataSource ds = new DriverManagerDataSource(); |
| 68 | + ds.setDriverClassName("com.mysql.jdbc.Driver"); |
| 69 | + ds.setUrl("jdbc:mysql://localhost:3306/beachbox?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8"); |
| 70 | + ds.setUsername("beachbox"); |
| 71 | + ds.setPassword("beachboxpwd"); |
| 72 | + return ds; |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + public JpaDialect jpaDialect(){ |
| 77 | + return new HibernateJpaDialect(); |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + public PlatformTransactionManager transactionManager() |
| 82 | + { |
| 83 | + EntityManagerFactory factory = entityManagerFactory().getObject(); |
| 84 | + JpaTransactionManager tx = new JpaTransactionManager(); |
| 85 | + tx.setEntityManagerFactory(factory); |
| 86 | + tx.setJpaDialect(jpaDialect()); |
| 87 | + return tx; |
| 88 | + } |
| 89 | + |
| 90 | + @Bean |
| 91 | + public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor(){ |
| 92 | + return new PersistenceAnnotationBeanPostProcessor(); |
| 93 | + } |
| 94 | + |
| 95 | + @Bean |
| 96 | + public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){ |
| 97 | + return new PersistenceExceptionTranslationPostProcessor(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Bean |
| 102 | + public ViewResolver resolver(){ |
| 103 | + InternalResourceViewResolver url = new InternalResourceViewResolver(); |
| 104 | + url.setViewClass(JstlView.class); |
| 105 | + url.setPrefix("/WEB-INF/views/"); |
| 106 | + url.setSuffix(".jsp"); |
| 107 | + return url; |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + public PageableArgumentResolver pageableArgumentResolver(){ |
| 112 | + return new PageableArgumentResolver(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void addResourceHandlers(ResourceHandlerRegistry registry){ |
| 117 | + registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/"); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public Validator getValidator() { |
| 122 | + return new LocalValidatorFactoryBean(); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + @Override |
| 127 | + public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { |
| 128 | + argumentResolvers.add(resolvers()); |
| 129 | + } |
| 130 | + |
| 131 | + @Bean |
| 132 | + public ServletWebArgumentResolverAdapter resolvers() { |
| 133 | + return new ServletWebArgumentResolverAdapter(pageable()); |
| 134 | + } |
| 135 | + |
| 136 | + @Bean |
| 137 | + public PageableArgumentResolver pageable() { |
| 138 | + return new PageableArgumentResolver(); |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | +} |
0 commit comments