Polish samples

This commit is contained in:
Stephane Nicoll 2016-12-30 17:57:14 +01:00
parent a19a28062f
commit c903ff46a7
30 changed files with 149 additions and 105 deletions

View File

@ -27,6 +27,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -16,14 +16,16 @@
package sample.actuator.noweb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Autowired
private ServiceProperties configuration;
private final ServiceProperties configuration;
public HelloWorldService(ServiceProperties configuration) {
this.configuration = configuration;
}
public String getHelloMessage() {
return "Hello " + this.configuration.getName();

View File

@ -18,8 +18,10 @@ package sample.actuator.noweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(ServiceProperties.class)
public class SampleActuatorNoWebApplication {
public static void main(String[] args) throws Exception {

View File

@ -17,12 +17,13 @@
package sample.actuator.noweb;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
@Component
public class ServiceProperties {
/**
* Name of the service.
*/
private String name = "World";
public String getName() {

View File

@ -1 +1 @@
health.diskspace.enabled: false
health.diskspace.enabled=false

View File

@ -45,14 +45,16 @@ dependencies {
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-security")
compile("org.springframework.boot:spring-boot-starter-web")
compile("com.h2database:h2")
runtime("com.h2database:h2")
compileOnly('org.springframework.boot:spring-boot-configuration-processor')
testCompile("org.springframework.boot:spring-boot-starter-test")
insecure configurations.runtime
}
// Slightly odd requirement (package a jar file as an insecure app, exlcuding Spring Security)
// Slightly odd requirement (package a jar file as an insecure app, excluding Spring Security)
// just to demonstrate the "customConfiguration" feature of the Boot gradle plugin.
springBoot {
customConfiguration = "insecure"

View File

@ -39,10 +39,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -16,14 +16,16 @@
package sample.actuator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Autowired
private ServiceProperties configuration;
private final ServiceProperties configuration;
public HelloWorldService(ServiceProperties configuration) {
this.configuration = configuration;
}
public String getHelloMessage() {
return "Hello " + this.configuration.getName();

View File

@ -20,17 +20,25 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SampleActuatorApplication implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("hello", "world").build();
}
@EnableConfigurationProperties(ServiceProperties.class)
public class SampleActuatorApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleActuatorApplication.class, args);
}
@Bean
public HealthIndicator helloHealthIndicator() {
return new HealthIndicator() {
@Override
public Health health() {
return Health.up().withDetail("hello", "world").build();
}
};
}
}

View File

@ -23,7 +23,6 @@ import java.util.Map;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
@ -36,8 +35,11 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Description("A controller for handling requests for hello messages")
public class SampleController {
@Autowired
private HelloWorldService helloWorldService;
private final HelloWorldService helloWorldService;
public SampleController(HelloWorldService helloWorldService) {
this.helloWorldService = helloWorldService;
}
@GetMapping("/")
@ResponseBody

View File

@ -17,12 +17,13 @@
package sample.actuator;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
@Component
public class ServiceProperties {
/**
* Name of the service.
*/
private String name = "World";
public String getName() {

View File

@ -16,24 +16,26 @@
package sample.flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SampleFlywayApplication implements CommandLineRunner {
@Autowired
private PersonRepository repository;
@Override
public void run(String... args) throws Exception {
System.err.println(this.repository.findAll());
}
public class SampleFlywayApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleFlywayApplication.class, args);
}
@Bean
public CommandLineRunner runner(final PersonRepository repository) {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
System.err.println(repository.findAll());
}
};
}
}

View File

@ -16,19 +16,16 @@
package sample.hypermedia.jpa;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -37,19 +34,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
@AutoConfigureMockMvc
public class SampleHypermediaJpaApplicationIntegrationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void links() throws Exception {
this.mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON))

View File

@ -27,6 +27,13 @@
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -1,2 +1,2 @@
logging.level.org.springframework.integration.file: DEBUG
service.greeting: Hello
logging.level.org.springframework.integration.file=DEBUG
service.greeting=Hello

View File

@ -31,6 +31,13 @@
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -17,12 +17,13 @@
package sample.metrics.dropwizard;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
public class HelloWorldService {
public class HelloWorldProperties {
/**
* Name of the service.
*/
private String name = "World";
public String getName() {
@ -33,8 +34,4 @@ public class HelloWorldService {
this.name = name;
}
public String getHelloMessage() {
return "Hello " + this.name;
}
}

View File

@ -19,9 +19,6 @@ package sample.metrics.dropwizard;
import java.util.Collections;
import java.util.Map;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
@ -32,32 +29,21 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Description("A controller for handling requests for hello messages")
public class SampleController {
@Autowired
private HelloWorldService helloWorldService;
private final HelloWorldProperties helloWorldProperties;
@Autowired
private GaugeService gauges;
private final GaugeService gauges;
public SampleController(HelloWorldProperties helloWorldProperties, GaugeService gauges) {
this.helloWorldProperties = helloWorldProperties;
this.gauges = gauges;
}
@GetMapping("/")
@ResponseBody
public Map<String, String> hello() {
this.gauges.submit("timer.test.value", Math.random() * 1000 + 1000);
return Collections.singletonMap("message",
this.helloWorldService.getHelloMessage());
}
protected static class Message {
@NotBlank(message = "Message value cannot be empty")
private String value;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
return Collections.singletonMap("message", "Hello " +
this.helloWorldProperties.getName());
}
}

View File

@ -18,8 +18,10 @@ package sample.metrics.dropwizard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(HelloWorldProperties.class)
public class SampleDropwizardMetricsApplication {
public static void main(String[] args) throws Exception {

View File

@ -27,6 +27,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -17,11 +17,9 @@
package sample.metrics.opentsdb;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
public class HelloWorldService {
public class HelloWorldProperties {
private String name = "World";
@ -33,8 +31,4 @@ public class HelloWorldService {
this.name = name;
}
public String getHelloMessage() {
return "Hello " + this.name;
}
}

View File

@ -21,7 +21,6 @@ import java.util.Map;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@ -31,14 +30,17 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Description("A controller for handling requests for hello messages")
public class SampleController {
@Autowired
private HelloWorldService helloWorldService;
private final HelloWorldProperties helloWorldProperties;
public SampleController(HelloWorldProperties helloWorldProperties) {
this.helloWorldProperties = helloWorldProperties;
}
@GetMapping("/")
@ResponseBody
public Map<String, String> hello() {
return Collections.singletonMap("message",
this.helloWorldService.getHelloMessage());
return Collections.singletonMap("message", "Hello " +
this.helloWorldProperties.getName());
}
protected static class Message {

View File

@ -20,13 +20,14 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.ExportMetricWriter;
import org.springframework.boot.actuate.metrics.opentsdb.DefaultOpenTsdbNamingStrategy;
import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbGaugeWriter;
import org.springframework.boot.actuate.metrics.opentsdb.OpenTsdbNamingStrategy;
import org.springframework.boot.actuate.metrics.writer.GaugeWriter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableConfigurationProperties(HelloWorldProperties.class)
public class SampleOpenTsdbExportApplication {
@Bean
@ -40,7 +41,7 @@ public class SampleOpenTsdbExportApplication {
@Bean
@ConfigurationProperties("metrics.names")
public OpenTsdbNamingStrategy namingStrategy() {
public DefaultOpenTsdbNamingStrategy namingStrategy() {
return new DefaultOpenTsdbNamingStrategy();
}

View File

@ -31,6 +31,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -17,11 +17,9 @@
package sample.metrics.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false)
public class HelloWorldService {
public class HelloWorldProperties {
private String name = "World";
@ -33,8 +31,4 @@ public class HelloWorldService {
this.name = name;
}
public String getHelloMessage() {
return "Hello " + this.name;
}
}

View File

@ -31,14 +31,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Description("A controller for handling requests for hello messages")
public class SampleController {
private final HelloWorldProperties helloWorldProperties;
@Autowired
private HelloWorldService helloWorldService;
public SampleController(HelloWorldProperties helloWorldProperties) {
this.helloWorldProperties = helloWorldProperties;
}
@GetMapping("/")
@ResponseBody
public Map<String, String> hello() {
return Collections.singletonMap("message",
this.helloWorldService.getHelloMessage());
return Collections.singletonMap("message", "Hello " +
this.helloWorldProperties.getName());
}
protected static class Message {

View File

@ -24,11 +24,13 @@ import org.springframework.boot.actuate.metrics.export.MetricExportProperties;
import org.springframework.boot.actuate.metrics.jmx.JmxMetricWriter;
import org.springframework.boot.actuate.metrics.repository.redis.RedisMetricRepository;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.jmx.export.MBeanExporter;
@SpringBootApplication
@EnableConfigurationProperties(HelloWorldProperties.class)
public class SampleRedisExportApplication {
@Autowired

View File

@ -16,14 +16,16 @@
package sample.parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldService {
@Autowired
private ServiceProperties configuration;
private final ServiceProperties configuration;
public HelloWorldService(ServiceProperties configuration) {
this.configuration = configuration;
}
public String getHelloMessage(String name) {
return this.configuration.getGreeting() + " " + name;

View File

@ -27,8 +27,12 @@ import org.springframework.util.StreamUtils;
@MessageEndpoint
public class SampleEndpoint {
private final HelloWorldService helloWorldService;
@Autowired
private HelloWorldService helloWorldService;
public SampleEndpoint(HelloWorldService helloWorldService) {
this.helloWorldService = helloWorldService;
}
@ServiceActivator
public String hello(File input) throws Exception {

View File

@ -1 +1 @@
service.greeting: Hello
service.greeting=Hello