JPAConfig
@Configuration
@EnableTransactionManagement
public class JPAConfiguration {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
factoryBean.setDataSource(dataSource());
factoryBean.setJpaProperties(additionalProperties());
factoryBean.setPackagesToScan(new String[] { "br.models" });
return factoryBean;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5431/dbtestesnpc");
dataSource.setUsername("postgres");
dataSource.setPassword("******");
return dataSource;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
return properties;
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf){
return new JpaTransactionManager(emf);
}
}
ServletSpring
public class ServletSpringMVC extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppWebConfiguration.class, JPAConfiguration.class };
}
/**
* mapea o controllers quais outras classes devem ser carregadas pelo
* container
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppWebConfiguration.class, JPAConfiguration.class };
}
/**
* diz qual é o padrão de endereço que vai ser delegado para o Servlet do
* Spring MVC = <url-mapping>
*/
@Override
protected String[] getServletMappings() {
return new String[] {"/index", "/cadastrarTitulo", "/salvarTitulo", "/listarTitulos", "/editarTitulo", "/atualizarTitulo", "/excluirTitulo"};
}
@Override
protected Filter[] getServletFilters(){
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
return new Filter[] {encodingFilter};
}
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement(""));
}
}