From dd1df85e603668aa14091eb914929d982b75002f Mon Sep 17 00:00:00 2001 From: Antoine Rey <antoine.rey@gmail.com> Date: Tue, 25 Oct 2016 19:02:31 +0200 Subject: [PATCH] Adding typesafe custom configuration --- springboot-petclinic-server/pom.xml | 6 +++ .../petclinic/PetClinicApplication.java | 3 ++ .../petclinic/config/PetclinicProperties.java | 42 +++++++++++++++++++ .../src/main/resources/application.properties | 8 ++-- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java diff --git a/springboot-petclinic-server/pom.xml b/springboot-petclinic-server/pom.xml index 3466edf5..f48fd7b7 100644 --- a/springboot-petclinic-server/pom.xml +++ b/springboot-petclinic-server/pom.xml @@ -49,6 +49,12 @@ <optional>true</optional> </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-configuration-processor</artifactId> + <optional>true</optional> + </dependency> + <dependency> <groupId>org.springframework.samples</groupId> <artifactId>springboot-petclinic-client</artifactId> diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java index 21d6e053..c9b8e697 100644 --- a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java @@ -2,7 +2,9 @@ package org.springframework.samples.petclinic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.samples.petclinic.config.PetclinicProperties; import org.springframework.ui.ModelMap; import org.springframework.web.context.request.WebRequest; import org.springframework.web.context.request.WebRequestInterceptor; @@ -10,6 +12,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @SpringBootApplication +@EnableConfigurationProperties(PetclinicProperties.class) public class PetClinicApplication { public static void main(String[] args) { diff --git a/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java new file mode 100644 index 00000000..137529aa --- /dev/null +++ b/springboot-petclinic-server/src/main/java/org/springframework/samples/petclinic/config/PetclinicProperties.java @@ -0,0 +1,42 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed 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. + */ +package org.springframework.samples.petclinic.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Typesafe custom configuration. + * <p> + * Offers contextual help and "code completion" as users are working with application.properties. + * + * @author Antoine Rey + */ +@ConfigurationProperties(prefix = "petclinic") +public class PetclinicProperties { + + /** + * Relational database supported by SpringBoot Petclinic: hsqldb, mysql or postgresql + */ + private String database; + + public String getDatabase() { + return database; + } + + public void setDatabase(String database) { + this.database = database; + } +} diff --git a/springboot-petclinic-server/src/main/resources/application.properties b/springboot-petclinic-server/src/main/resources/application.properties index e99b56c5..97914865 100644 --- a/springboot-petclinic-server/src/main/resources/application.properties +++ b/springboot-petclinic-server/src/main/resources/application.properties @@ -1,7 +1,7 @@ # database init, supports mysql too -database=hsqldb -spring.datasource.schema=classpath*:db/${database}/schema.sql -spring.datasource.data=classpath*:db/${database}/data.sql +petclinic.database=hsqldb +spring.datasource.schema=classpath*:db/${petclinic.database}/schema.sql +spring.datasource.data=classpath*:db/${petclinic.database}/data.sql # JPA spring.jpa.hibernate.ddl-auto=none @@ -17,4 +17,4 @@ logging.level.org.springframework=INFO server.compression.enabled=true server.compression.mime-types=application/json,text/css,application/javascript -server.compression.min-response-size=2048 \ No newline at end of file +server.compression.min-response-size=2048 -- GitLab