一、Spring Boot 特点
二、环境搭建
创建spring-boot maven 工程。在pom.xml文件中添加依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.2.3.RELEASE</version></dependency>
创建hello/SampleController.javapackage com.demo.spring.boot.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("/spring-boot")public class HelloController {@RequestMapping(value = "/hello", method = RequestMethod.GET)@ResponseBodypublic String hello() {System.out.println("Hello, this is spring boot");return "Spring Boot";}}
@Controller, 声明该类为controller 类。
@RequestMapping, 声明请求RUL路径,当前例子的URL路径为 “:8080/spring-boot/hello”, method = RequestMethod.GET, 声明请求方法为GET方式。
@ResponseBody, 声明方法返回对象为请求的返回对象。如果不写该声明,那么方法返回值默认为跳转到return的URL路径下。
创建启动类:package com.demo.spring.boot.main;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("com.demo.spring.boot")@EnableAutoConfigurationpublic class Startup {public static void main(String[] args) {SpringApplication app = new SpringApplication(Startup.class);app.run(args);}}
@Configuration, 声明启动配置项。
@ComponentScan, 声明加载bean 包,默认当前类所在的包,可以指定包路径。
@EnableAutoConfiguration, 声明自动配置。
SpringApplication app = new SpringApplication(Startup.class);创建容器,同时可以在该容器下添加各种启动参数。app.run(args); 启动容器。
通过run application方式运行主函数,即可启动容器。
启动容器后, 打开浏览器,访问::8080/spring-boot/hello 就可以在页面中看到输出“Spring Boot”, 同时可以在控制台看到输出信息:Hello, this is spring boot 。
这样,一个简单的spring-boot环境就搭建好了。
版权声明:本文为博主原创文章,未经博主允许不得转载。
,每年的同一天和他庆祝生日,每年的情人节、圣诞节、除夕,