Spring boot 小Demo


首先配置好Maven環境,並配置到你的開發工具中。
1.去官網下載Maven壓縮包。
 

下載后綴名為zip的那個。
 

 

下載完成后解壓到你的某個硬盤內。我的路徑是D:\apache-maven-3.3.9
然后把這個路徑添加到你電腦的環境變量中去。
打開你的DOS輸入命令mvn --version測試成功界面為
 
2.配置到你的開發工具當中
我這里用的是myeclipse8.6版本。
windows-preferences-maven
 

 

添加你的maven路徑並應用。
改寫settings.xml在第53行左右,添加一個你自己創建的文件夾作倉庫路徑。

 完成后,為下圖所示。

 

 

以上就是所有maven的配置。

下面就進入我們的Speing boot demo 了。
1.新建一個maven項目
 

 
項目加載完后大致為下圖的構造,此處要注意,Maven項目的建立必須聯網,耐心等待右下角讀條完畢再去寫碼。首先編寫pom.xml
<? xml version = "1.0" encoding = "UTF-8" ?>  
< 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.example </ groupId >  
   < artifactId > myFirstproject </ artifactId >  
    < version > 0.0.1-SNAPSHOT </ version >  
 
    <!-- Inherit defaults from Spring Boot -->  
     < parent >  
        < groupId > org.springframework.boot </ groupId >  
      < artifactId > spring-boot-starter-parent </ artifactId >  
        < version > 1.4.0.BUILD-SNAPSHOT </ version >  
    </ parent >  
 
   <!-- Add typical dependencies for a web application -->  
    < dependencies >  
        < dependency >  
            < groupId > org.springframework.boot </ groupId >  
            < artifactId > spring-boot-starter-web </ artifactId >  
        </ dependency >  
  </ dependencies >  
 
    <!-- Package as an executable jar -->  
       < build >  
      < plugins >  
            < plugin >  
                < groupId > org.springframework.boot </ groupId >  
                < artifactId > spring-boot-maven- plugin </ artifactId >  
           </ plugin >  
        </ plugins >  
    </ build >  
 
    <!-- Add Spring repositories -->  
    <!-- (you don't need this if you are using a .RELEASE version) -->  
    < repositories >  
       < repository >  
            < id > spring-snapshots </ id >  
            < url > http://repo.spring.io/snapshot </ url >  
            < snapshots >< enabled > true </ enabled ></ snapshots >  
        </ repository >  
        < repository >  
            < id > spring-milestones </ id >  
            < url > http://repo.spring.io/milestone </ url >  
        </ repository >  
    </ repositories >  
    < pluginRepositories >  
        < pluginRepository >  
            < id > spring-snapshots </ id >    
                     < url > http://repo.spring.io/snapshot </ url >  
       </ pluginRepository >  
       < pluginRepository >  
            < id > spring-milestones </ id >  
            < url > http://repo.spring.io/milestone </ url >  
       </ pluginRepository >  
    </ pluginRepositories >  
</ project >
把需要用到的包加載進來。

 

 

如圖,只需編寫Example.java和index.jsp即可完成一個最基本的Demo
 
 
package SpringBoot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController ;
 
@RestController
@EnableAutoConfiguration
public class Example {
            
             @RequestMapping ( "/" )
            String home(){
                         return "Hello World!" ;
            }
            
             public static void main(String[] args) {
                        SpringApplication. run(Example. class , args);
            }
 
}
index.jsp
 
< html >
< body >
< h2 > Hello World!Spring Boot </ h2 >
</ body >
</ html >
web.xml
 
<! DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
< web-app >
  < display-name > Archetype Created Web Application </ display-name >
</ web-app >
右擊項目運行,選擇你寫的Example.java.等待項目跑起。
 

 


 
跑完后在地址欄輸入http://localhost:8080/index.jsp即可看到Html的內容

 


至此一個簡單的Spring Boot 的小Demo就完成了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM