Springboot2.x——idea新建springboot项目及运行

一、springboot简介

这段时间一直写node.js,发现写一个服务太方便了,只要安装node环境和依赖的库 就可以直接开发。而spring开发相对比较麻烦,springboot则是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置

SpringBoot所具备的特征有:

(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;

(2)内嵌Tomcat或Jetty等Servlet容器;

(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;

(4)尽可能自动配置Spring容器;

(5)提供准备好的特性,如指标、健康检查和外部化配置;

(6)绝对没有代码生成,不需要XML配置

二、新建springboot项目

1、JAVA安装及配置环境变量:

查看java环境是否正常:

MacBookPro:~ wuxi$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

2、MAVEN安装及配置环境变量(看个人习惯,也可以使用Gradle配置)

查看maven环境是否正常:

MacBookPro:~ wuxi$ mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: /Users/wuxi/Documents/apache-maven-3.5.0
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.3", arch: "x86_64", family: "mac"

3、安装idea 或者eclipse

https://www.jetbrains.com/idea/download/#section=mac

https://www.eclipse.org/downloads/

4、使用idea新建springboot项目

 Springboot2.x——idea新建springboot项目及运行_第1张图片

 4.1 项目jdk选择自己环境变量配置的,点击NextSpringboot2.x——idea新建springboot项目及运行_第2张图片

4.2 修改Group组织名如:com.wuxi,Artifact项目名:demo,点击Next(springboot为2.0以上版本)

Springboot2.x——idea新建springboot项目及运行_第3张图片

4.3 选择依赖 我这边选web -spring web 进行简单测试,继续下一步,直到完成

检查项目jdk和maven设置是否正确

jdk:File——Project Structure ——Project:

Springboot2.x——idea新建springboot项目及运行_第4张图片

maven:

Springboot2.x——idea新建springboot项目及运行_第5张图片

三、运行springboot项目

1、查看当前项目结构:

Springboot2.x——idea新建springboot项目及运行_第6张图片

 

 

查看当前pom依赖:可以看到自动添加了spring-boot-starter-web 还有测试需要的依赖包



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.0.RELEASE
         
    
    com.wuxi
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

查看程序主类:

@SpringBootApplication
public class DemoApplication {

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

}

 

该类main方法是程序入口,@SpringBootApplication等价于以下注解组合

@SpringBootConfiguration:springboot项目配置注解

@EnableAutoConfiguration:开启自动配置

@ComponentScan:默认扫描@SpringBootApplication注解所在类的同级目录以及它的子目录

2、新建DemoControllar.class文件

//@RestController
@Controller
public class DemoControllar {
    
    @RequestMapping("/")
    @ResponseBody
    public String hello(){
        return "hello world!";
    }

}

@RestController:@Controller+@ResponseBody 用户返回json/string等内容,不能返回jsp、html等页面

@RequestMapping:提供路由信息,http请求指定路径访问映射到指定方法

@ResponseBody:返回json数据

@Controller:可用于返回jsp、html页面,配合@ResponseBody 使用

 

3、运行该项目:命令行:mvn spring-boot:run (或者运行程序配置mvn命令行)

MacBookPro:demo wuxi$ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.2.0.RELEASE:run (default-cli) > test-compile @ demo >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/wuxi/Pictures/blog-picture/demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/wuxi/Pictures/blog-picture/demo/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/wuxi/Pictures/blog-picture/demo/target/test-classes
[INFO] 
[INFO] <<< spring-boot-maven-plugin:2.2.0.RELEASE:run (default-cli) < test-compile @ demo <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.2.0.RELEASE:run (default-cli) @ demo ---
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.0.RELEASE)

2019-10-30 16:18:03.358  INFO 2253 --- [           main] com.wuxi.demo.DemoApplication            : Starting DemoApplication on MacBookPro with PID 2253 (/Users/wuxi/Pictures/blog-picture/demo/target/classes started by wuxi in /Users/wuxi/Pictures/blog-picture/demo)
2019-10-30 16:18:03.360  INFO 2253 --- [           main] com.wuxi.demo.DemoApplication            : No active profile set, falling back to default profiles: default
2019-10-30 16:18:04.223  INFO 2253 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-10-30 16:18:04.237  INFO 2253 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-10-30 16:18:04.238  INFO 2253 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-10-30 16:18:04.317  INFO 2253 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-10-30 16:18:04.317  INFO 2253 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 920 ms
2019-10-30 16:18:04.481  INFO 2253 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-30 16:18:04.639  INFO 2253 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-30 16:18:04.642  INFO 2253 --- [           main] com.wuxi.demo.DemoApplication            : Started DemoApplication in 2.096 seconds (JVM running for 2.426)

内置tomcat,Tomcat started on port(s): 8080,默认8080端口

使用浏览器访问:http://localhost:8080/

测试通过

 

参考:

https://spring.io/projects/spring-boot#overview

https://baike.baidu.com/item/Spring%20Boot/20249767?fr=aladdin#reference-[2]-20954318-wrap

 

 

 

 

 

你可能感兴趣的:(SpringBoot)