Prerequisites:
Java 11 Maven 3.x Spring Framework Hyperon Studio with H2 database (default for bundle distribution) We will show how to configure Hyperon Engine using Spring annotated configuration (http://docs.spring.io) .
Maven configuration Apart from standard spring boot dependencies, you need to include hyperon-runtime dependency, available in Maven Central.
<dependency>
<groupId> pl.decerto</groupId>
<artifactId> hyperon-runtime</artifactId>
<version> ${hyperon-runtime.version}</version>
</dependency>
Another needed dependency is the JDBC driver to the database of choice, e.g., h2, oracle, mssql, postgresql and database connection pool managing library:
<dependency>
<groupId> com.h2database</groupId>
<artifactId> h2</artifactId>
<version> ${h2-database.version}</version>
</dependency>
<dependency>
<groupId> org.apache.commons</groupId>
<artifactId> commons-dbcp2</artifactId>
<version> ${commons-dbcp2.version}</version>
</dependency>
Properties Add Hyperon Runtime data source properties to application.properties file.
hyperon.database.url=<jdbc_url>
hyperon.database.username=<username>
hyperon.database.password=<password>
Properties for versions before 2.0.0 are available here
Spring configuration Add required beans to your java class annotated with @Configuration:
private Environment env;
private static final Logger log = LoggerFactory.getLogger(TestConfiguration.class);
public DialectRegistry getDialectRegistry () {
DialectRegistry registry = new DialectRegistry();
registry.setDialect(env.getProperty("hyperon.database.dialect" ));
return registry;
}
public DialectTemplate getDialectTemplate (DialectRegistry dialectRegistry) {
return dialectRegistry.create();
}
(destroyMethod = "close" )
public DataSource getDataSource (DialectTemplate dialectTemplate) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUsername(env.getProperty("hyperon.database.username" ));
dataSource.setPassword(env.getProperty("hyperon.database.password" ));
dataSource.setUrl(env.getProperty("hyperon.database.url" ));
dataSource.setInitialSize(4);
dataSource.setDriverClassName(dialectTemplate.getJdbcDriverClassName());
return dataSource;
}
(destroyMethod = "destroy" )
public HyperonEngineFactory getHyperonEngineFactory (DataSource dataSource) {
log.info("Engine factory begin creation..." );
HyperonEngineFactory hyperonEngineFactory = new HyperonEngineFactory();
hyperonEngineFactory.setDataSource(dataSource);
return hyperonEngineFactory;
}
public HyperonEngine getHyperonEngine (HyperonEngineFactory hyperonEngineFactory) {
log.info("Engine begin creation..." );
return hyperonEngineFactory.create();
}
The application is ready to start. After successfully starting the application, you can see similar log output: