Skip to content
Snippets Groups Projects
Commit 98f88546 authored by Maciej Szarlinski's avatar Maciej Szarlinski
Browse files

Strongly typed vets-service properties

parent 66de0d44
Branches
No related tags found
No related merge requests found
Showing
with 63 additions and 89 deletions
......@@ -40,6 +40,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
......
......@@ -2,10 +2,13 @@ package org.springframework.samples.petclinic.vets;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.samples.petclinic.vets.infrastructure.config.VetsProperties;
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigurationProperties(VetsProperties.class)
public class VetsServiceApplication {
public static void main(String[] args) {
......
......@@ -7,7 +7,7 @@ import org.ehcache.config.units.EntryUnit;
import org.ehcache.expiry.Duration;
import org.ehcache.expiry.Expirations;
import org.ehcache.jsr107.Eh107Configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.JCacheManagerCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
......@@ -22,11 +22,8 @@ import java.util.concurrent.TimeUnit;
@EnableCaching
public class CacheConfig {
@Value("${vets.cache.ttl}")
private int cacheTtl;
@Value("${vets.cache.heap-size}")
private int cacheHeapSize;
@Autowired
VetsProperties vetsProperties;
@Bean
public JCacheManagerCustomizer cacheManagerCustomizer() {
......@@ -34,8 +31,8 @@ public class CacheConfig {
CacheConfiguration<Object, Object> config = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Object.class, Object.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(cacheHeapSize, EntryUnit.ENTRIES))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(cacheTtl, TimeUnit.SECONDS)))
.heap(vetsProperties.getCache().getHeapSize(), EntryUnit.ENTRIES))
.withExpiry(Expirations.timeToLiveExpiration(Duration.of(vetsProperties.getCache().getTtl(), TimeUnit.SECONDS)))
.build();
cacheManager.createCache("vets", Eh107Configuration.fromEhcacheCacheConfiguration(config));
};
......
package org.springframework.samples.petclinic.vets.infrastructure.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Typesafe custom configuration.
*
* @author mszarlinski on 2016-11-08.
*/
//TODO: Lombok
@ConfigurationProperties(prefix = "vets")
public class VetsProperties {
private Cache cache;
public static class Cache {
private int ttl;
private int heapSize;
public int getTtl() {
return ttl;
}
public void setTtl(int ttl) {
this.ttl = ttl;
}
public int getHeapSize() {
return heapSize;
}
public void setHeapSize(int heapSize) {
this.heapSize = heapSize;
}
}
public Cache getCache() {
return cache;
}
public void setCache(Cache cache) {
this.cache = cache;
}
}
......@@ -8,3 +8,7 @@ spring.jpa.hibernate.ddl-auto: none
logging.level.org.springframework: INFO
vets:
cache:
ttl: 10
heap-size: 10
/*
* 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.infrastructure.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;
}
}
package org.springframework.samples.petclinic.infrastructure.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
}
/*
* Copyright 2002-2013 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.support.web;
import org.springframework.web.bind.annotation.CrossOrigin;
/**
* @author Antoine Rey
*/
@CrossOrigin
public abstract class AbstractResourceController {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment