Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.exet.brother.TechTask;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- 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.view.JstlView;
- import org.springframework.web.servlet.view.UrlBasedViewResolver;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- @PropertySource("classpath:config.properties")
- @EnableTransactionManagement
- @EnableWebMvc
- public class AppConfig extends WebMvcConfigurerAdapter {
- @Value("${hibernate.dialect}")
- private String sqlDialect;
- @Value("${hbm2ddl.auto}")
- private String hbm2dllAuto;
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
- return new JpaTransactionManager(emf);
- }
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory
- (DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
- Properties jpaProp = new Properties();
- jpaProp.put("hibernate.hbm2ddl.auto", hbm2dllAuto);
- LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactory.setDataSource(dataSource);
- entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
- entityManagerFactory.setPackagesToScan("com.gmail.exet.brother.TechTask");
- entityManagerFactory.setJpaProperties(jpaProp);
- return entityManagerFactory;
- }
- @Bean
- public JpaVendorAdapter jpaVendorAdapter() {
- HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
- adapter.setDatabasePlatform(sqlDialect);
- return adapter;
- }
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource ds = new DriverManagerDataSource();
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- ds.setUrl("jdbc:mysql://localhost:3306/urltester");
- ds.setUsername("root");
- ds.setPassword("password");
- return ds;
- }
- @Bean
- public UrlBasedViewResolver setupViewResolver() {
- UrlBasedViewResolver resolver = new UrlBasedViewResolver();
- resolver.setPrefix("/WEB-INF/");
- resolver.setSuffix(".jsp");
- resolver.setViewClass(JstlView.class);
- resolver.setOrder(1);
- return resolver;
- }
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry
- .addResourceHandler("/resources/**")
- .addResourceLocations("/resources/");
- }
- }
Add Comment
Please, Sign In to add comment