Skip to content

Commit

Permalink
Polish samples
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Dec 30, 2016
1 parent a19a280 commit c903ff4
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 105 deletions.
7 changes: 7 additions & 0 deletions spring-boot-samples/spring-boot-sample-actuator-noweb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
health.diskspace.enabled: false
health.diskspace.enabled=false
6 changes: 4 additions & 2 deletions spring-boot-samples/spring-boot-sample-actuator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions spring-boot-samples/spring-boot-sample-actuator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@
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();
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

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;
Expand All @@ -36,8 +35,11 @@
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,19 +34,12 @@

@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))
Expand Down
7 changes: 7 additions & 0 deletions spring-boot-samples/spring-boot-sample-integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
logging.level.org.springframework.integration.file: DEBUG
service.greeting: Hello
logging.level.org.springframework.integration.file=DEBUG
service.greeting=Hello
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -33,8 +34,4 @@ public void setName(String name) {
this.name = name;
}

public String getHelloMessage() {
return "Hello " + this.name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
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;
Expand All @@ -32,32 +29,21 @@
@Description("A controller for handling requests for hello messages")
public class SampleController {

@Autowired
private HelloWorldService helloWorldService;
private final HelloWorldProperties helloWorldProperties;

private final GaugeService gauges;

@Autowired
private 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

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 {
Expand Down
Loading

0 comments on commit c903ff4

Please sign in to comment.