Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package first.test;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
- import org.springframework.web.servlet.LocaleContextResolver;
- import org.springframework.web.servlet.ViewResolver;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- import org.springframework.web.servlet.i18n.SessionLocaleResolver;
- import org.springframework.web.servlet.view.InternalResourceViewResolver;
- import org.springframework.web.servlet.view.JstlView;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import javax.validation.Validator;
- import java.util.Locale;
- import java.util.Properties;
- @Configuration
- @EnableWebMvc
- @ComponentScan(basePackages = "first.test")
- @EnableTransactionManagement
- public class WebConfig implements WebMvcConfigurer {
- @Bean
- public ViewResolver internalResourceViewResolver() {
- InternalResourceViewResolver bean = new InternalResourceViewResolver();
- bean.setViewClass(JstlView.class);
- bean.setPrefix("/WEB-INF/templates/");
- bean.setSuffix(".jsp");
- return bean;
- }
- @Bean(name="localeResolver")
- public LocaleContextResolver getLocaleContextResolver() {
- SessionLocaleResolver localeResolver = new SessionLocaleResolver();
- localeResolver.setDefaultLocale(new Locale("pl","PL"));
- return localeResolver;
- }
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
- em.setDataSource(dataSource());
- em.setPackagesToScan(new String[] {"first.test"});
- em.setJpaDialect(new HibernateJpaDialect());
- em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- em.setJpaProperties(additionalProperties());
- return em;
- }
- Properties additionalProperties() {
- Properties properties = new Properties();
- properties.setProperty("hibernate.hbm2ddl.auto", "drop-and-create");
- properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
- properties.setProperty("hibernate.show_sql", "true");
- properties.setProperty("hibernate.format_sql", "true");
- return properties;
- }
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(emf);
- return transactionManager;
- }
- @Bean
- public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
- return new PersistenceExceptionTranslationPostProcessor();
- }
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/testdb?useSSL=false");
- dataSource.setUsername("root");
- dataSource.setPassword("tajne123");
- return dataSource;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement