Tiven Wang
Wang Tiven July 25, 2017
425 favorite favorites
bookmark bookmark
share share

A dead tree towers over the jungle canopy in the rain forest of the Mosquito Coast, Honduras.

下載本篇完整代碼 Github


Install Spring CLI

依照文檔 Installing the Spring Boot CLI 步驟安裝Sprring CLI 工具:

  • 下載解壓 spring-boot-cli-1.5.4.RELEASE-bin.zip
  • 創建環境變量 SPRING_HOME
  • 添加 SPRING_HOME/bin 到 path 環境變量中

執行命令 spring version 查看是否成功安裝。

Building Project

我們這裡不是用Spring CLI生成項目代碼,而是一步步手動添加各個組件,以期讀者對項目結構有深入理解。

mkdir try-cf-spring-boot cd try-cf-spring-boot

Build System

創建文件 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <!-- to jar when packaging-->


    <!-- Additional lines to be added here... -->


这是通过继承 spring-boot-starter-parent 来获得默认的配置, 包括 common dependencies, plugins, resource filtering等. 你也可以不继承它而自己配置这些设置,比如添加 spring-boot-dependencies 来引入默认的packages.

            <!-- Import dependency management from Spring Boot -->

使用 mvn dependency:tree 下載依賴並查看項目依賴結構,目前只有一個 wang.tiven:try-cf:jar:0.0.1-SNAPSHOT

我們要開發 web application 所以添加 spring-boot-starter-web module


重新運行 mvn dependency:tree 下載相關依賴。

更多内容请参考 Build systems on Spring Boot Reference Guide.

Structuring Code


package wang.tiven.trycf.web;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

public class HomeController {

    String home() {
        return "Hello World!";

    public static void main(String[] args) throws Exception {
        SpringApplication.run(HomeController.class, args);


運行命令 mvn spring-boot:run 成功啟動 Tomcat 後查看 url http://localhost:8080/ 便可得到 Hello World!

Deploy to Cloud Foundry




然後執行 mvn clean package 可以得到可執行的jar, 可以測試jar可執行性 java -jar target/try-cf-spring-boot-0.0.1-SNAPSHOT.jar

Push to Pivotal Web Services

創建 Cloud Foundry 配置文件 manifest.yml

- name: try-cf-spring-boot
  buildpack: java_buildpack
  instances: 1
  memory: 200M
  host: try-cf-spring-boot
  path: target/try-cf-spring-boot-0.0.1-SNAPSHOT.jar

path 變量指定 artifact 的位置

cf api https://api.run.pivotal.io

cf login

cf push

成功後可以得到可訪問鏈接 http://try-cf-spring-boot.cfapps.io


更改 Spring Boot 的啟動入口, 其中 @SpringBootApplication 為一方便性的註解,它包含了

  • @Configuration 標註此class在應用上下文 application context 中為一定義bean的class。
  • @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
  • 如果 Spring Boot 發現 spring-webmvc on the classpath, 則自動添加 @EnableWebMvc, 你也可以手動添加 @EnableWebMvc
  • @ComponentScan 則說明自動查找其他components, configurations, and services
public class HeroApplication {

  public static void main(String[] args) throws Exception {
    SpringApplication.run(HeroApplication.class, args);


Similar Posts

  • Apache Camel - Testing Testing is a crucial activity in any piece of software development or integration. Typically Camel Riders use various different technologies wired together in a variety of patterns with different expression languages together with different forms of Bean Integration and Dependency Injection so its very easy for things to go wrong! (smile) . Testing is the crucial weapon to ensure that things work as you would expect.
  • Spring Boot - Apache Camel This article shows how to work with a simple Apache Camel application using Spring Boot.
  • Spring Boot - Caching The Spring Framework provides support for transparently adding caching to an application. At its core, the abstraction applies caching to methods, reducing thus the number of executions based on the information available in the cache. The caching logic is applied transparently, without any interference to the invoker.
  • Spring Boot - Basic The Spring Boot Framework.
  • Try Cloud Foundry 13 - Logs Loggregator is the next generation system for aggregating and streaming logs and metrics from all of the user apps and system components in an Elastic Runtime deployment. Loggregator uses Google's protocol buffers along with gRPC to deliver logs.
  • Try Cloud Foundry 12 - Config Server Config Server for Pivotal Cloud Foundry (PCF) is an externalized application configuration service, which gives you a central place to manage an application’s external properties across all environments.


Back to Top