From 429cdcee61367131d99d711305c7c22597bc5652 Mon Sep 17 00:00:00 2001 From: Calvin Date: Sun, 12 Oct 2014 23:45:26 +0800 Subject: [PATCH] =?UTF-8?q?#394=20update=20Spring=20Boot=20demo=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.txt | 7 +- examples/bootservice/README.txt | 66 +++++++++++++++++++ examples/bootservice/pom.xml | 19 ++---- .../bootservice/BootServiceApplication.java | 3 +- .../functional/TaskRestServiceTest.java | 49 +++++++------- 5 files changed, 99 insertions(+), 45 deletions(-) create mode 100644 examples/bootservice/README.txt diff --git a/README.txt b/README.txt index 2f397d07..41dd94a2 100644 --- a/README.txt +++ b/README.txt @@ -2,12 +2,13 @@ It shows the mainstream technologies and pragmatic practice in JavaEE world. - It has 2 major examples: + It has 4 major examples: 1. Quickstart -- a minimal CRUD Todo-List web application. 2. Showcase -- demonstrate many other interesting technologies. + 3. BootService -- a Spring Boot based WebService application. + 4. BootWeb -- a Spring Boot based Web application(TODO). ------------------------------- Offical Site: http://springside.github.io -Document: https://github.com/springside/springside4/wiki -CI Status: [![Build Status](https://travis-ci.org/springside/springside4.svg?branch=master)](https://travis-ci.org/springside/springside4) \ No newline at end of file +Document: https://github.com/springside/springside4/wiki \ No newline at end of file diff --git a/examples/bootservice/README.txt b/examples/bootservice/README.txt new file mode 100644 index 00000000..40e0c2fe --- /dev/null +++ b/examples/bootservice/README.txt @@ -0,0 +1,66 @@ +Things not in demo + +1. use applicactionContextxml + + @ImportResource("applicationContext.xml") + public class BootServiceApplication { + } + + +2. use Jetty instead of Tomcat + +3. disable web in junit test use spring context + + setWebEnvironment(false) + +4. set system exit code + + org.springframework.boot.ExitCodeGenerator + +5. read configuration + @Value(${name}) + String name + + or + + @Autowired + Environment env; + env.get + +6. set the configuration file path + + java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties + + or just change the file name + + java -jar myproject.jar --spring.config.name=myproject + +7. active profile + java -jar myproject.jar --spring.profiles.active=dev,hsqldb + + SpringApplication.setAdditionalProfiles(…​) + +8. define logging level in appliation.properties + +logging.level.org.springframework.web: DEBUG +logging.level.org.hibernate: ERROR + +9. add spring-mvc defenation +wtrite WebMvcConfigurerAdapter without @EnableWebMvc + +10. add servlet/filter + +in application.properties + +ServletRegistrationBean or FilterRegistrationBean + +11. configure web + +add context path +server.contextPath + +set tomcat threads + +12. JMX + +@ManagedResource, @ManagedAttribute, @ManagedOperation diff --git a/examples/bootservice/pom.xml b/examples/bootservice/pom.xml index 82d9ce53..551bb2f3 100644 --- a/examples/bootservice/pom.xml +++ b/examples/bootservice/pom.xml @@ -12,6 +12,11 @@ 4.3.0-SNAPSHOT 1.1.8.RELEASE + + UTF-8 + 1.6 + ${java.version} + ${java.version} @@ -93,20 +98,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - ${java.version} - ${java.version} - - - - org.apache.maven.plugins - maven-jar-plugin - 2.4 - org.apache.maven.plugins maven-war-plugin diff --git a/examples/bootservice/src/main/java/org/springside/examples/bootservice/BootServiceApplication.java b/examples/bootservice/src/main/java/org/springside/examples/bootservice/BootServiceApplication.java index 578f09c0..17cf4f96 100644 --- a/examples/bootservice/src/main/java/org/springside/examples/bootservice/BootServiceApplication.java +++ b/examples/bootservice/src/main/java/org/springside/examples/bootservice/BootServiceApplication.java @@ -7,7 +7,8 @@ import org.springframework.context.annotation.Configuration; // Spring Java Config的标识 @Configuration -@ComponentScan("org.springside.examples.bootservice") +// Spring 默认扫描ClassPath中的Component,以本类所在地package为起点。 +@ComponentScan // Spring Boot的AutoConfig @EnableAutoConfiguration public class BootServiceApplication { diff --git a/examples/bootservice/src/test/java/org/springside/examples/bootservice/functional/TaskRestServiceTest.java b/examples/bootservice/src/test/java/org/springside/examples/bootservice/functional/TaskRestServiceTest.java index 2bfc32f8..382f4f30 100644 --- a/examples/bootservice/src/test/java/org/springside/examples/bootservice/functional/TaskRestServiceTest.java +++ b/examples/bootservice/src/test/java/org/springside/examples/bootservice/functional/TaskRestServiceTest.java @@ -20,46 +20,41 @@ import static org.assertj.core.api.Assertions.*; import java.net.URI; import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.StringUtils; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; -import org.springframework.boot.SpringApplication; -import org.springframework.context.ConfigurableApplicationContext; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.client.RestTemplate; import org.springside.examples.bootservice.BootServiceApplication; import org.springside.examples.bootservice.domain.Task; import org.springside.examples.bootservice.domain.User; import org.springside.modules.test.data.RandomData; +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = BootServiceApplication.class) +@WebAppConfiguration +@IntegrationTest("server.port=0") +@DirtiesContext public class TaskRestServiceTest { - private static ConfigurableApplicationContext context; - private RestTemplate restTemplate = new RestTemplate(); - private String resourceUrl = "http://localhost:8080/task"; + @Value("${local.server.port}") + private int port; - @BeforeClass - public static void start() throws Exception { - Future future = Executors.newSingleThreadExecutor().submit( - new Callable() { + private RestTemplate restTemplate; + private String resourceUrl; - public ConfigurableApplicationContext call() throws Exception { - return SpringApplication.run(BootServiceApplication.class); - } - }); - context = future.get(60, TimeUnit.SECONDS); - } - - @AfterClass - public static void stop() { - if (context != null) { - context.close(); - } + @Before + public void setup() { + restTemplate = new TestRestTemplate(); + resourceUrl = "http://localhost:" + port + "/task"; } @Test