SpringBoot(5.0)基礎(半成品筆記)

Spring Boot 項目的橫空出世更是引起了空前的關注,這是一個改變遊戲規則的項目,它能
夠極大地簡化配置,高效地管理依賴,並且與當下流行的微服務架構模式契合良好,得到了廣泛
地應用


Servlet 請求接口:ServletRequest或者HttpServletRequest
響應接口:ServletResponse或者HttpServletResponse
Spring5.0將ServerRequest作爲請求接口、ServerRespon作爲響應接口


各目錄的作用
src/main/java java目錄
src/main/resources WEB-APP下的一系列資源
/static 靜態文件目錄
/templates 用於存放模板文件
src/test/java 測試文件
target 編譯後的class輸出文件夾


項目結構
Spring Cloud 分佈式
Spring Boot 承上啓下
Spring Framework 基層


@SpringBootApplication相當於以下三個註解
//創建配置信息
@Configuration
//掃描包
@ComponentScan
//使用自動配置
@EnableAutoConfiguration






Spring Boot配置清單(application.properties文件)
server.port=80 //端口設置
當然還有另兩種方法:
啓動時:SpringApplication.run(MusicApplication.class, "--server.port=80");
執行時:E:\javaee\pl\music\target>java -jar music-cn.anlemusic.music.jar.1.0.jar --server.port=6000
server.address
server.ssl.*
server.tomcat.*


如何執行
java -jar myapp.jar --debug


執行
E:\javaee\pl\music\target>java -jar music-cn.anlemusic.music.jar.1.0.jar










































Spring Boot有四大神器,分別是auto-configuration(自動配置)、starters(起動裝置,根據配置文件)、cli(用於使用Spring進行快速原型搭建)、actuator(監控系統)
actuator是spring boot提供的對應用系統的自省和監控的集成功能,可以對應用系統進行配置查看、相關功能統計等。
<!-- 引入依賴監控程序 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.0之後,需要前面加入/actuator/
/actuator/health
/actuator/info
/actuator
HTTP方法 路徑 描述 鑑權(false默認開啓)
GET /autoconfig 查看自動配置的使用情況 true
GET /configprops 查看配置屬性,包括默認配置 true
GET /beans 查看bean及其關係列表 true
GET /dump 打印線程棧 true
GET /env 查看所有環境變量 true
GET /env/{name} 查看具體變量值 true
GET /health 查看應用健康指標 false
GET /info 查看應用信息 false
GET /mappings 查看所有url映射 true
GET /metrics 查看應用基本指標 true
GET /metrics/{name} 查看具體指標 true
POST /shutdown 關閉應用 true
GET /trace 查看基本追蹤信息 true


開啓方法:
application.yml
management:
  endpoint:
    auditevents:
      enabled: true
  endpoints:
    web:
      exposure:
        include:
          health,info,env,metrics,mappings


或者application.properties
management.endpoint.auditevents.enabled=true
management.endpoints.web.exposure.include=health,info,env,metrics,mappings,beans


pom.xml
<!-- Tomcat程序 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 監控程序 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 測試程序 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>




MusicApplication.java
@SpringBootApplication
@Controller
public class MusicApplication {


@ResponseBody
@RequestMapping(value = "/")
String home() {
return "hello word";
}


public static void main(String[] args) {
SpringApplication.run(MusicApplication.class, args);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章