天天看點

Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

maven版本控制工具:管理JAR

一、系統要求

  1. JDK:maven3.3以上需要jdk1.7或以上
  2. Eclipse
  3. Maven程式包

二、Maven 下載下傳與安裝

  1. Maven 下載下傳位址:http://maven.apache.org/download.cgi
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 将檔案解壓到D:\Maven\apache-maven-3.6.3目錄下
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  2. 建立環境變量MAVEN_HOME,指派D:\Maven\apache-maven-3.6.3
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 編輯環境變量Path,加上%MAVEN_HOME%\bin;
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 通過win+R打開cmd,輸入mvn -v檢視是否安裝成功
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

三、配置Maven本地倉庫

  1. 并在該目錄下建立maven-repository,作maven的本地庫。
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  2. 打開D:\Maven\apache-maven-3.6.3\conf\settings.xml檔案,,修改預設倉庫的路徑:

改為

  1. localRepository節點用于配置本地倉庫,用以本地緩存,maven項目中運作mvn install,項目将會自動打包到本地倉庫中。
  2. 運作mvn help:system指令

    ***mvn help:system作用:***首次執行 mvn help:system 指令,Maven相關工具自動到Maven中央倉庫下載下傳預設的或者更新的各種配置檔案和類庫(jar包)到Maven本地倉庫中。

    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    運作成功後,D:\Maven\apache-maven-3.6.3\maven-repository會有一些檔案,這些是 mvn help:system 指令成功後列印出的所有Java系統屬性和環境變量資訊。
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

四、在Eclipse配置Maven環境

  1. Eclipse,打開Window->Preferences->Maven->Installations,右側點選Add,設定maven的安裝目錄,然後Finish。
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 打開Window->Preferences->Maven->User Settings,配置如下完成後Apply:
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

五、建立maven工程

  1. 建立Maven工程
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  2. maven工程下包的作用
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 編輯pom.xml增加依賴的jar包
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.MavenHelloWorld</groupId>
  <artifactId>HelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- 引入父級依賴 -->
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.6.RELEASE</version>
 </parent>
  <!-- 加入web依賴 應用spring springmvc -->
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
       </dependency>        
 </dependencies>
 <!--加入jdk1.8插件 -->
 <build>    
        <plugins>    
            <plugin>    
                <groupId>org.apache.maven.plugins</groupId>    
                <artifactId>maven-compiler-plugin</artifactId>    
                <configuration>    
                    <source>1.8</source>    
                    <target>1.8</target>    
                </configuration>    
            </plugin>    
        </plugins>    
</build> 
</project>
           

4.建立類controller

@EnableAutoConfiguration //啟用自動配置
@Controller//配置controller注解
@ConfigurationProperties(prefix="book")//
public class UserController {
// @Value("${book.name}")
 private String name;
// @Value("${book.author}")
 private String author;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
 @RequestMapping("hello")
 @ResponseBody//配置界面響應
 public String helloTest() {
  System.out.println(name+""+author);
  return "hello";
 }
 public static void main(String[] args) {
  SpringApplication.run(UserController.class, args);//springboot運作入口
 }
}
           

六、springboot基礎知識

  1. 配置檔案,用來改變tomcat的預設端口号
  • 支援兩種格式
Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  • 第一種: .yml(key:value)
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  • 第二種:.properties(key=value)
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
  1. 擷取properties的value方法
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    第一種方法
//不需要生成getter setter方法
@Value("${book.name}")
 private String name;
@Value("${book.author}")
 private String author;
           

第二種方法

@ConfigurationProperties(prefix="book")//封裝類,前提是properties字首名稱是一緻的 ,需要生成getter setter方法

 private String name;
 private String author;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAuthor() {
  return author;
 }
 public void setAuthor(String author) {
  this.author = author;
 }
           
  1. 類型安全的配置
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

    當自定義屬性很多時,value 注解就很麻煩

    1):在定義屬性時,注意用相同的字首

    2):使用@ConfigurationProperties(prefix=“book”) prefix 填入定義屬性時的字首

    3):使用@VALUE時不需要生成setter和getter,但類型安全比對則需要生成

  2. start poom

    為工程提供自動配置的Bean,做相關的開發時,隻要加入相關的stater poom即可

spring-boot-starter  Spring Boot 核心的starter,包含自動配置、日志、yaml配置檔案的支援等spring-boot-starter-actuator  準生産特性,用來監控和管理應用
spring-boot-starter-amqp  對”進階消息隊列協定”的支援,通過spring-rabbit實作
spring-boot-starter-aop   對面向切面程式設計的支援,包括spring-aop和AspectJ
spring-boot-starter-batch   對Spring Batch的支援,包括HSQLDB資料庫
spring-boot-starter-cloud-connectors   對Spring Cloud Connectors的支援,簡化在雲平台下(例如,Cloud Foundry 和Heroku)服務的連接配接
spring-boot-starter-data-elasticsearch   對Elasticsearch搜尋和分析引擎的支援,包括spring-data-elasticsearch
spring-boot-starter-data-gemfire   對GemFire分布式資料存儲的支援,包括spring-data-gemfire
spring-boot-starter-data-jpa   對”Java持久化API”的支援,包括spring-data-jpa,spring-orm和Hibernate
spring-boot-starter-data-mongodb   對MongoDB NOSQL資料庫的支援,包括spring-data-mongodb
spring-boot-starter-data-rest   對通過REST暴露Spring Data倉庫的支援,通過spring-data-rest-webmvc實作
spring-boot-starter-data-solr   對Apache Solr搜尋平台的支援,包括spring-data-solr
spring-boot-starter-freemarker   對FreeMarker模闆引擎的支援
spring-boot-starter-groovy-templates   對Groovy模闆引擎的支援
spring-boot-starter-hateoas   對基于HATEOAS的RESTful服務的支援,通過spring-hateoas實作
spring-boot-starter-hornetq   對”Java消息服務API”的支援,通過HornetQ實作
spring-boot-starter-integration   對普通spring-integration子產品的支援
spring-boot-starter-jdbc   對JDBC資料庫的支援
spring-boot-starter-jersey   對Jersey RESTful Web服務架構的支援
spring-boot-starter-jta-atomikos   對JTA分布式事務的支援,通過Atomikos實作
spring-boot-starter-jta-bitronix   對JTA分布式事務的支援,通過Bitronix實作
spring-boot-starter-mail   對javax.mail的支援
spring-boot-starter-mobile   對spring-mobile的支援
spring-boot-starter-mustache   對Mustache模闆引擎的支援
spring-boot-starter-redis   對REDIS鍵值資料存儲的支援,包括spring-redis
spring-boot-starter-security   對spring-security的支援
spring-boot-starter-social-facebook   對spring-social-facebook的支援
spring-boot-starter-social-linkedin   對spring-social-linkedin的支援
spring-boot-starter-social-twitter   對spring-social-twitter的支援
spring-boot-starter-test   對常用測試依賴的支援,包括JUnit, Hamcrest和Mockito,還有spring-test子產品
spring-boot-starter-thymeleaf   對Thymeleaf模闆引擎的支援,包括和Spring的內建
spring-boot-starter-velocity   對Velocity模闆引擎的支援
spring-boot-starter-web   對全棧web開發的支援,包括Tomcat和spring-webmvc
spring-boot-starter-websocket   對WebSocket開發的支援
spring-boot-starter-ws   對Spring Web服務的支援
spring-boot-starter-remote-shell   添加遠端ssh shell支援
spring-boot-starter-jetty   導入Jetty HTTP引擎(作為Tomcat的替代)
spring-boot-starter-log4j   對Log4J日志系統的支援
spring-boot-starter-logging   導入Spring Boot的預設日志系統(Logback)
spring-boot-starter-tomcat   導入Spring Boot的預設HTTP引擎(Tomcat)
spring-boot-starter-undertow   導入Undertow HTTP引擎(作為Tomcat的替代)
           
  1. @EnableAutoConfiguration自動配置原理

    預設情況下,執行spring.factories 找到相關的類路徑,然後執行個體化,類中有的屬性如沒有且全局的application.properties中沒有覆寫,就使用預設的屬性值

    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識
    Maven安裝與配置以及建立maven基礎工程、springboot基礎知識

繼續閱讀