一起學JAVA之《spring boot》03 - 開始spring boot基本配置及項目結(jié)構(gòu)

一后频、導航

本節(jié)內(nèi)容簡介:
1. spring boot 配置文件帝簇,使用@SpringBootApplication注解
2. spring boot 修改Java版本 和項目編碼
3. 一個標準的spring boot 代碼結(jié)構(gòu)
4. 查看當前項目自動配置了那些模塊
5. 禁用自動配置
6. 自定義banner及關(guān)閉banner

一、spring boot 配置文件残揉,使用@SpringBootApplication注解

spring boot 默認使用application.properties或者application.yml,放置在src/main/resources目錄或者類路徑的/config下抱环,一般建議就放在src/main/resources
這里我們使用application.properties來配置,這里我們試著修改下端口和訪問路徑
目錄結(jié)構(gòu)如下:
[圖片上傳失敗...(image-fb1bc8-1524582350472)]
配置代碼:

server.port=8081
server.context-path=/boot

編寫測試controller類

package com.likeoak.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試Controller
 * The type Test controller.
 */
@RestController
public class TestController {
    /**
     * 返回 String 字符串眶痰,訪問成功竖伯,返回“test ok”
     * Test string.
     *
     * @return the string
     */
@RequestMapping("/test")
    public String test(){

        return  "test ok!";
    }

}

啟動main方法因宇,及運行APP啟動類

package com.likeoak;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


/**
 * 默認啟動類
 */
@SpringBootApplication
public class App
{
    public static void main( String[] args )
    {

        SpringApplication.run(App.class,args);
    }
}

訪問:http://localhost:8081/boot/test
結(jié)果:test ok!

代碼解釋:
@SpringBootApplication 解釋
先看下注解@SpringBootApplication的源碼

@SpringBootApplication的源碼

@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 {

里面包含@SpringBootConfiguration的源碼

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

總結(jié):@SpringBootApplication注解其實是@Configuration察滑,@EnableAutoConfiguration贺辰,@ComponentScan這三個注解組合

注解解釋
@Configuration 注解:標明該類使用Spring是基于java的配置
@EnableAutoConfiguration :開啟自動配置注解,有這個注解spring boot就會根據(jù)我們所引用的jar包來自動配置我們需要的配置莽鸭,這正是spring boot 魔力滓侍。

@ComponentScan:spring掃描注解撩笆,有這個注解spring boot 就會掃描(默認是以根路徑為準)所有的包,來加載所有的@Bean,所有這里的TestController 就是被掃描到的氮兵,我們就可以訪問了歹鱼。

二、spring boot 修改Java版本 和項目編碼

在使用spring bootde 過程中掺涛,想自定義java配置疼进,可以使用以下配置,加載到pom.xml中即可

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

三、一個標準的spring boot 代碼結(jié)構(gòu)

一個典型的spring boot 項目結(jié)構(gòu)拣帽,這也是官網(wǎng)推薦的

com
 +- example
     +- myproject
         +- Application.java
         |
         +- domain
         |   +- Customer.java
         |   +- CustomerRepository.java
         |
         +- service
         |   +- CustomerService.java
         |
         +- web
             +- CustomerController.java

四减拭、 查看當前項目自動配置了那些模塊

查看當前項目有哪些自動配置拧粪,一共有三種方法

  1. 直接運行jar -jar xxx.jar --debug
  2. 在application中設(shè)置屬性
debug=true
  1. 直接在啟動的時候沧侥,增加啟動參數(shù)


    idea 增加啟動參數(shù)--debug

我們可以選著任何一種正什,訪問結(jié)果如下
已啟動配置:

Positive matches:
-----------------

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)

   DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition)

   DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched:
      - @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition)

....

未自動配置:

Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition)

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.springframework.batch.core.launch.JobLauncher', 'org.springframework.jdbc.core.JdbcOperations' (OnClassCondition)

...

五、 禁用自動配置

比如不想自動配置數(shù)據(jù)庫連接盾致,就可以用如何代碼來關(guān)掉自動配置

/**
 * 測試關(guān)閉數(shù)據(jù)庫自動配置
 * The type Data source config.
 */
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DataSourceConfig {

}

六庭惜、自定義banner及關(guān)閉banner

自定義spring boot 默認啟動圖案步驟:

  1. 直接在src/main/resources下創(chuàng)建一個banner.txt
  2. 訪問網(wǎng)站http://patorjk.com/software/taag 生成字符护赊,這里我們用"yiqixuejava"(一起學java),將生成的字符復制到banner.txt中,啟動應(yīng)用即可

啟動結(jié)果:

        .__         .__                              __
 ___.__.|__|   _____|__| ___  _____ __   ____       |__|____ ___  _______
<   |  ||  |  / ____/  | \  \/  /  |  \_/ __ \      |  \__  \\  \/ /\__  \
 \___  ||  | < <_|  |  |  >    <|  |  /\  ___/      |  |/ __ \\   /  / __ \_
 / ____||__|  \__   |__| /__/\_ \____/  \___  > /\__|  (____  /\_/  (____  /
 \/              |__|          \/           \/  \______|    \/           \/


后續(xù)會繼續(xù)推出這一系列spring boot的文章

本人網(wǎng)站:一起學JAVA
一起學習QQ群:581665151

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市判耕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帚豪,老刑警劉巖狸臣,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異统翩,居然都是意外死亡厂汗,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門娶桦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來衷畦,“玉大人知牌,你說我怎么就攤上這事∑谢欤” “怎么了扁藕?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵亿柑,是天一觀的道長。 經(jīng)常有香客問我疟游,道長乡摹,這世上最難降的妖魔是什么采转? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮框全,結(jié)果婚禮上干签,老公的妹妹穿的比我還像新娘。我一直安慰自己喘沿,他們只是感情好竭贩,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布窄赋。 她就那樣靜靜地躺著忆绰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪错敢。 梳的紋絲不亂的頭發(fā)上伐债,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天致开,我揣著相機與錄音双戳,去河邊找鬼糜芳。 笑死充石,一個胖子當著我的面吹牛荠商,可吹牛的內(nèi)容都是我干的扣墩。 我是一名探鬼主播呻惕,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼亚脆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了键耕?” 一聲冷哼從身側(cè)響起屈雄,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤棚亩,失蹤者是張志新(化名)和其女友劉穎讥蟆,沒想到半個月后纺阔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體笛钝,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡结榄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年臼朗,在試婚紗的時候發(fā)現(xiàn)自己被綠了视哑。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片誊涯。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡跪呈,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出庆械,到底是詐尸還是另有隱情缭乘,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站奴紧,受9級特大地震影響黍氮,放射性物質(zhì)發(fā)生泄漏沫浆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一淮捆、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧本股,春花似錦攀痊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至躬审,卻和暖如春涩笤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背盒件。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留舱禽,地道東北人炒刁。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像誊稚,于是被迫代替她去往敵國和親翔始。 傳聞我的和親對象是個殘疾皇子渤闷,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容