簡述Spring-boot

簡述Spring-boot應用

SpringBoot簡介

       Create stand-alone Spring applicationsjava

       Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)mysql

       Provide opinionated 'starter' dependencies to simplify your build configurationgit

       Automatically configure Spring and 3rd party libraries whenever possiblegithub

       Provide production-ready features such as metrics, health checks and externalized configurationweb

       Absolutely no code generation and no requirement for XML configurationspring

       上面這段話摘自 http://spring.io/projects/spring-boot,簡單介紹了Spring Boot 的優點,翻譯過來就是,標準單一的Spring 應用程序,內置Tomcat,Jetty ,無需打成 war包,提供一個開始依賴來簡化依賴配置,自動配置了Spring 以及第三方依賴庫,提供生產特徵,好比指標,健壯檢查和外部化配置,絕對沒有任何代碼生成,而且不須要配置xml,看到這些與以往的Spring 搭建作出了巨大的優化。
sql

  環境準備

       Jdk  1.7 +數據庫

       Maven 項目管理工具後端

       Eclipse + Spring 插件,Idea,STSspring-mvc

       Mysql 數據庫

  Maven主要依賴

     Spring Boot與簡單SSM項目,作到了簡化配置,它儘量將用戶所需的開發依賴集中一個父類模塊,咱們在搭建Spring Boot 項目的時候能夠發現咱們的項目繼承,以下模塊

<!--父類模塊 -->
<
parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring-boot-version}</version> <relativePath /> <!-- lookup parent from repository --> </parent>

 該模塊能夠說整個項目重要組成部分,由於該父類模塊定義了許多咱們在開發中須要的依賴。

 除此以外,還有下幾個依賴

<dependencies>
<!-- -->
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.starter.version}</version> </dependency> <!-- @ConfigurationProperties --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configurationprocessor</artifactId> <optional>true</optional> </dependency> <!-- druibs數據源配置 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis pageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- swigger 配置 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> </dependencies>

      能夠看到整個項目依賴是極其簡單,由於咱們父類模塊作了許多事,把咱們想要作的事情都作了,在早期開發SSM項目時候,有時候一個項目中咱們可能會丟失一些依賴,如今這個問題已經沒有了。我說一下幾個主要的依賴,

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

      上面這個依賴,開啓項目spring-mvc模塊,你們在搭建項目的時候能夠點進去看一下,能夠看到以下一些細節

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.9.RELEASE</version>
      <scope>compile</scope>
  </dependency>
  <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.9.RELEASE</version>
      <scope>compile</scope>
  </dependency>

        能夠看到它也是把咱們的Spring Mvc 依賴給加載進來,跟咱們以前作的工做差很少,用專業點的詞語就是"封裝"。

 同理咱們看一下以下這個依賴

<dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>${mybatis.spring.boot.starter.version}</version>
</dependency>

        一樣點擊去看一下

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
  </dependency>
  <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
  </dependency>
 <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
  </dependency>

       我能夠看到MyBaits這幾個大字出如今咱們眼前,因此仍是那個道理。

  spring boot 啓動介紹

       Spring Boot 自身與Spring Mvc 作了無縫鏈接,不須要其它依賴,咱們以前已經說過了,它自身已經集成了Spring Mvc,我要作是按照它的要求編寫咱們的代碼,我能夠先看看SpringBoot 項目的目錄結構

             

        這是個人項目的目錄結構,咱們要注意一點,代碼的包必須與主類平齊,這樣咱們的寫的類才能被Spring Boot 程序的容器所註冊或者實例化,Spring Boot 項目自己就是一個Spring 容器,在以前咱們不使用Spring Boot 項目時候,咱們不少時候都是用xml配置去註冊bean,而如今是經過註解方式來註冊,實例化bean,我看一下怎麼來實例化bean,

       我能夠先看一下啓動類

@SpringBootApplication(scanBasePackages="com.ysdevelop.oa")
@MapperScan("com.ysdevelop.oa.dao") 
@EnableTransactionManagement 
public class YuanshengOaApplication {

    public static void main(String[] args) {
        SpringApplication.run(YuanshengOaApplication.class, args);
    }
}

     在這裏我要介紹一下@SpringBootApplication,該註解開啓了Spring Boot啓動,我能夠看看該註解作了什麼

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

}

     這個註解開啓了Spring ComponentScan,經過它能夠去掃描咱們的本身的類中的註解,好比@Service,@Controller,@Autowired 等其餘註解,除此以外還有自動化配置。這個註解一句話就是開啓包掃描,以及自動化配置,我能夠經過配scanBasePackages

來指定所須要的包。

     可能還有一個問題,Spring Boot 程序是怎麼來啓動咱們的web應用,與傳統war部署方式相比,如今就是作了一點改進,它把Tomcat 服務器直接內置到項目中,這樣免得咱們去折騰,一旦到部署到線上,能夠搭配Nginx來作反向代理來實現路由分發。

  mybaits,spring mvc配置

     以前我簡單說了一下程序的啓動,如今來講一下配置,Spring Boot 是支持yml配置,同時也支持properties配置,首先會讀取application配置文件,

        

      從配置文件中能夠看出,Spring Boot 項目配置更加集中配置,數據庫鏈接,數據源,這是主要配置,我沒有貼出其它配置,可是這已經能說明Spring Boot 強大。若是項目是先後端分離的,我不須要對Spring Mvc 配置,這裏還缺乏了端口配置。

      還要說明必定,爲了更方便項目階段切換,Spring Boot 支持多配置文件切換,好比test,dev,pro,也能夠在啓動jar的時候指定配置文件。 

     

傳統Spring對比

    與以前的傳統的Spring 項目相比,Spring Boot 優化了配置,內嵌Tomcat服務器,去xml化,註解多元化,這是我對Spring Boot 的簡單理解,不喜勿噴。 

相關文章
相關標籤/搜索