Fast forward existing prototype work
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package org.springframework.bootstrap.sample.simple;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.bootstrap.CommandLineRunner;
|
||||
import org.springframework.bootstrap.SpringApplication;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.bootstrap.sample.simple.service.HelloWorldService;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan
|
||||
public class SimpleBootstrapApplication implements CommandLineRunner {
|
||||
|
||||
// Simple example shows how a command line spring application can execute an
|
||||
// injected bean service. Also demonstrates how you can use @Value to inject
|
||||
// command line args ('--name=whatever') or application properties
|
||||
|
||||
@Autowired
|
||||
private HelloWorldService helloWorldService;
|
||||
|
||||
@Override
|
||||
public void run(String... args) {
|
||||
System.out.println(this.helloWorldService.getHelloMessage());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SimpleBootstrapApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.springframework.bootstrap.sample.simple.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HelloWorldService {
|
||||
|
||||
@Value("${name:World}")
|
||||
private String name;
|
||||
|
||||
public String getHelloMessage() {
|
||||
return "Hello " + name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
name: Phil
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.springframework.bootstrap.sample.simple;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class SimpleBootstrapApplicationTests {
|
||||
|
||||
private static PrintStream savedOutput;
|
||||
private static ByteArrayOutputStream output;
|
||||
private String profiles;
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
savedOutput = System.out;
|
||||
output = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(output));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
this.profiles = System.getProperty("spring.profiles.active");
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
if (this.profiles != null) {
|
||||
System.setProperty("spring.profiles.active", this.profiles);
|
||||
} else {
|
||||
System.clearProperty("spring.profiles.active");
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void clear() {
|
||||
System.setOut(savedOutput);
|
||||
}
|
||||
|
||||
private static String getOutput() {
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSettings() throws Exception {
|
||||
SimpleBootstrapApplication.main(new String[0]);
|
||||
String output = getOutput();
|
||||
assertTrue("Wrong output: " + output, output.contains("Hello Phil"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandLoneOverrides() throws Exception {
|
||||
SimpleBootstrapApplication.main(new String[] { "--name=Gordon" });
|
||||
String output = getOutput();
|
||||
assertTrue("Wrong output: " + output, output.contains("Hello Gordon"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user