Spring Boot--Start Up

22391 ワード

Java Webをする学生は、プロジェクト起動時にservlet容器に入れて実行する必要があることを知っています。どのIDEを使っても、非常に面倒なことです。ずっと前にservlet容器の下で多くのプロジェクトを下に置いて一緒に運行していましたが、今の時代には多くのサービスが一台足りなくなりました。ですから、多くの場合、一つの容器はもともと一つのプロジェクトのためだけにサービスしています。このようにすると、容器式のサーバーは、包装してserverを再起動する必要があります。
Spring Bootはプロジェクトを最小限にできる配置であり、外部の容器に追加配置する必要がなく、プロジェクトのマイクロサービスのフレームワークを高速に起動することができる。どうやって生産力を解放しますか?
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.3.1.RELEASE)
POM.XMLに設定を追加します。
現在Spring Bootの最新バージョンは  1.3.1.RELEASEです。ここではmavenをプロジェクトの管理方案として使っています。もちろん、プロジェクト管理方案としてSpring Bootを追加してもいいです。
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>1.3.1.RELEASEversion>
parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>
dependencies>
コードの作成
package hello;

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

@Controller
@EnableAutoConfiguration
public class SampleApplication {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleApplication.class, args);
    }
}
その後、このようなmain()の方法を直接実行すると、コンサートの中で以下の情報が印刷されます。
.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.3.1.RELEASE)

......
2016-01-03 20:40:15.732  INFO 12899 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f021e6c: startup date [Sun Jan 03 20:40:15 CST 2016]; root of context hierarchy
2016-01-03 20:40:17.909  INFO 12899 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-03 20:40:19.232  INFO 12899 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-03 20:40:19.256  INFO 12899 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
......
この時、簡単なwebプロジェクトが起動されました。
プロジェクト起動の設定を変更したい場合は、「サービスのポート、最大サポートスレッド数など」として、resourceディレクトリの下に、必要な配置を記入してください。具体的なパラメータは公式のマニュアルを参照することができます。
効果のデモンストレーション
私たちはこの127.0.0.1:8080のアドレスにアクセスするように命令しています。
➜  ~  http get 127.0.0.1:8080
HTTP/1.1 200 OK
Content-Length: 12
Content-Type: text/plain;charset=UTF-8
Date: Sun, 03 Jan 2016 12:52:03 GMT
Server: Apache-Coyote/1.1

Hello World!
プロジェクトの起動に成功し、アクセスできるようになりました。
実はまだ容器です。
コンサート・ソロ情報からは、Spring Bootはやはりtomcatを使っていますが、tomcatは組み込みサーバになりました。もちろん、私達もbeanを通じて管理して、デフォルトのtomcat案を他の方式に変えてもいいです。
後に書く
過去に私達はいくつかウェブの小さいプロジェクトを書いて、とても複雑な過程で、調整は不便で、繰り返しtomcatをスタートさせて、ユニットのテストの内容と容器は高度に結合して、配置はとても多くて、Spring Bootは銀弾ではありませんが、しかしよくいくつか問題を解決しました。