Created
March 18, 2015 18:36
-
-
Save LetItRock/c2446b059d68da16c293 to your computer and use it in GitHub Desktop.
Spring + Postgresql + JPA + OPENSHIFT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@ComponentScan(basePackages = "com.shekhar.notebook") | |
@EnableJpaRepositories(basePackageClasses = NotebookRepository.class) | |
@EnableTransactionManagement | |
public class ApplicationConfig { | |
@Bean | |
public LocalContainerEntityManagerFactoryBean entityManagerFactory() { | |
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); | |
vendorAdapter.setDatabase(Database.POSTGRESQL); | |
vendorAdapter.setGenerateDdl(true); | |
vendorAdapter.setShowSql(true); | |
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); | |
factory.setJpaVendorAdapter(vendorAdapter); | |
factory.setPackagesToScan(Notebook.class.getPackage().getName()); | |
factory.setDataSource(dataSource()); | |
return factory; | |
} | |
@Bean(destroyMethod = "close") | |
public DataSource dataSource() { | |
BasicDataSource dataSource = new BasicDataSource(); | |
dataSource.setDriverClassName("org.postgresql.Driver"); | |
dataSource.setUrl("jdbc:postgresql://" + System.getenv("OPENSHIFT_POSTGRESQL_DB_HOST") + ":" | |
+ System.getenv("OPENSHIFT_POSTGRESQL_DB_PORT") + "/"+System.getenv("OPENSHIFT_APP_NAME")); | |
dataSource.setUsername(System.getenv("OPENSHIFT_POSTGRESQL_DB_USERNAME")); | |
dataSource.setPassword(System.getenv("OPENSHIFT_POSTGRESQL_DB_PASSWORD")); | |
dataSource.setTestOnBorrow(true); | |
dataSource.setTestOnReturn(true); | |
dataSource.setTestWhileIdle(true); | |
dataSource.setTimeBetweenEvictionRunsMillis(1800000L); | |
dataSource.setNumTestsPerEvictionRun(3); | |
dataSource.setMinEvictableIdleTimeMillis(1800000L); | |
dataSource.setValidationQuery("SELECT 1"); | |
return dataSource; | |
} | |
@Bean | |
public JpaDialect jpaDialect() { | |
return new HibernateJpaDialect(); | |
} | |
@Bean | |
public PlatformTransactionManager transactionManager() { | |
JpaTransactionManager txManager = new JpaTransactionManager(); | |
txManager.setEntityManagerFactory(entityManagerFactory().getObject()); | |
return txManager; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-rest-webmvc</artifactId> | |
<version>1.1.0.BUILD-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-rest-repository</artifactId> | |
<version>1.1.0.BUILD-SNAPSHOT</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.data</groupId> | |
<artifactId>spring-data-rest-core</artifactId> | |
<version>1.1.0.BUILD-SNAPSHOT</version> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class RestExporterWebInitializer implements WebApplicationInitializer { | |
@Override | |
public void onStartup(ServletContext servletContext) | |
throws ServletException { | |
AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext(); | |
rootCtx.register(ApplicationConfig.class); | |
servletContext.addListener(new ContextLoaderListener(rootCtx)); | |
AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext(); | |
webCtx.register(RepositoryRestMvcConfiguration.class); | |
DispatcherServlet dispatcherServlet = new DispatcherServlet(webCtx); | |
ServletRegistration.Dynamic reg = servletContext.addServlet( | |
"rest-exporter", dispatcherServlet); | |
reg.setLoadOnStartup(1); | |
reg.addMapping("/*"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment