= Configuration

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


Spring provides numerous mechanisms to configure domain services, both in terms of binding or passing in the configuration property to the service, and in terms of setting the value within some sort of configuration file.

The mechanism prefered by Apache Causeway itself, and which you are free to use for your own services, is the type-safe link:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/properties/ConfigurationProperties.html[ConfigurationProperties], whereby the configuration properties are expressed in a series of nested static classes.

The xref:docs:starters:simpleapp.adoc[simpleapp starter app] includes an example:

[source,java]
----
import org.springframework.validation.annotation.Validated;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("app.simple-module")
@lombok.Data
@Validated
public static class Configuration {
    private final Types types = new Types();
    @lombok.Data
    public static class Types {
        private final Name name = new Name();
        @lombok.Data
        public static class Name {
            private final Validation validation = new Validation();
            @lombok.Data
            public static class Validation {
                private char[] prohibitedCharacters =
                    "!&%$".toCharArray();
                private String message =
                    "Character '{character}' is not allowed";
            }
        }
    }
}
----

This configuration property can be injected, like any other component, and makes the configuration value available in a type-safe fashion:

[source,java]
----
val prohibitedCharacters =
  config.getTypes().getName().getValidation().getProhibitedCharacters();
----

For this configuration property service to be discovered and managed by Spring, we need to use the link:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/properties/EnableConfigurationProperties.html[EnableConfigurationProperties] annotation.
This normally would reside on the owning module (discussed in more detail xref:modules.adoc[later]):

[source,java]
.SimpleModule.java
----
import org.springframework.context.annotation.Configuration

@Configuration
// ...
@EnableConfigurationProperties({
        SimpleModule.Configuration.class,
})
public class SimpleModule /* ... */ {
    // ...
}
----


These configuration properties can then be specified using either Spring's `application.yml` or `application.properties`.
For example:

[source,yml]
.application.yml
----
app:
  simple-module:
    types:
      name:
        validation:
          message: "'{character}' is invalid."
          prohibited-characters: "&%$"
----

Moreover, Spring is able to configure the IDE so that these configuration values can be specified using code completion.
All that is required is this dependency:

[source,xml]
.pom.xml
----
<!-- IDE support  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
----
