fi.luontola.cqrshotel.Application.java Source code

Java tutorial

Introduction

Here is the source code for fi.luontola.cqrshotel.Application.java

Source

// Copyright  2016-2017 Esko Luontola
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package fi.luontola.cqrshotel;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import fi.luontola.cqrshotel.framework.EventStore;
import fi.luontola.cqrshotel.framework.PsqlEventStore;
import fi.luontola.cqrshotel.pricing.PricingEngine;
import fi.luontola.cqrshotel.pricing.RandomPricingEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.time.Clock;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public EventStore eventStore(DataSource dataSource, ObjectMapper objectMapper) {
        return new PsqlEventStore(dataSource, objectMapper);
    }

    @Bean
    public PricingEngine pricingEngine(Clock clock) {
        return new RandomPricingEngine(clock);
    }

    @Bean
    public Clock clock() {
        return Clock.systemDefaultZone();
    }

    @Bean
    public ObjectMapper jacksonObjectMapper() {
        ObjectMapper om = new ObjectMapper();
        om.registerModules(new JavaTimeModule(), new MoneyModule());
        om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        om.configure(SerializationFeature.INDENT_OUTPUT, true);
        om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        om.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
        return om;
    }
}