springboot学习--《二》

来源:互联网 发布:php拍卖系统源码 编辑:程序博客网 时间:2024/06/05 14:28

springboot之springloader


所谓热部署,就是在应用正在运行的时候升级软件,却不需要重新启动应用. 在SpringBoot中启用热部署是非常简单的一件事,因为SpringBoot为我们提供了一个非常方便的工具spring-boot-devtools,我们只需要把这个工具引入到工程里就OK了,下面我就说一下怎么引入spring-boot-devtools.
1. spring-boot-devtools
spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的App上面去。原理是在发现代码有更改之后,重新启动应用,但是速度比手动停止后再启动还要更快,更快指的不是节省出来的手工操作的时间。
其深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为 restart ClassLoader
,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间(5秒以内)


1. 添加依赖包:


<!--     spring-boot-devtools工具包 --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-devtools</artifactId>    <optional>true</optional>    <scope>true</scope></dependency>

2. 添加spring-boot-maven-plugin:


<plugins>    <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>        <configuration>        <!--            fork :  如果没有该项配置,肯呢个devtools不会起作用,            即应用不会restart         -->            <fork>true</fork>        </configuration>    </plugin></plugins>

说明:
1. devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的。
2. devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现(这里注意不同的模板配置不一样)。
测试方法: 修改类–>保存:应用会重启 修改配置文件–>保存:应用会重启
修改页面–>保存:应用会重启,页面会刷新(原理是将spring.thymeleaf.cache设为false)

2.

使用FastJson解析JSON数据

<!-- 使用json框架fastjson --><dependency>      <groupId>com.alibaba</groupId>      <artifactId>fastjson</artifactId>      <version>1.2.15</version>      <!-- 支持的最低版本1.2.10 --></dependency>

第一种方法:


(1)启动类继承extends WebMvcConfigurerAdapter
(2)覆盖方法configureMessageConverters

@SpringBootApplicationpublic class ApiCoreApp  extends WebMvcConfigurerAdapter {    @Override    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {        super.configureMessageConverters(converters);        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setSerializerFeatures(                SerializerFeature.PrettyFormat        );        fastConverter.setFastJsonConfig(fastJsonConfig);        converters.add(fastConverter);    }}

第二种方法:


(1) 在App.java启动类中,
(2) 注入Bean : HttpMessageConverters

@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();        FastJsonConfig fastJsonConfig = new FastJsonConfig();        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);        fastConverter.setFastJsonConfig(fastJsonConfig);        HttpMessageConverter<?> converter = fastConverter;        return new HttpMessageConverters(converter);}

源码下载:http://download.csdn.net/download/qq_37334428/10160655