SpringBoot学习:IDEA中快速搭建springboot项目

项目下载地址:http://download.csdn.net/download/insis_mo/10156486

(一)IDEA中创建maven web项目


创建好项目后设置项目的编译路径:


(二)引入spring-boot项目所需的jar包:

[html]  view plain  copy
  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>1.5.2.RELEASE</version>  
  5.     <relativePath/>  
  6.   </parent>  
  7.   <properties>  
  8.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  9.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  10.     <java.version>1.8</java.version>  
  11.     <spring-boot.version>1.5.2.RELEASE</spring-boot.version>  
  12.   </properties>  
  13.   <dependencies>  
  14.     <dependency>  
  15.       <groupId>junit</groupId>  
  16.       <artifactId>junit</artifactId>  
  17.       <version>3.8.1</version>  
  18.       <scope>test</scope>  
  19.     </dependency>  
  20.     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->  
  21.     <dependency>  
  22.       <groupId>org.springframework.boot</groupId>  
  23.       <artifactId>spring-boot</artifactId>  
  24.       <version>${spring-boot.version}</version>  
  25.     </dependency>  
  26.     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->  
  27.     <dependency>  
  28.       <groupId>org.springframework.boot</groupId>  
  29.       <artifactId>spring-boot-starter</artifactId>  
  30.       <version>${spring-boot.version}</version>  
  31.     </dependency>  
  32.     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->  
  33.     <!-- spring-web, spring-webmvc Spring WebMvc框架 -->  
  34.     <!-- tomcat-embed-* 内嵌Tomcat容器 -->  
  35.     <!-- jackson 处理json数据 -->  
  36.     <!-- spring-* Spring框架 -->  
  37.     <!-- spring-boot-autoconfigure Spring Boot提供的自动配置功能 -->  
  38.     <dependency>  
  39.       <groupId>org.springframework.boot</groupId>  
  40.       <artifactId>spring-boot-starter-web</artifactId>  
  41.       <version>${spring-boot.version}</version>  
  42.       <!--<exclusions>  
  43.         <exclusion>  
  44.           <groupId>org.springframework.boot</groupId>  
  45.           <artifactId>spring-boot-starter-tomcat</artifactId>  
  46.         </exclusion>  
  47.       </exclusions>-->  
  48.     </dependency>  
  49.     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->  
  50.     <dependency>  
  51.       <groupId>org.springframework.boot</groupId>  
  52.       <artifactId>spring-boot-starter-test</artifactId>  
  53.       <version>${spring-boot.version}</version>  
  54.     </dependency>  
  55.     <dependency>  
  56.       <groupId>org.springframework.boot</groupId>  
  57.       <artifactId>spring-boot-starter-tomcat</artifactId>  
  58.       <version>${spring-boot.version}</version>  
  59.     </dependency>  
  60.     <dependency>  
  61.       <groupId>org.springframework.boot</groupId>  
  62.       <artifactId>spring-boot-starter-actuator</artifactId>  
  63.       <version>${spring-boot.version}</version>  
  64.     </dependency>  
  65.   </dependencies>  
  66.   <build>  
  67.     <finalName>springBoot</finalName>  
  68.     <plugins>  
  69.       <plugin>  
  70.         <groupId>org.springframework.boot</groupId>  
  71.         <artifactId>spring-boot-maven-plugin</artifactId>  
  72.       </plugin>  
  73.     </plugins>  
  74.   </build>  
(三)配置文件、启动类:

项目路径如下:

application.yml配置文件中内容为:

[html]  view plain  copy
  1. #server  
  2. server:  
  3.   # 项目contextPath,一般在正式发布版本中,我们不配置  
  4.   context-path: /boot  
  5.   # 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置  
  6.   #address: 192.168.0.101  
  7.   # 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知  
  8.   error:  
  9.      path: /error  
  10.   # 服务端口  
  11.   port: 8080  
  12.   # session最大超时时间(分钟),默认为30  
  13.   session:  
  14.     timeout: 60  
  15.   tomcat:  
  16.     # tomcat的URI编码  
  17.     uri-encoding: utf-8  
  18.     # tomcat最大线程数,默认为200  
  19.     max-threads: 1000  
  20.     # 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)  
  21.     basedir: G:/springboot-tomcat-tmp  
  22.     # 打开Tomcat的Access日志,并可以设置日志格式的方法:  
  23.     #server.tomcat.access-log-enabled=true  
  24.     #server.tomcat.access-log-pattern=  
Application.java启动类如下,spring会扫描该类所在目录下的java类:

[html]  view plain  copy
  1. package com.sun;  
  2.   
  3. import org.springframework.boot.CommandLineRunner;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6.   
  7. /**  
  8.  * @Configuration:标注此文件为一个配置项  
  9.  * @EnableAutoConfiguration:使用自动配置  
  10.  * @ComponentScan:可扫描的  
  11.  * @SpringBootApplication 包含上面三个  
  12.  * Application:启动管理器  
  13.  * Created by sun on 2017-1-14.  
  14.  */  
  15. @SpringBootApplication  
  16. public class Application implements CommandLineRunner {  
  17.   
  18.     /* Servlet容器默认的Context路径是/  
  19.     DispatherServlet匹配的路径(servlet-mapping中的url-patterns)是*//*  
  20.     @ComponentScan路径被默认设置为SampleController的同名package,  
  21.     也就是该package下的所有@Controller,@Service, @Component, @Repository都会被实例化后并加入Spring Context中。*/  
  22.     public static void main(String[] args) {  
  23.         SpringApplication springApplication = new SpringApplication(Application.class);  
  24.         springApplication.run(args);  
  25.     }  
  26.   
  27.     // springboot运行后此方法首先被调用  
  28.     // 实现CommandLineRunner抽象类中的run方法  
  29.     @Override  
  30.     public void run(String... args) throws Exception {  
  31.         System.out.println("项目启动完成!");  
  32.     }  
  33. }  
TestController类:

[html]  view plain  copy
  1. package com.sun.test.controller;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.ResponseBody;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8.   
  9. /**  
  10.  * 测试类  
  11.  * Created by sun on 2017-1-14.  
  12.  */  
  13. @Controller  
  14. @RequestMapping("/test")  
  15. public class TestController {  
  16.   
  17.     @RequestMapping("/")  
  18.     @ResponseBody  
  19.     String test(HttpServletRequest req){  
  20.         return "Hello World!";  
  21.     }  
  22.   
  23. }  

启动Application,在页面上输入http://localhost:8080/boot/test/ 页面上打印出Hello World!
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值