Spring Boot的优点
回顾一下我们如何搭建一个SSM(Spring,Spring MVC,MyBatis)web框架,配置web.xml,将权限交给Spring,设置Spring容器的配置文件application.properties,开启自动扫包、配置数据库文件、引用数据源、文件资源过滤等等一系统繁琐的操作之后,我们才能够搭建起来一个框架,光是配置就得耗费我们大把的时间。但是,使用Spring Boot就可以让我们从繁琐的配置工作中解脱出来,将主要的精力集中到编码上。
启动一个简单的Spring Boot
参照Spring Boot快速开始文档,我们先在MyEclipse里面创建一个简单java的maven项目。
New Project,选择Maven Project
勾选创建简单工程,点击Next
填写Group Id和Artifict Id,点击Finish
打开pom文件,按照官网提供的启动方式,引入Spring Boot依赖
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>
</dependencies>
在项目src/main/java的目录下,新建一个包com.init.springCloud,再创建一个我们的Spring Boot启动类FirstApplication,为这个启动类添加一个main()方法
package com.init.springCloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
使用@SpringBootApplication注解标明我们的项目是一个Spring Boot项目,@SpringBootApplication注解是一个整合了多个注解的集合注解,能够完成以前我们需要在配置文件中配置的自动扫包等等的功能。
完成上面启动类的创建,我们的第一个Spring Boot项目也完成了,这个时候,运行main()方法,启动我们的Spring Boot,控制台会输出如图的信息
在控制台我们看到项目的端口是8080,访问一下http://localhost:8080,出现以下页面,我们的项目就算是搭建成功了
补充:Spring Boot是采用内部插件的方式启动项目的,所以这里没有项目名称
Spring Boot的控制器
在com.init.springCloud包下新建class类MyController,为Controller类添加@Controller注解,为方法添加@GetMapping和@ResponseBody注解
package com.init.springCloud;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello";
}
}
之后运行FirstApplication启动类的main()方法,启动项目,访问http://localhost:8080/hello,在浏览器返回了我们的“hello字符串”,Spring Boot的Controller控制器也就测试通过了。
Spring Boot的Rest风格控制器
我们演示让Rest风格的控制器返回一个Person类的JsonString,先在com.init.springCloud包下新建class类Person,这里使用了lombok的解决方案,按步骤操作的同学根据自己实际情况改写Person类
package com.init.springCloud;
import lombok.Data;
@Data
public class Person {
private Integer id; //主键ID
private String name; //姓名
private Integer age; //年龄
}
之后继续在com.init.springCloud包下创建class类MyRestController,使用@RestController标识这是一个Rest风格的控制器,在方法上使用@RequestMapping注解,value属性代表请求路径和参数,method用GET的方式请求(按照Rest风格,查询数据使用的是GET方式),produces表示我们产出的是一个JSON字符串。
package com.init.springCloud;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@RequestMapping(value = "/person/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public Person getPerson(@PathVariable Integer id){
Person person = new Person();
person.setId(id);
person.setName("Spirit");
person.setAge(22);
return person;
}
}
配置完毕以后,运行FirstApplication启动类的main()方法,启动项目,访问http://localhost:8080/person/1,在浏览器端看到了我们的JSON数据,Rest风格的控制器也测试通过了。
Spring boot配置
Spring Boot会默认读取application.properties或者application.yml(yaml语言,这里不细讲了,有需要的同学可自行查阅资料),读取配置文件的先后顺序或者说优先级分别是:
A:根目录的config文件夹下的application.properties
B:根目录下的application.properties
C:src/main/resources目录的config文件夹下的application.properties
D:src/main/resources目录下的application.properties
如果同时配置了多个application.properties,配置文件里又同时存在相同的某个属性,但是属性值不一样,后面读取的配置文件属性值不会覆盖前一个文件的属性值!
使用自定义的配置文件启动Spring Boot项目
我们在src/main/resources下面新建一个名为myapp.properties的file,在这个配置文件里面写上自己的一个配置属性
test.user.name = Spirit
为了查看这个是否读取到我们的属性值,我们在com.init.springCloud包下创建class类MyConfigController,用于在浏览器端请求并展示这个属性值
package com.init.springCloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyConfigController {
@Autowired
private ApplicationContext ctx;
@GetMapping("/prop")
@ResponseBody
public String getName(){
return ctx.getEnvironment().getProperty("test.user.name");
}
}
最后在com.init.springCloud包下创建class类MyConfigApplication作为我们自定义的启动类,内容如下:
package com.init.springCloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class MyConfigApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyConfigApplication.class).properties("spring.config.location=classpath:/myapp.properties").run(args);
}
}
运行MyConfigApplication的main()方法,启动Spring Boot,访问http://localhost:8080/prop,在浏览器端看到我们的请求结果,自定义配置类以及自定义的启动方式就生效了。
多环境下配置文件的切换
按照传统的部署方式,我们通常是把本地的war包或者class字节码文件里面需要修改的文件或者配置等等的一系列信息修改了以后,再放到生产环境或者拿给测试人员做测试。配置文件的大量修改,造成了不必要的工作量,而且繁多的配置容易导致修改的时候出错,Spring Boot恰好提供了那么一种多环境下配置文件切换的方式,可以让我们做最小的修改就能完成生产线的切换,比如从开发状态切换到测试状态,或者切换到产品状态。
我们在src/main/resources目录下新建三个配置文件,分别是application-dev.properties、application-test.properties、application-prod.properties
然后在三个配置文件里面分别写上自定义的属性:
test.user.name = Spirit in dev
test.user.name = Spirit in test
test.user.name = Spirit in prod
之后在src/main/resources目录下的application.properties文件里配置激活对应的配置文件
spring.profiles.active = dev
运行FirstApplication类的main()方法,访问http://localhost:8080/prop,在浏览器端可以看到我们的请求结果,对应环境的配置就被激活了。
结合多环境的配置文件,我们也可以在代码中指定运行环境,不同作用的代码在相应的运行环境下起作用。在com.init.springCloud包下创建class类BeanConfiguration,并用@Configuration注解标知类,用@Bean和@profile标知一个初始化运行的方法,@Profile写明运行时环境,我们就能在项目启动运行时看到方法内部打印出来的信息。注:方法内部使用了Java8的lambda表达式。
package com.init.springCloud;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class BeanConfiguration {
@Bean
@Profile("dev")
public Runnable test1() {
System.out.println("开发环境使用的 Bean");
return () -> {};
}
@Bean
@Profile("test")
public Runnable test2() {
System.out.println("测试环境使用的 Bean");
return () -> {};
}
@Bean
@Profile("prod")
public Runnable test3() {
System.out.println("生产环境使用的 Bean");
return () -> {};
}
}
之后运行FirstApplication类的main()方法,我们注意到控制台输出的信息里面打印出了对应环境的语句。
动态热部署
Spring Boot为我们提供了动态热部署,能然我们实时更新了代码之后自行重新启动项目。在Spring Boot官网文档里面搜索devtools,找到下面提供的maven依赖,拷入到自己的pom文件里面。
为了各位方便操作,我把依赖粘贴出来:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
保存并重启我们的项目,这个时候代码如果做了修改,项目就会自动重新启动,编译部署。
最后,大家有什么不懂的或者其他需要交流的内容,也可以进入我的QQ讨论群一起讨论:654331206
Spring Cloud系列:
Spring Cloud服务管理框架Eureka简单示例(三)
Spring Cloud服务管理框架Eureka项目集群(四)
Spring Cloud整合RabbitMQ或Kafka消息驱动(二十二)