Spring Boot 编写入门程序

1. SpringBoot 入门

  • 快速创建独立运行的Spring项目以及与主流框架集成;
  • 使用嵌入式的Servlet容器,应用无需打成WAR包;
  • starters自动依赖与版本控制;
  • 大量的自动配置,简化开发,也可修改默认值;
  • 无需配置XML,无代码生成,开箱即用;
  • 准生产环境的运行时应用监控;
  • 与云计算的天然集成;

2. SpringBoot 之Hello World

2.1 pom.xml 配置

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
 </parent>

 <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 </dependencies>

2.2 编写主程序,用于启动Spring Boot应用

@SpringBootApplication
public class HelloWorldMainApplication{

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

2.3 编写Controller

@Controller
public class HelloController{
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

3. HelloWorld 细节

3.1 POM 文件

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
 </parent>

 他的父项目
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

用来管理Spring Boot应用里面的所有依赖版本;

3.2 导入的依赖(启动器)

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter: spring-boot 场景启动器,帮我们导入了Web模块正常运行所依赖的组件;

4. 使用Spring starter Project创建Spring Boot应用

4.1 默认生成的Spring Boot项目:

  • 主程序已经创建完成;
  • resources文件夹中目录结构:
    • static:保存所有的静态资源,JS,CSS,images;
    • templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面)
      可以使用模板引擎(freemarker,thymeleaf)
    • application.properties:Spring Boot应用的配置文件;

参考资料:

猜你喜欢

转载自www.cnblogs.com/linkworld/p/9131570.html